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监听器

posted @ 2010-07-06 13:54  风云  阅读(816)  评论(1编辑  收藏  举报