[英]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. 在这种情况下,我可能实例化
SqlConnection
和PrincipalContext
类型,并返回该实例。 The method takes in a single parameter, type Object
. 该方法接受单个参数,类型为
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. 我的问题是方法的返回类型是
Object
,因此在调用方法时需要强制转换。
An example would be: 一个例子是:
SqlConnection connection2 = new SqlConnection();
SqlConnection sqlCon = (SqlConnection)ConnectionFactory.RolloutConnectionType(connection2);
And the RolloutConnectionType method: 和RolloutConnectionType方法:
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
. 我认为我正处于正确的轨道上,但它似乎非常混乱,并且可能与创建实例所需的实例多余 -
connection2
在RolloutConnectionType
创建并返回obj
。 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
. 你可以去泛型(
Create<T>()
),但现在你要为不是PrinicpalContext
或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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.