简体   繁体   English

.net核心中的Unity框架

[英]Unity framework in .net core

I am new to .net core. 我是.net核心的新手。 Need help setting up unity framework. 需要帮助建立统一框架。 Here is what i tried. 这是我试过的。

I added reference to System.Configuration.ConfigurationManager .net standard V2.0 我添加了对System.Configuration.ConfigurationManager .net标准V2.0的引用

Then created app.config 然后创建了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>

In class i try reading the configuration , but getting NULL. 在类中我尝试读取配置,但获取NULL。

UnityConfigurationSection section =


(UnityConfigurationSection)ConfigurationManager.GetSection("unity");

If you mean the Unity DI Container Framework, you don't need to set it up because dotnet core comes with it's own IoC DI container. 如果你的意思是Unity DI容器框架,你不需要设置它,因为dotnet core附带了它自己的IoC DI容器。 Also the dotnet core uses appSettings.json config files. dotnet核心也使用appSettings.json配置文件。 In Startup.cs there should be this method: 在Startup.cs中应该有这个方法:

public void ConfigureServices(IServiceCollection services) 
{

}

And you can configure your dependencies using the services object, like this: 您可以使用services对象配置依赖项,如下所示:

services.AddSingleton<IContract, Contract>();

There are other options on how you can configure your dependencies, I've just presented the Singleton one, but you can go from here. 关于如何配置依赖项还有其他选项,我刚刚介绍了Singleton,但你可以从这里开始。

The easiest way to check this is to start a new project: 检查这个的最简单方法是启动一个新项目:

dotnet new mvc -n testProj

And check the Startup.cs file, add an interface, an implemenatation to it, then register it into the IServiceCollection instance. 并检查Startup.cs文件,添加接口,实现它,然后将其注册到IServiceCollection实例中。

in app.config can you try this 在app.config中可以试试这个

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection"/>

instead of this line 而不是这一行

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>

Here is how i finally implemented. 这是我最终实现的方式。 In startup class i configured the dependencies 在启动类中,我配置了依赖项

 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>();
    }

Here is code to resolve it. 这是解决它的代码。 using DependencyFactory.Resolve(); 使用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.

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