[英]Unity framework in .net core
我是.net核心的新手。 需要帮助建立统一框架。 这是我试过的。
我添加了对System.Configuration.ConfigurationManager .net标准V2.0的引用
然后创建了app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--In older version, Microsoft.Practices.Unity.Configuration.dll is part of older version (around 3.5.1404). In newer version,
Microsoft.Practices.Unity.Configuration.UnityConfigurationSection class is moved to Unity.Configuration.dll.-->
<!--<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>-->
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<!--Old syntax-->
<!--<typeAliases>
<typeAlias alias="IDBAccess" type="Interfaces.IDataProvider, Interfaces" />
<typeAlias alias="SQLDataAccess" type="SQLDataProvider.DataProvider, SQLDataProvider" />
</typeAliases>-->
<!--New syntax supported in newer versions. So if above syntax does not work then try below one-->
<alias alias="IDBAccess" type="Interfaces.IDataProvider, Interfaces" />
<alias alias="SQLDataAccess" type="SQLDataProvider.DataProvider, SQLDataProvider" />
<alias alias="OracleDataAccess" type="OracleDataProvider.DataProvider, OracleDataProvider" />
<containers>
<container name="DataAccessProvider">
<register type="IDBAccess" mapTo="SQLDataAccess"/>
<register type="IDBAccess" mapTo="SQLDataAccess" name="SQLDataAccess" />
<register type="IDBAccess" mapTo="OracleDataAccess" name="OracleDataAccess" />
</container>
</containers>
</unity>
</configuration>
在类中我尝试读取配置,但获取NULL。
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
如果你的意思是Unity DI容器框架,你不需要设置它,因为dotnet core附带了它自己的IoC DI容器。 dotnet核心也使用appSettings.json配置文件。 在Startup.cs中应该有这个方法:
public void ConfigureServices(IServiceCollection services)
{
}
您可以使用services对象配置依赖项,如下所示:
services.AddSingleton<IContract, Contract>();
关于如何配置依赖项还有其他选项,我刚刚介绍了Singleton,但你可以从这里开始。
检查这个的最简单方法是启动一个新项目:
dotnet new mvc -n testProj
并检查Startup.cs文件,添加接口,实现它,然后将其注册到IServiceCollection实例中。
在app.config中可以试试这个
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection"/>
而不是这一行
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
这是我最终实现的方式。 在启动类中,我配置了依赖项
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddScoped<IRepositoryFactory, RepositoryFactory>();
services.AddScoped<IMapperFactory, MapperFactory>();
services.AddScoped<ITestService, testtService>();
// services.AddScoped<IMapperFactory, MapperFactory>();
}
这是解决它的代码。 使用DependencyFactory.Resolve();
public DependencyFactory(IServiceCollection services)
{
_container=services;
}
public static T Resolve<T>()
{
T ret = default(T);
var provider = _container.BuildServiceProvider();
if (provider.GetService<T>() !=null)
{
ret = (T)provider.GetService<T>();
}
return ret;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.