Unity4.0的使用


最近公司用到了Unity,自己就研究了一下。

新建一個ASP.NET MVC基本項目,在NuGet上引入 Unity4.0.1最新版。
因為我使用的項目為ASP.NET MVC,所以又添加一個Unity bootstrapper for ASP.NET MVC。
NuGet會幫你在App_Start文件夾下自動添加兩個文件 UnityConfig.csUnityMvcActivator.cs
 
文件 UnityConfig.cs是配置Unity依賴注入用的,可以分為兩種
這兩種方式的不同點在於,修改依賴注入時,如果是方式一可以在系統運行時修改(支持熱插拔)。
文件UnityWebActivator.cs是我們創建的依賴關系注冊到MVC中。
我們需要做的是在Global.asax文件中將Unity運行。
 
下面來詳細講述一下這兩方式的配置:
方式一使用 web.config配置:
1.在 UnityConfig.cs下將注釋取消container.LoadConfiguration()
        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();
        }

2.在configSections節點下添加以下內容

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

3.配置unity節點信息

  <unity  xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly  name="IBLL"/>
    <assembly  name="BLL"/>
    <containers>
      <container>
        <register  type="IBLL.ITest,IBLL" mapTo="BLL.Test,BLL" />
      </container>
    </containers>
  </unity>

 配置完成運行就ok了。

這是我遇到一個問題,找了很多資料才解決的
原來web.config是這樣配置的
  <unity  xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <containers>
      <container>
        <register  type="IBLL.ITest" mapTo="BLL.Test" />
      </container>
    </containers>
  </unity>

這種情況下會報這種錯誤

The type name or alias IBLL.ITest could not be resolved. Please check your configuration file and verify this type name.
 
 
方式二使用代碼配置:
這種方式十分的簡單基本上我們只要參考作者給的提示就ok
UnityConfig.cs下配置如下代碼:
        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            //container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();
            container.RegisterType<IBLL.ITest,BLL.Test>();
        }

以上是個人使用經驗分享給大家,上面的內容比較淺顯,如果有錯誤請大家指正

 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM