简体   繁体   中英

C# - Method to Create Instances of Different Types

I've created a method that acts as a factory to roll out the type of connection I need. In this case, I'm possibly instantiating types SqlConnection and PrincipalContext , and returning that instance. The method takes in a single parameter, type Object . If the parameter value is of the specified type above, it will create an instance of that object. My issue is the return type of the method is Object , so a cast is required when the method is called.

An example would be:

SqlConnection connection2 = new SqlConnection();
SqlConnection sqlCon = (SqlConnection)ConnectionFactory.RolloutConnectionType(connection2);

And the RolloutConnectionType method:

public static Object RolloutConnectionType(Object obj) {
    if (obj == null) {
        if (obj is PrincipalContext) {//create new PrincipalContext
            string user, pass, domain;
            domain = ConfigurationManager.AppSettings["SAdomain"];
            user = ConfigurationManager.AppSettings["SAuser"];
            pass = ConfigurationManager.AppSettings["SApass"];

            obj = new PrincipalContext(ContextType.Domain, domain + ".mydomain.ca", "CN=MyCN,DC=myDC,DC=ca", user, pass);
        } else if (obj is SqlConnection) {//create new SqlConnection
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

            obj = new SqlConnection(connStr);
        }
    }
    return obj;
}

I think I'm on the right track with this, but it seems very messy and likely redundant with an instance required to create an instance - connection2 to create and return obj in RolloutConnectionType . It works, but I don't like how it works. Is what I'm attempting possible? Are there other avenues I could pursue?

Assuming you want to stick with the factory pattern, you should be looking at something like the following:

public interface Factory<T>
{
    T Create();
}

public class PrincipalContextFactory : IFactory<PrincipalContext>
{
    public PrinicipalContext Create()
    {
        // return new PrincipalContext(...);
    }
}

public class SqlConnectionFactory : IFactory<SqlConnection>
{
    public SqlConnection Create()
    {
        // return new SqlConnection(...);
    }
}

Your factory shouldn't need anything more than to return a new instance of what you're after. You could go generics ( Create<T>() ), but now you're creating a bunch of edge cases for models not a PrinicpalContext or SqlConnection .

Why not just have your factory class with static methods of each thing your are trying to create and have each method return its own properly type-cast object

public static class YourFactory
{
   public static SqlConnection GetConnection()
   {
      string connStr = System.Configuration.ConfigurationManager
                          .ConnectionStrings["MyConnectionString"].ConnectionString;
      return new SqlConnection(connStr);
   }

   public static PrincipalContext GetPrincipalContext()
   {
        string user, pass, domain;
        domain = ConfigurationManager.AppSettings["SAdomain"];
        user = ConfigurationManager.AppSettings["SAuser"];
        pass = ConfigurationManager.AppSettings["SApass"];

        return new PrincipalContext(ContextType.Domain, domain + ".mydomain.ca",
                 "CN=MyCN,DC=myDC,DC=ca", user, pass);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM