Unity 2.x依賴注入by運行時注入[附源碼]
Unity 2.x依賴注入(控制反轉)IOC,對於沒有大項目經驗的童鞋來說,這些都是陌生的名詞,甚至有些同學還停留在拉控件的階段。
您可以訪問http://unity.codeplex.com/releases得到最新版本的Unity現在。當然,如果您在您的visual studio 中安裝了Nuget 包管理器,你可以直接在Nuget中獲取到最新版本的Unity。貌似最新是3了,第5講我們糟糕的代碼演示了接口如何用
這裡http://unity.codeplex.com/documentation我們找到了幫助文檔大家可以下載下來看看
我們采用的是構造函數注入,運行時注入。
這塊的概念可以算算是本系統最模糊的,大家應該好好理解下,博客園一位大蝦的
【ASP.Net MVC3 】使用Unity 實現依賴注入 大家進去看看
這裡我不再詳說了。
貼出代碼,告訴大家怎麼做就好了。
下載http://files.cnblogs.com/ymnets/Microsoft.Practices.Unity.rar
在App.Admin創建Library放進去,以後我們要用到的類庫都放到這裡來,除非說明,引用的類庫都是開源的。
App.Core引用Microsoft.Practices.Unity.dll , System.Web.Mvc, System.Web,3個類庫和4.BLL,App.IBLL,App.DAL,App.IDAL 4個類庫
添加以下2個類
using System; using System.Collections.Generic; using System.Linq; using System.Web; using App.BLL; using App.DAL; using App.IBLL; using App.IDAL; using Microsoft.Practices.Unity; namespace App.Core { public class DependencyRegisterType { //系統注入 public static void Container_Sys(ref UnityContainer container) { container.RegisterType<ISysSampleBLL, SysSampleBLL>();//樣例 container.RegisterType<ISysSampleRepository, SysSampleRepository>(); } } }
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace App.Core { public class UnityDependencyResolver : IDependencyResolver { private const string HttpContextKey = "perRequestContainer"; private readonly IUnityContainer _container; public UnityDependencyResolver(IUnityContainer container) { _container = container; } public object GetService(Type serviceType) { if (typeof(IController).IsAssignableFrom(serviceType)) { return ChildContainer.Resolve(serviceType); } return IsRegistered(serviceType) ? ChildContainer.Resolve(serviceType) : null; } public IEnumerable<object> GetServices(Type serviceType) { if (IsRegistered(serviceType)) { yield return ChildContainer.Resolve(serviceType); } foreach (var service in ChildContainer.ResolveAll(serviceType)) { yield return service; } } protected IUnityContainer ChildContainer { get { var childContainer = HttpContext.Current.Items[HttpContextKey] as IUnityContainer; if (childContainer == null) { HttpContext.Current.Items[HttpContextKey] = childContainer = _container.CreateChildContainer(); } return childContainer; } } public static void DisposeOfChildContainer() { var childContainer = HttpContext.Current.Items[HttpContextKey] as IUnityContainer; if (childContainer != null) { childContainer.Dispose(); } } private bool IsRegistered(Type typeToCheck) { var isRegistered = true; if (typeToCheck.IsInterface || typeToCheck.IsAbstract) { isRegistered = ChildContainer.IsRegistered(typeToCheck); if (!isRegistered && typeToCheck.IsGenericType) { var openGenericType = typeToCheck.GetGenericTypeDefinition(); isRegistered = ChildContainer.IsRegistered(openGenericType); } } return isRegistered; } } } UnityDependencyResolver.cs
在系統開始運行時候我們就把構造函數注入。所以我們要在Global文件寫入代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using App.Core; using Microsoft.Practices.Unity; namespace App.Admin { // 注意: 有關啟用 IIS6 或 IIS7 經典模式的說明, // 請訪問 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); //啟用壓縮 BundleTable.EnableOptimizations = true; BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); //注入 Ioc var container = new UnityContainer(); DependencyRegisterType.Container_Sys(ref container); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } } } Global.asax.cs
好了,我們已經把