簡體   English   中英

.net核心中的Unity框架

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM