Mini 容器学习笔记4——组件的生命周期(应用篇)
2010-07-06 13:54
543 查看
Mini容器支持6中生命周期类型:
1.Singleton:单利类型(缺省组件都是单利类型的生命周期,由容器进行托管的)
3.Thread:在同一个线程内是单利的
5.GenericTransient:
6.GenericThread:
Mini容器官方网站:
http://nlite.codeplex.com/
推荐资源:
1.Singleton:单利类型(缺省组件都是单利类型的生命周期,由容器进行托管的)
[Test] publicvoidSingletonLifestyleTest() { ServiceRegistry.Register<Person>(); varperson=ServiceLocator.Get<IPerson>(); Assert.IsTrue(person!=null); varperson2=ServiceLocator.Get<IPerson>(); Assert.IsTrue(person2!=null); Assert.AreSame(person,person2); Assert.IsTrue(Person.HasVisited); }
2.Transient:临时,每次请求组件实例都返回一个新的实例,该实例不会被容器托管
[Test]
publicvoidTransientLifestyleTest()
{
ServiceRegistry.Current.Register<IPerson,Person>("person",LifestyleFlags.Transient);
varperson=ServiceLocator.Get<IPerson>();
Assert.IsTrue(person!=null);
varperson2=ServiceLocator.Get<IPerson>();
Assert.IsTrue(person2!=null);
Assert.AreNotSame(person,person2);
Assert.IsTrue(Person.HasVisited);
}
3.Thread:在同一个线程内是单利的
[Test]
publicvoidThreadLifestyleTest()
{
ServiceRegistry.Current.Register<IPerson,Person>("person",LifestyleFlags.Thread);
IPersonperson=null,
person2=null,person3=null,person4=null;
PopulatePerThreadLifestyle(refperson,refperson2);
varmre=newResetEvent(false);
ThreadPool.QueueUserWorkItem((s)=>
{
PopulatePerThreadLifestyle(refperson3,refperson4);
mre.Set();
});
mre.Wait();
Assert.AreSame(person,person2);
Assert.AreSame(person3,person4);
Assert.AreNotSame(person,person3);
}
privatevoidPopulatePerThreadLifestyle(refIPersonperson,refIPersonperson2)
{
person=ServiceLocator.Get(typeof(IPerson))asIPerson;
Assert.IsTrue(person!=null);
person2=ServiceLocator.Get(typeof(IPerson))asPerson;
Assert.IsTrue(person2!=null);
}
下面三种类型是在上面的三种类型之上扩展来的
4.GenericSingleton:
[Test]
publicvoidGenericSingletonLifestyleTest()
{
ServiceRegistry.Current.Register(typeof(IList<>),typeof(List<>));
varinstance=ServiceLocator.Get<IList<int>>();
Assert.IsNotNull(instance);
varinstance2=ServiceLocator.Get<IList<int>>();
Assert.IsNotNull(instance2);
Assert.AreSame(instance,instance2);
}
5.GenericTransient:
[Test]
publicvoidGenericTransientLifestyleTest()
{
ServiceRegistry.Current.Register(newComponentInfo(null,typeof(IList<>),typeof(List<>),LifestyleFlags.Transient));
varinstance=ServiceLocator.Get<IList<int>>();
Assert.IsNotNull(instance);
varinstance2=ServiceLocator.Get<IList<int>>();
Assert.IsNotNull(instance2);
Assert.AreNotSame(instance,instance2);
}
6.GenericThread:
[Test]
publicvoidGenericThreadLifestyleTest()
{
ServiceRegistry.Current.Register(newComponentInfo(null,typeof(IList<>),typeof(List<>),LifestyleFlags.Thread));
IList<int>coll=null,
coll2=null,coll3=null,coll4=null;
PopulatePerThreadLifestyle(refcoll,refcoll2);
varmre=newResetEvent(false);
ThreadPool.QueueUserWorkItem((s)=>
{
PopulatePerThreadLifestyle(refcoll3,refcoll4);
mre.Set();
});
mre.Wait();
Assert.AreSame(coll,coll2);
Assert.AreSame(coll3,coll4);
Assert.AreNotSame(coll,coll3);
}
privatevoidPopulatePerThreadLifestyle(refIList<int>first,refIList<int>second)
{
first=ServiceLocator.Get(typeof(IList<int>))asIList<int>;
Assert.IsTrue(first!=null);
second=ServiceLocator.Get(typeof(IList<int>))asIList<int>;
Assert.IsTrue(second!=null);
}
Mini容器官方网站:
推荐资源:
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容器学习笔记14——异常处理
Mini容器学习笔记15——监听器-初始化监听器
Mini容器学习笔记16——监听器-释放监听器
Mini容器学习笔记17——监听器-启动/停止监听器
Mini容器学习笔记18——监听器-AOP监听器
相关文章推荐
- Mini 容器学习笔记5—— 组件的获取
- Mini 容器学习笔记6——组件的获取(应用)
- Mini 容器学习笔记2——组件元数据(基础篇)
- Mini 容器学习笔记3——组件的注册(基础篇)
- Java学习笔记-----中间件,组件,容器,框架的精彩理解
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- 学习React Native 笔记(三)React组件的生命周期
- Mini 容器学习笔记10——方法注入
- Sencha Touch 2 学习笔记(三)----Form表单容器及其子组件
- Mini 容器学习笔记1——环境搭建(基础篇)
- Mini 容器学习笔记1——环境搭建(基础篇)
- Flex学习笔记3——组件和容器
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- Mini 容器学习笔记7——构造函数注入
- react学习笔记之组件生命周期
- angular4.x学习笔记 --组件以及生命周期
- Tomcat6源代码学习--容器组件通讯机制与生命周期管理
- Mini 容器学习笔记12——组合实例
- Mini 容器学习笔记11——Lazy注入
- Mini 容器学习笔记8——字段注入