我正在审查我之前编写的代码,我注意到我过去曾这样做过
public class Linq2DbSettings : ILinqToDBSettings
{
public IEnumerable<IDataProviderSettings> DataProviders
{
get { yield break; }
}
public string DefaultConfiguration =>
"SqlServer"; // lets set your configuration as default, so you can call just new DataContext() or new DataConnection()
public string DefaultDataProvider => ProviderName.SqlServer; // and set default database type
public IEnumerable<IConnectionStringSettings> ConnectionStrings
{
get
{
yield return
new ConnectionStringSettings
{
Name = "SqlServer",
ProviderName = "SqlServer",
ConnectionString =ConfigurationManager.ConnectionStrings["default"].ConnectionString
};
}
}
}
public class ConnectionStringSettings : IConnectionStringSettings
{
public string ConnectionString { get; set; }
public string Name { get; set; }
public string ProviderName { get; set; }
public bool IsGlobal => false;
}
即使它与 Linq2Db 相关,它也适用于我需要解析容器的所有类。
正如你所看到的,我在这里使用ConfigurationManager.ConnectionStrings["default"],而最好使用IConfiurationfromMicrosoft.Extensions.Configuration
为此,我应该解析IConfiguration在 SimpleInjector 的容器中注册的项目。
过去我用过包装纸
public static class ContainerWrapper
{
public static Container Container { get; set; }
}
我将其指定为
ContainerWrapper.Container = container;
container.Verify();
但我认为这是一个错误的方法,最好的解决方案是什么?
拉风的咖菲猫
相关分类