這是我的第三篇討論IOC工廠的文章了,貌似我已經(jīng)跟IOC工廠杠上了。
前兩篇是說的造輪子的過程。貌似沒什么人感興趣,那這次就直接發(fā)布輪子吧。
上圖是 我的容器與 微軟的企業(yè)庫 unity的性能比較。
可以看見。效率是微軟企業(yè)庫的6倍。
使用依賴注入模式 效率也比微軟企業(yè)庫 要快。
目前支持以下幾種模式對(duì)象的創(chuàng)建
普通模式,
單例模式,
依賴注入模式,
裝飾模式,
以及單例注入模式
5種模式的獲取方式類似都是采用
Factory.Get<IType>([key])的方式進(jìn)行獲取。
但是注冊(cè)有所不同。
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); } } }
類庫已經(jīng)上傳到附件。有興趣的朋友可以下載
點(diǎn)擊 訪問github獲取本項(xiàng)目的最新代碼
附件:http://down.51cto.com/data/2363596另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章標(biāo)題:輕量級(jí)IOC容器IOCFactory發(fā)布。-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://chinadenli.net/article6/dgjoog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、做網(wǎng)站、搜索引擎優(yōu)化、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容