DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比

DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比

参考:https://www.cnblogs.com/xishuai/p/3670292.html

参考:https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff660914(v=pandp.20)?redirectedfrom=MSDN#config_registerelement

必读:

在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-08-26 03:25:33

DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比的相关文章

DI 依赖注入之unity(mvc)

DI 依赖注入之unity(mvc) 一.nuget下载安装: 使用Nuget安装Unity.MVC 安装完成后会在~/App_Start/目录下自动生成UnityMvcActivator.cs和UnityConfig.cs文件 二.配置: 打开UnityConfig文件,修改RegisterTypes()方法的代码 public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.co

框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示

1 什么是spring 1.1官网 spring.io 1.2介绍 Spring的核心是控制反转(IoC)和面向切面(AOP). IoC(Inverse of Control 反转控制) AOP(Aspect Oriented Programming 面向切面编程为内核) 简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架. *轻量级:依赖其他内容较小,使用资源消耗也少.对比:EJB 重量级 *分层:经典三层体系架构,spring 提供解决方案

谈谈php里的IOC控制反转,DI依赖注入

理论 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和"DI依赖注入"是什么,能够解决什么问题,这些在维基百科中有非常清晰的说明. 控制反转(Inversion of Control,缩写为IoC):是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度. 依赖注入(Dependency Injection,简称DI):DI是IOC的一种实现,表现为:在类A的实例创建过程中即创建了依赖的B对象,通过类型或名称来判断将不同的对象注入到不同的属

谈谈php里的IOC控制反转,DI依赖注入(转)

转自:http://www.cnblogs.com/qq120848369/p/6129483.html 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和"DI依赖注入"是什么,能够解决什么问题,这些在维基百科中有非常清晰的说明. 控制反转(Inversion of Control,缩写为IoC):是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度. 依赖注入(Dependency Injection,简称DI):DI是IOC的一种实现

【Spring】DI 依赖注入传递参数的方式

DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致) 注入基本数据类型和String类型 通过setter方法注入基本数据类型与String 案例: <bean id="book" class="cn.xdl.bean.Book"> <!-- 对于基本数据类型 与 String的注入操作 通过set方法 ,完成注入 name: 属性的名称 value: 属性要赋的

IoC 依赖注入容器 Unity

原文:IoC 依赖注入容器 Unity IoC 是什么? 在软件工程领域,“控制反转(Inversion of Control,缩写为IoC)”是一种编程技术,表述在面向对象编程中,可描述为在编译时静态分析器并不知道具体被耦合的对象,而该对象是在运行时被对象装配器绑定的. 在传统编程中,决定业务流程的对象是被静态分配的.而在 IoC 中,业务流程取决于对象装配器实例化提供的对象,这使利用抽象来定义对象间的交互成为可能.对象装配器为了能绑定一个对象,要求该对象必须具备兼容的抽象定义.例如类 Cla

Spring详解(三)------DI依赖注入

上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可能依赖于其他的对象,即类的属性如何赋值?这也是我们这篇博客讲解 Spring 另一个核心要点:DI依赖注入. PS:本篇博客源码下载链接:http://pan.baidu.com/s/1c2xVUDi密码:v1h3 1.什么是DI依赖注入? spring动态的向某个对象提供它所需要的其他对象.这一点

初识Spring框架实现IOC和DI(依赖注入)

学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制.如何理解好Ioc呢?理解好Ioc的关键是要明确“谁控制谁,控制什么,为何是反转(有反转就应该有正转

.netCore2.0 程序集DI依赖注入

传统的依赖注入确实简单,但是随着项目的扩展随之而来的问题又来了,因为传统的注入是单个类和接口注入的,加入项目的接口和类增加到了上百个的话,就需要在Startup.cs中复制注入上百次,虽然能解决问题,但是显然有点笨拙. 下面介绍一个程序集DI依赖注入,即通过反射进行文件注入 首先通过反射获取当前程序集 /// <summary> /// 通过程序集的名称加载程序集 /// </summary> /// <param name="assemblyName"&