這是個人第三篇討論IOC工廠的文章了,貌似我已經跟IOC工廠槓上了。git
前兩篇是說的造輪子的過程。貌似沒什麼人感興趣,那此次就直接發佈輪子吧。github
上圖是 個人容器與 微軟的企業庫 unity的性能比較。c#
能夠看見。效率是微軟企業庫的6倍。ide
使用依賴注入模式 效率也比微軟企業庫 要快。性能
目前支持如下幾種模式對象的建立this
普通模式,spa
單例模式,orm
依賴注入模式,對象
裝飾模式,blog
以及單例注入模式
5種模式的獲取方式相似都是採用
Factory.Get<IType>([key])的方式進行獲取。
可是註冊有所不一樣。
factory.Regist<T, Q>(IOCFactoryModel.InstType.Normal);//普通模式 factory.Regist<T, Q>(InstType.DI);//依賴注入模式 factory.Regist<T, Q>(InstType.Singleton);//單例模式 factory.RegistDecorate<T, Q>("catDog", "cat");//裝飾者模式 factory.Regist<T, Q>(InstType.DISingleton)//單例注入模式
下面是使用的示例代碼
namespace IOCFactoryUnitTest { public interface Animal { string Hawl(); } public interface Toy { string Play(); } public class Ball : Toy { public string Play() { return "roll and roll"; } } public class Dog : Animal { public string Hawl() { var returnValue = "wang wang wang"; return returnValue; } } public class Cat : Animal { public string Hawl() { var returnValue = "miao miao miao"; return returnValue; } } public class CatDog : Animal { private Animal toDecorate; public CatDog(Animal toDecorate) { this.toDecorate = toDecorate; } public string Hawl() { var returnValue = toDecorate.Hawl(); returnValue += " wang wang wang"; return returnValue; } } public class MachineCatDog : Animal { private Animal toDecorate; public MachineCatDog(Animal toDecorate) { this.toDecorate = toDecorate; } public string Hawl() { var returnValue = toDecorate.Hawl(); returnValue += " hmm hmm hmm"; return returnValue; } } public class SingleTonTest : Animal { private SingleTonTest() { } public string Hawl() { return this.GetHashCode().ToString(); } } public class DITest { Animal animal; Toy toy; public DITest(Animal animal, Toy toy) { this.animal = animal; this.toy = toy; } public string Test() { return animal.Hawl() + this.toy.Play(); } } [TestClass] public class IOCFactoryTest { [ClassInitialize()] public static void Init(TestContext context) { Factory factory = Factory.GetInst(); factory.Regist<Animal, Cat>("cat", InstType.Normal); factory.Regist<Animal, Dog>("dog", InstType.Normal); factory.Regist<Animal, SingleTonTest>(InstType.Singleton); factory.RegistDecorate<Animal, CatDog>("catDog", "cat"); factory.RegistDecorate<Animal, MachineCatDog>("catDog", "cat"); factory.Regist<Toy, Ball>(InstType.Normal); factory.Regist<DITest, DITest>(InstType.DISingleton); } [TestCleanup] public void CleanUp() { } [TestMethod] public void NormalInstTest() { Factory factory = Factory.GetInst(); var result = new Dog(); var dog = factory.Get<Animal>("dog"); Assert.AreEqual(dog.Hawl(), result.Hawl()); var cat = factory.Get<Animal>("cat"); Assert.AreNotEqual(cat.Hawl(), result.Hawl()); } [TestMethod] public void SingleInstTest() { Factory factory = Factory.GetInst(); var obj1 = factory.Get<Animal>(); var obj2 = factory.Get<Animal>(); Assert.AreEqual(obj1, obj2); } [TestMethod] public void DecorateInstTest() { Factory factory = Factory.GetInst(); var dog = factory.Get<Animal>("dog"); var cat = factory.Get<Animal>("cat"); var catDog = factory.Get<Animal>("catDog"); Assert.AreEqual(cat.Hawl() + " " + dog.Hawl() + " hmm hmm hmm", catDog.Hawl()); } [TestMethod] public void DIInstTest() { Factory factory = Factory.GetInst(); var ani = factory.Get<Animal>(); var toy = factory.Get<Toy>(); var result = factory.Get<DITest>(); var result2 = factory.Get<DITest>(); var exp = new DITest(ani, toy); Assert.AreEqual(exp.Test(), result.Test()); Assert.AreEqual(result, result2); } [TestMethod] public void MultiThreadTest() { try { int count = 100; Action func = () => { for (var i = 0; i < count; i++) { DIInstTest(); } }; var a = new Thread(new ThreadStart(func)); var b = new Thread(new ThreadStart(func)); var c = new Thread(new ThreadStart(func)); a.Start(); b.Start(); c.Start(); } catch (Exception ex) { Assert.Fail(); } Assert.IsTrue(true); } } }
類庫已經上傳到附件。有興趣的朋友能夠下載