DI unity dependency of the injection MVC version 2.0 and version comparison Microsoft.Practices.Unity1.2

DI unity dependency of the injection MVC version 2.0 and version comparison Microsoft.Practices.Unity1.2

Reference: https://www.cnblogs.com/xishuai/p/3670292.html

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

Privacy Policy:

In unity1.2 we use constructor injection, injection properties and methods of implantation will parameterType node, that these types can be configured in a manner dependent implantation constructor, property and method of these nodes, but does not exist in unity2.0 parameterType node, and all types of registration are configured via register node, the equivalent type of node unity1.2, although unity2.0 exist constructor, property and method node, but I feel that it is carried out for the constructors, properties, and methods itself injection. In addition the life cycle configuration management in unity2.0 alias node configuration is not needed.

A download and install:

Microsoft.Practices.Unity.dll

Microsoft.Practices.Unity.Configuration.dll

download link:

Second configuration:

1. Create UnityConfig.cs file in the folder App_Start

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. Create ZLPDependencyResolver.cs in App_Start file folder (create a class that implements the 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. Add a line of code in the method of Global file 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);
        }
    }
}

 

 

Three profiles:

Version: 1.2

1.2 in the nodes:

  <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

Guess you like

Origin www.cnblogs.com/zlp520/p/12020153.html