c# Unity DI容器不接受带有RegisterType函数的Enum
本文关键字:RegisterType 函数 Enum 不接受 Unity DI | 更新日期: 2023-09-27 17:52:56
见下面的代码
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.local);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.remote);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.OrcsWeb);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.Sage);
Unity RegisterType不接受enum,但如果我传递字符串,则没有问题发生,但我必须使用enum。我的完整代码如下。所以有人看到我的代码,告诉我在代码中修复什么和哪里,结果enum应该被接受。
public enum ConType { local, remote, OrcsWeb, Sage, };
public interface IBBAConnection
{
IDbConnection GetConnection();
string ConType { get; set; }
}
public class BBAConnection : IBBAConnection
{
public ConType ConType { get; set; }
public IDbConnection GetConnection()
{
string _connectionString = "";
IDbConnection connection = null;
try
{
// inside if else logic we fetch connection string from ini file or from any source and inistialize connection.
if (ConType == ConType.local)
{
_connectionString = "put here local db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.remote)
{
_connectionString = "put here remote db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.OrcsWeb)
{
_connectionString = "put here website db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.Sage)
{
_connectionString = "put here sage connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
connection.Open();
}
catch (Exception ex)
{
string strErr = ex.Message;
}
return connection;
}
}
public static class Factory
{
static IUnityContainer cont = null;
public static IBBAConnection initialize(ConType oConType)
{
IBBAConnection oDbConnection = null;
cont = new UnityContainer();
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.local);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.remote);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.OrcsWeb);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.Sage);
oDbConnection = cont.Resolve<IBBAConnection>(oConType);
//oDbConnection.ConType = type;
return oDbConnection;
}
}
public enum ConType { local, remote, OrcsWeb, Sage, };
public interface IBBAConnection
{
IDbConnection GetConnection();
string ConType { get; set; }
}
public class BBAConnection : IBBAConnection
{
public ConType ConType { get; set; }
public IDbConnection GetConnection()
{
string _connectionString = "";
IDbConnection connection = null;
try
{
// inside if else logic we fetch connection string from ini file or from any source and inistialize connection.
if (ConType == ConType.local)
{
_connectionString = "put here local db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.remote)
{
_connectionString = "put here remote db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.OrcsWeb)
{
_connectionString = "put here website db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == ConType.Sage)
{
_connectionString = "put here sage connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
connection.Open();
}
catch (Exception ex)
{
string strErr = ex.Message;
}
return connection;
}
}
public static class Factory
{
static IUnityContainer cont = null;
public static IBBAConnection initialize(ConType oConType)
{
IBBAConnection oDbConnection = null;
cont = new UnityContainer();
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.local);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.remote);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.OrcsWeb);
cont.RegisterType<IBBAConnection, BBAConnection>(ConType.Sage);
oDbConnection = cont.Resolve<IBBAConnection>(oConType);
//oDbConnection.ConType = type;
return oDbConnection;
}
}
寻找指南,结果Enum应该被接受。
我认为你应该这样做。
这将大大降低BBAConnection
的复杂性,因为你让你的IConnectionConfig
绑定决定你需要的连接字符串。
public interface IConnectionConfig
{
string GetConnectionString();
}
public class LocalConnectionConfig : IConnectionConfig
{
public string GetConnectionString()
{
return "db connection for local";
}
}
public class BBAConnection : IBBAConnection
{
private readonly IConnectionConfig config;
public BBAConnection(IConnectionConfig config)
{
this.config = config;
}
public IDbConnection GetConnection()
{
string _connectionString = "";
IDbConnection connection = null;
try
{
connection = new System.Data.SqlClient.SqlConnection(this.config.GetConnectionString());
connection.Open();
}
catch (Exception ex)
{
string strErr = ex.Message;
}
return connection;
}
}
注册:
container.RegisterType<IBBAConnection, BBAConnection>();
container.RegisterType<IConnectionConfig, LocalConnectionConfig>();
概念上讲
您通常会让您的构建配置定义您正在使用的配置。然后你可以在你的代码中使用它来定义你需要的配置。