Mini 容器学习笔记4——组件的生命周期(应用篇)
生活随笔
收集整理的這篇文章主要介紹了
Mini 容器学习笔记4——组件的生命周期(应用篇)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mini容器支持6中生命周期類型:
???? 1. Singleton :單利類型(缺省組件都是單利類型的生命周期,由容器進行托管的)
[Test]public void SingletonLifestyleTest(){ServiceRegistry.Register<Person>();var person = ServiceLocator.Get<IPerson>();Assert.IsTrue(person != null);var person2 = ServiceLocator.Get<IPerson>();Assert.IsTrue(person2 != null);Assert.AreSame(person, person2);Assert.IsTrue(Person.HasVisited);} ? 2. Transient:臨時,每次請求組件實例都返回一個新的實例,該實例不會被容器托管 [Test]public void TransientLifestyleTest(){ServiceRegistry.Current.Register<IPerson, Person>("person", LifestyleFlags.Transient);var person = ServiceLocator.Get<IPerson>();Assert.IsTrue(person != null);var person2 = ServiceLocator.Get<IPerson>();Assert.IsTrue(person2 != null);Assert.AreNotSame(person, person2);Assert.IsTrue(Person.HasVisited);} ??? 3. Thread:在同一個線程內是單利的 [Test]public void ThreadLifestyleTest(){ServiceRegistry.Current.Register<IPerson, Person>("person", LifestyleFlags.Thread);IPerson person = null,person2 = null, person3 = null, person4 = null;PopulatePerThreadLifestyle(ref person, ref person2);var mre = new ResetEvent(false);ThreadPool.QueueUserWorkItem((s) =>{PopulatePerThreadLifestyle(ref person3, ref person4);mre.Set();});mre.Wait();Assert.AreSame(person, person2);Assert.AreSame(person3, person4);Assert.AreNotSame(person, person3);}private void PopulatePerThreadLifestyle(ref IPerson person, ref IPerson person2){person = ServiceLocator.Get(typeof(IPerson)) as IPerson;Assert.IsTrue(person != null);person2 = ServiceLocator.Get(typeof(IPerson)) as Person;Assert.IsTrue(person2 != null);} 下面三種類型是在上面的三種類型之上擴展來的 4. GenericSingleton: [Test]public void GenericSingletonLifestyleTest(){ServiceRegistry.Current.Register(typeof(IList<>), typeof(List<>));var instance = ServiceLocator.Get<IList<int>>();Assert.IsNotNull(instance);var instance2 = ServiceLocator.Get<IList<int>>();Assert.IsNotNull(instance2);Assert.AreSame(instance, instance2);} ??????? 5. GenericTransient: [Test]public void GenericTransientLifestyleTest(){ServiceRegistry.Current.Register(new ComponentInfo(null,typeof(IList<>),typeof(List<>), LifestyleFlags.Transient));var instance = ServiceLocator.Get<IList<int>>();Assert.IsNotNull(instance);var instance2 = ServiceLocator.Get<IList<int>>();Assert.IsNotNull(instance2);Assert.AreNotSame(instance, instance2);} ????????? 6. GenericThread: [Test]public void GenericThreadLifestyleTest(){ServiceRegistry.Current.Register(new ComponentInfo(null, typeof(IList<>), typeof(List<>), LifestyleFlags.Thread));IList<int> coll = null,coll2 = null, coll3 = null, coll4 = null;PopulatePerThreadLifestyle(ref coll, ref coll2);var mre = new ResetEvent(false);ThreadPool.QueueUserWorkItem((s) =>{PopulatePerThreadLifestyle(ref coll3, ref coll4);mre.Set();});mre.Wait();Assert.AreSame(coll, coll2);Assert.AreSame(coll3, coll4);Assert.AreNotSame(coll, coll3);}private void PopulatePerThreadLifestyle(ref IList<int> first, ref IList<int> second){first = ServiceLocator.Get(typeof(IList<int>)) as IList<int>;Assert.IsTrue(first != null);second = ServiceLocator.Get(typeof(IList<int>)) as IList<int>;Assert.IsTrue(second != null);} ?Mini 容器官方網站:
?? ? ??http://nlite.codeplex.com/
推薦資源:
Mini容器介紹
Mini容器學習目錄
Mini容器學習目錄1——環境搭建(基礎篇)
Mini 容器學習筆記2——組件元數據(基礎篇)
Mini 容器學習筆記3——組件的注冊(基礎篇)
Mini 容器學習筆記4——組件的生命周期(應用篇)
Mini 容器學習筆記5——組件的獲取
Mini 容器學習筆記6——組件的獲取(應用)
Mini 容器學習筆記7——構造函數注入
Mini 容器學習筆記8——字段注入
Mini 容器學習筆記9——屬性注入
Mini 容器學習筆記10——方法注入
Mini 容器學習筆記11——Lazy注入
Mini 容器學習筆記12——組合實例
Mini 容器學習筆記13——插件注入
Mini 容器學習筆記14——異常處理
Mini 容器學習筆記15——監聽器-初始化監聽器
Mini 容器學習筆記16——監聽器-釋放監聽器
Mini 容器學習筆記17——監聽器-啟動/停止監聽器
Mini 容器學習筆記18——監聽器-AOP監聽器
轉載于:https://www.cnblogs.com/netcasewqs/archive/2010/07/06/1772085.html
總結
以上是生活随笔為你收集整理的Mini 容器学习笔记4——组件的生命周期(应用篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 商务智能之绩效管理 Performanc
- 下一篇: jQuery对象与dom对象不能划等号