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

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

框架 .NET Framework 4.5

dll文件:Microsoft.Practices.Unity.dll

Microsoft.Practices.Unity.Configuration.dll

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

代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.Practices.Unity;
  10. using Microsoft.Practices.Unity.Configuration;
  11. using System.Configuration;
  12. namespace WindowsFormsApplication3
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. //创建容器
  21. UnityContainer container = new UnityContainer();
  22. UnityConfigurationSection configuration = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
  23. configuration.Configure(container,"defaultContainer");
  24. //通过Resolve<IA>方法返回的是一个类型为A的对象,该对象的三个属性被进行了有效的初始化。
  25. //这个简单的程序分别体现了接口注入(通过相应的接口根据配置解析出相应的实现类型)、构造器注入(属性B)、属性注入(属性C)和方法注入(属性D)
  26. A a = container.Resolve<IA>() as A;
  27. if (null != a)
  28. {
  29. Console.WriteLine("a.B==null?{0}",a.B == null ? "Yes" : "No");
  30. Console.WriteLine("a.C==null?{0}",a.C == null ? "Yes" : "No");
  31. Console.WriteLine("a.D==null?{0}",a.D == null ? "Yes" : "No");
  32. }
  33.  
  34. }
  35. }
  36.  
  37. public interface IA { }
  38. public interface IB { }
  39. public interface IC { }
  40. public interface ID { }
  41.  
  42. public class A : IA
  43. {
  44. //1、构造器注入
  45. public IB B { get; set; }
  46. //2、属性注入
  47. //属性C上应用了DependencyAttribute特性,
  48. //意味着这是一个需要以属性注入方式被初始化的依赖属性
  49. [Dependency]
  50. public IC C { get; set; }
  51. //3、方法注入
  52. //属性D则通过方法Initialize初始化,该方法上应用了特性InjectionMethodAttribute,
  53. //意味着这是一个注入方法在A对象被IoC容器创建的时候会被自动调用
  54. public ID D { get; set; }
  55.  
  56. public A(IB b)
  57. {
  58. this.B = b;
  59. }
  60. [InjectionMethod]
  61. public void Initalize(ID d)
  62. {
  63. this.D = d;
  64. }
  65. }
  66. public class B : IB { }
  67. public class C : IC { }
  68. public class D : ID { }
  69. }

配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <!--要保证configSections为configuration第一个节点-->
  4. <configSections>
  5. <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  6. </configSections>
  7. <unity>
  8. <containers>
  9. <!--定义了一个名称为defaultContainer的Unity容器-->
  10. <container name="defaultContainer">
  11. <register type="WindowsFormsApplication3.IA,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.A,WindowsFormsApplication3"/>
  12. <register type="WindowsFormsApplication3.IB,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.B,WindowsFormsApplication3"/>
  13. <register type="WindowsFormsApplication3.IC,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.C,WindowsFormsApplication3"/>
  14. <register type="WindowsFormsApplication3.ID,WindowsFormsApplication3" mapTo="WindowsFormsApplication3.D,WindowsFormsApplication3"/>
  15. </container>
  16. </containers>
  17. </unity>
  18. <startup>
  19. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  20. </startup>
  21. </configuration>

结果:

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


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

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