DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比
参考:https://www.cnblogs.com/xishuai/p/3670292.html
必读:
在unity1.2中我们使用构造器注入、属性注入和方法注入会有parameterType节点,就是说在constructor、property和method这些节点可以配置这些方式注入所依赖的类型,但是在unity2.0并不存在parameterType节点了,所有类型注册都是通过register节点进行配置的,相当于unity1.2中的type节点,虽然unity2.0存在constructor、property和method节点,但我感觉只是针对构造器、属性和方法本身进行注入。另外在unity2.0配置中alias节点下的生命管理周期配置并不需要了。
一.下载安装:
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
下载地址:
二.配置:
1.在App_Start文件夹下创建UnityConfig.cs文件
using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using System.Web.Mvc; using ZLP.BLL; using ZLP.DAL; using ZLP.IBLL; using ZLP.IDAL; using ZLP.MVC.Controllers; namespace ZLP.MVC { public class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); //代码注入 // container.RegisterType<IUserDAL, UserDAL>(); //container.RegisterType<IUserBLL, UserBLL>(); //配置文件注入 var section=(UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName); section.Configure(container); DependencyResolver.SetResolver(new ZLPDependencyResolver(container)); } } }
2.在App_Start文件夹下创建ZLPDependencyResolver.cs文件(创建一个实现IDependencyResolver的类)
using Microsoft.Practices.Unity; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace ZLP.MVC { public class ZLPDependencyResolver : IDependencyResolver { public ZLPDependencyResolver(IUnityContainer container) { this.container = container; } IUnityContainer container; public object GetService(Type serviceType) { try { return this.container.Resolve(serviceType); } catch (Exception) { return null; } } public IEnumerable<object> GetServices(Type serviceType) { try { return this.container.ResolveAll(serviceType); } catch (Exception) { return Enumerable.Empty<object>(); } } } }
3.在Global文件中Application_Start方法中增加一行代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace ZLP.MVC { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); UnityConfig.RegisterComponents();//增加unity的方法调用 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace ZLP.MVC { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); UnityConfig.RegisterComponents();//增加unity的方法调用 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace ZLP.MVC { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); UnityConfig.RegisterComponents();//增加unity的方法调用 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
三.配置文件:
版本:1.2
1.2中的节点:
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> </configSections> <unity> <typeAliases> <typeAlias alias="IUserBLL" type="ZLP.IBLL.IUserBLL,ZLP.IBLL" /> <typeAlias alias="UserBLL" type="ZLP.BLL.UserBLL,ZLP.BLL" /> </typeAliases> <containers> <container name="defaultContainer"> <type type="IUserBLL" mapTo="UserBLL" name="a"></type > </container> </containers> </unity>
版本:2.0
2.0中节点变更为如下:
- The <unity> Configuration Section
- The <container> Element
- The <register> Element
- The <lifetime> Element
- The <constructor> Element
- The <property> Element
- The <method> Element
- The <param> Element
- The <dependency> Element
- The <value> Element
- The <optional> Element
- The <array> Element
- The <extension> Element
- The <instance> Element
- The <namespace > Element
- The <alias> Element
- The <sectionExtension> Element
-
-
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" /> </configSections> <unity> <alias alias="IUserDAL" type="ZLP.IDAL.IUserDAL,ZLP.IDAL" /> <alias alias="UserDAL" type="ZLP.DAL.UserDAL,ZLP.DAL" /> <alias alias="IUserBLL" type="ZLP.IBLL.IUserBLL,ZLP.IBLL" /> <alias alias="UserBLL" type="ZLP.BLL.UserBLL,ZLP.BLL" /> <container> <register type="IUserDAL" mapTo="UserDAL"></register> <register type="IUserBLL" mapTo="UserBLL"></register> </container> </unity>
-
- 详细的查看官方地址:https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff660914(v=pandp.20)?redirectedfrom=MSDN#config_registerelement
原文地址:https://www.cnblogs.com/zlp520/p/12020153.html
时间: 2024-11-13 19:31:21