Unity依赖注入(构造器注入、属性注入、方法注入)

前端之家收集整理的这篇文章主要介绍了Unity依赖注入(构造器注入、属性注入、方法注入)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

框架 .NET Framework 4.5

dll文件:Microsoft.Practices.Unity.dll

Microsoft.Practices.Unity.Configuration.dll

dll下载地址:https://download.csdn.net/download/tiz198183/10406858

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //创建容器
            UnityContainer container = new UnityContainer();
            UnityConfigurationSection configuration = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
            configuration.Configure(container,"defaultContainer");
            //通过Resolve<IA>方法返回的是一个类型为A的对象,该对象的三个属性被进行了有效的初始化。
            //这个简单的程序分别体现了接口注入(通过相应的接口根据配置解析出相应的实现类型)、构造器注入(属性B)、属性注入(属性C)和方法注入(属性D)
            A a = container.Resolve<IA>() as A;
            if (null != a)
            {
                Console.WriteLine("a.B==null?{0}",a.B == null ? "Yes" : "No");
                Console.WriteLine("a.C==null?{0}",a.C == null ? "Yes" : "No");
                Console.WriteLine("a.D==null?{0}",a.D == null ? "Yes" : "No");
            }  

        }
    }

    public interface IA { }
    public interface IB { }
    public interface IC { }
    public interface ID { }

    public class A : IA
    {
        //1、构造器注入
        public IB B { get; set; }
        //2、属性注入
        //属性C上应用了DependencyAttribute特性,
        //意味着这是一个需要以属性注入方式被初始化的依赖属性
        [Dependency]
        public IC C { get; set; }
        //3、方法注入
        //属性D则通过方法Initialize初始化,该方法上应用了特性InjectionMethodAttribute,
        //意味着这是一个注入方法在A对象被IoC容器创建的时候会被自动调用
        public ID D { get; set; }

        public A(IB b)
        {
            this.B = b;
        }
        [InjectionMethod]
        public void Initalize(ID d)
        {
            this.D = d;
        }
    }
    public class B : IB { }
    public class C : IC { }
    public class D : ID { }  
}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--要保证configSections为configuration第一个节点-->
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <containers>
      <!--定义了一个名称为defaultContainer的Unity容器-->
      <container name="defaultContainer">
        <register type="WindowsFormsApplication3.IA,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.A,WindowsFormsApplication3"/>
        <register type="WindowsFormsApplication3.IB,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.B,WindowsFormsApplication3"/>
        <register type="WindowsFormsApplication3.IC,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.C,WindowsFormsApplication3"/>
        <register type="WindowsFormsApplication3.ID,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.D,WindowsFormsApplication3"/>
      </container>
    </containers>
  </unity>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
</configuration>

结果:

a.B == null ? No
 a.C == null ? No
 a.D == null ? No


文章来源:https://www.cnblogs.com/zhangchenliang/archive/2013/01/08/2850970.html

猜你在找的设计模式相关文章