.NET MVC4 實訓記錄之一(引入Unity3.0 Ioc架構)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   io   

  一直在做維護項目,沒有機會接觸完整的架構,於是自學.NET MVC。自今日起,將自學的過程、遇到的問題以及解決方案記錄下來。

  在WebApp項目中右鍵,使用NuGet引入Unity3.0。

  

  引入後項目目錄下會自動產生一個Bootstrapper.cs檔案。

  

   手動在WebApp根目錄下建立IOC.config檔案,用於配置IOC規則。(注意:之前看網上教程中的配置文 件,<typeAliases></typeAliases>節點和<unity></unity>節 點平級,但運行時總是異常,找不到IBaseService的定義。應該是沒有找到自訂類型所在的程式集。我 將<typeAliases></typeAliases>節點移到<unity></unity>的 內部後,異常沒有了。)

 1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 5 Microsoft.Practices.Unity.Configuration" /> 6 </configSections> 7  8 <unity> 9 <typeAliases>10 <!--生命週期類型別名-->11 <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />12 <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />13 <!--使用者定義的類型別名-->14 <typeAlias alias="IBaseService" type="Framework.Services.IBaseService, Framework.Services" />15 <typeAlias alias="BaseService" type="Framework.Services.BaseService, Framework.Services" /> 16 </typeAliases>17 <containers>18 <container>19 <types>20       <!--預設為全生命週期,即為單例模式-->21 <type type="IBaseService" mapTo="BaseService" />22 </container>23 </containers>24 </unity>25 <startup>26 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>27 </startup>28 </configuration>

  修改Bootstrapper.cs檔案中的 BuildUnityContainer() 方法:

 1    private static IUnityContainer BuildUnityContainer() 2  { 3 var container = new UnityContainer(); 4  5 // register all your components with the container here 6 // it is NOT necessary to register your controllers 7  8 //擷取IOC設定檔位置 9 string configFile = System.Web.HttpRuntime.AppDomainAppPath + "IOC.config";10 //設定擴充設定檔類型11 var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };12 //從config檔案中讀取配置資訊13 System.Configuration.Configuration config =14  ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);15 //擷取指定名稱的配置節16 UnityConfigurationSection configuration = (UnityConfigurationSection)config.GetSection("unity"); 17 //配置IOC容器18  configuration.Configure(container);19 20 return container;21 }

  在Globale.asax.cs 中初始化IOC容器:

 1         protected void Application_Start() 2  { 3  AreaRegistration.RegisterAllAreas(); 4  5  WebApiConfig.Register(GlobalConfiguration.Configuration); 6  FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 7  RouteConfig.RegisterRoutes(RouteTable.Routes); 8  BundleConfig.RegisterBundles(BundleTable.Bundles); 9  AuthConfig.RegisterAuth();10 11 Bootstrapper.Initialise(); //初始化IOC容器12 }

  在Controller中添加IBaseService類型的屬性,加上[Dependency]標記,這樣在該Controller中就可以直接使用。

  

  IOC架構Unity 3.0引入完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.