Unity是微軟在CodePlex上的一個開源項目,可用於依賴注入、控制反轉,相似Spring,下面是使用示例:ide
1.先來定義幾個接口、類測試
1 namespace UnityTest 2 { 3 public interface IRun 4 { 5 void Run(); 6 } 7 }
1 namespace UnityTest 2 { 3 public class Pet 4 { 5 public string Name { set; get; } 6 } 7 }
1 using System; 2 3 namespace UnityTest 4 { 5 public class Cat : Pet, IRun 6 { 7 public void Run() 8 { 9 Console.WriteLine("A cat is runing..."); 10 } 11 } 12 }
1 using System; 2 3 namespace UnityTest 4 { 5 public class Dog : Pet, IRun 6 { 7 public void Run() 8 { 9 Console.WriteLine("A dog is runing..."); 10 } 11 } 12 }
1 namespace UnityTest.Model 2 { 3 public class Person 4 { 5 IRun iRun; 6 7 public Person(IRun iRun) 8 { 9 this.iRun = iRun; 10 } 11 12 public void HiWeGo() 13 { 14 iRun.Run(); 15 } 16 } 17 }
二、建立對象實例this
1 using System; 2 using System.Web.Script.Serialization; 3 using Microsoft.Practices.Unity; 4 5 namespace UnityTest 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 var container = new UnityContainer(); 12 container.RegisterType<IRun, Dog>(); //註冊類型,並將IRun映射到Dog 13 var dog1 = container.Resolve<IRun>(); //建立一個IRun實例,實際上就是Dog 14 (dog1 as Dog).Name = "buddy"; 15 Console.WriteLine("a dog is born,his name is \"{0}\"", (dog1 as Dog).Name); //a dog is born,his name is "buddy" 16 Console.Read(); 17 } 18 } 19 }
三、使用標識符spa
當IRun同時有多個實例類要注入時,若是沒有標識來區別,建立出來的實例「類型」就沒辦法顯示指定。3d
1 var container = new UnityContainer(); 2 container.RegisterType<IRun, Dog>() 3 .RegisterType<IRun, Cat>(); 4 var run = container.Resolve<IRun>();//這裏的run實例,是cat,仍是dog ? 5 Console.Write(run.ToString());//UnityTest.Cat
1 var container = new UnityContainer(); 2 container.RegisterType<IRun, Dog>("dogType") 3 .RegisterType<IRun, Cat>("catType"); 4 var run = container.Resolve<IRun>("dogType");//明確指示,我要一個dog 5 Console.Write(run.ToString());//UnityTest.Dog
四、建立單例code
1 var container = new UnityContainer(); 2 container.RegisterType<IRun, Dog>("dogTypeSingle", new ContainerControlledLifetimeManager())//dogTypeSingle爲單例模式 3 .RegisterType<IRun, Dog>("dogType"); 4 5 var dog1 = container.Resolve<IRun>("dogTypeSingle"); 6 var dog2 = container.Resolve<IRun>("dogTypeSingle"); 7 8 Console.WriteLine(object.ReferenceEquals(dog1, dog2));//True 9 Console.WriteLine(dog1.GetHashCode() == dog2.GetHashCode());//True 說明dog1與dog2是同一個對象 10 11 var dog3 = container.Resolve<IRun>("dogType"); 12 var dog4 = container.Resolve<IRun>("dogType"); 13 Console.WriteLine(object.ReferenceEquals(dog3, dog4));//False 14 Console.WriteLine(dog3.GetHashCode() == dog4.GetHashCode());//False 說明dog3與dog4是不一樣的對象
五、依賴注入對象
構造器自動注入blog
1 var container = new UnityContainer(); 2 container.RegisterType<IRun, Dog>(); 3 var personWithDog = container.Resolve<Person>(); 4 personWithDog.HiWeGo();//A dog is runing...
構造器也能夠結合標識符顯示注入接口
1 var container = new UnityContainer(); 2 container.RegisterType<IRun, Dog>("dog") 3 .RegisterType<IRun, Cat>("cat") 4 .RegisterType<Person, Person>("PersonWithDog", new InjectionConstructor(container.Resolve<IRun>("dog"))) 5 .RegisterType<Person, Person>("PersonWithCat", new InjectionConstructor(container.Resolve<IRun>("cat"))); 6 7 var personWithDog = container.Resolve<Person>("PersonWithDog"); 8 personWithDog.HiWeGo();//A dog is runing... 9 var personWithCat = container.Resolve<Person>("PersonWithCat"); 10 personWithCat.HiWeGo();//A cat is runing... 11 Console.Read();
使用InjectionConstructor特性注入ip
爲了演示[InjectionConstructor]特性,先改造一下Person類
1 using System.Collections.Generic; 2 using Microsoft.Practices.Unity; 3 namespace UnityTest 4 { 5 public class Person 6 { 7 private List<IRun> pets; 8 9 public Person() 10 { 11 pets = new List<IRun>(); 12 } 13 14 [InjectionConstructor] 15 public Person(IRun iRun):this() 16 { 17 pets.Add(iRun); 18 } 19 20 public Person(IRun iRun1,IRun iRun2):this() 21 { 22 pets.Add(iRun1); 23 pets.Add(iRun2); 24 } 25 26 27 public void HiWeGo() 28 { 29 foreach (var pet in pets) 30 { 31 pet.Run(); 32 } 33 } 34 } 35 }
測試一下:
1 var container = new UnityContainer(); 2 3 container.RegisterType<IRun, Dog>("dog") //註冊一個帶標識的dog類型 4 .RegisterType<IRun, Cat>("cat") //註冊一個帶標識的cat類型 5 .RegisterType<IRun, Dog>() //不指定標識,即默認IRun的實例爲dog 6 .RegisterType<Person, Person>("PersonWithDogAndCat", 7 new InjectionConstructor( 8 container.Resolve<IRun>("dog"), 9 container.Resolve<IRun>("cat") 10 )//顯示將二個參數的構造方法註冊到容器中 11 ); 12 13 14 var person = container.Resolve<Person>();//自動調用有InjectionConstructor標記的構造方法 15 person.HiWeGo();//A dog is runing... 16 17 18 Console.WriteLine("---------------"); 19 20 var personWithDogAndCat = container.Resolve<Person>("PersonWithDogAndCat");//顯式調用在容器中註冊過的構造方法 21 personWithDogAndCat.HiWeGo(); 22 //A dog is runing... 23 //A cat is runing... 24 25 Console.Read();
未完待續...