unity 三种注入示例

2018-06-17 20:00:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  1 /*
  2  * 演示Unity 注入
  3  * */
  4 using Microsoft.Practices.Unity;
  5 using System;
  6 
  7 namespace Unity.Property.Inject
  8 {
  9     class Program
 10     {
 11         public static IUnityContainer container;
 12 
 13         static void Main(string[] args)
 14         {
 15             container = new UnityContainer();
 16 
 17             //PropertyInject();
 18             //ConstructorInject();
 19             MethodInject();
 20 
 21             Console.ReadLine();
 22         }
 23 
 24         /// <summary>
 25         /// 构造注入
 26         /// </summary>
 27         static void ConstructorInject()
 28         {
 29             Console.WriteLine("构造注入");
 30             container.RegisterType<ILog, ALog>();
 31             //container.RegisterType<ILog, BLog>();
 32 
 33             container.RegisterType<IPeople, Man>();
 34             //container.RegisterType<IPeople, Woman>();
 35 
 36             var people = container.Resolve<IPeople>();
 37             people.Print2();
 38         }
 39 
 40         /// <summary>
 41         /// 属性注入
 42         /// </summary>
 43         static void PropertyInject()
 44         {
 45             Console.WriteLine("属性注入");
 46             //通过Register不同的ILog的派生类型,达到用不同的日志组件进行打印
 47 
 48             //container.RegisterType<ILog, ALog>();
 49             container.RegisterType<ILog, BLog>();
 50 
 51             container.RegisterType<IPeople, Man>();
 52             //container.RegisterType<IPeople, Woman>();
 53 
 54             var people = container.Resolve<IPeople>();
 55             people.Print();
 56         }
 57 
 58         static void MethodInject()
 59         {
 60             Console.WriteLine("函数注入");
 61             //通过Register不同的ILog的派生类型,达到用不同的日志组件进行打印
 62 
 63             //container.RegisterType<ILog, ALog>();
 64             container.RegisterType<ILog, BLog>();
 65 
 66             container.RegisterType<IPeople, Man>();
 67             //container.RegisterType<IPeople, Woman>();
 68 
 69             var people = container.Resolve<IPeople>();
 70             people.Logger.Write("函数注入 " + people.Logger != null ? "成功" : "失败");
 71         }
 72     }
 73 
 74     public interface IPeople
 75     {
 76         /// <summary>
 77         /// 属性注入用
 78         /// </summary>
 79         ILog Logger { get; set; }
 80 
 81         /// <summary>
 82         /// 构造注入用
 83         /// </summary>
 84         ILog Logger2 { get; set; }
 85 
 86         String Name { get; set; }
 87 
 88         String Sex { get; set; }
 89 
 90         /// <summary>
 91         /// 属性注入用
 92         /// </summary>
 93         void Print();
 94 
 95         /// <summary>
 96         /// 构造注入用
 97         /// </summary>
 98         void Print2();
 99 
100         /// <summary>
101         /// 函数注入用
102         /// </summary>
103         void Print3(ILog logger);
104     }
105 
106     public class Man : IPeople
107     {
108         [Dependency]
109         public ILog Logger { get; set; }
110         public ILog Logger2 { get; set; }
111 
112         public string Name { get; set; }
113 
114         public string Sex { get; set; }
115 
116         public Man(ILog logger2) { this.Logger2 = logger2; }
117 
118         public void Print()
119         {
120             Logger.Write(base.ToString());
121         }
122 
123         public void Print2()
124         {
125             Logger.Write(base.ToString());
126         }
127 
128         [InjectionMethod]
129         public void Print3(ILog logger)
130         {
131             this.Logger = logger;
132         }
133     }
134 
135     public class Woman : IPeople
136     {
137 
138         [Dependency]
139         public ILog Logger { get; set; }
140         public ILog Logger2 { get; set; }
141 
142         public string Name { get; set; }
143 
144         public string Sex { get; set; }
145 
146         public Woman(ILog logger2) { this.Logger2 = logger2; }
147 
148         public void Print()
149         {
150             Logger.Write(base.ToString());
151         }
152 
153         public void Print2()
154         {
155             Logger.Write(base.ToString());
156         }
157 
158         [InjectionMethod]
159         public void Print3(ILog logger)
160         {
161             this.Logger = logger;
162         }
163     }
164 
165 
166     public interface ILog
167     {
168         void Write(String msg);
169     }
170 
171     public class ALog : ILog
172     {
173         public void Write(String msg) { Console.WriteLine("[A] {0}", msg); }
174     }
175 
176     public class BLog : ILog
177     {
178         public void Write(String msg) { Console.WriteLine("[B] {0}", msg); }
179     }
180 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:理解C#系列 / 核心C# / 数据类型

下一篇:Task的一些用法总结