您的位置:首页 > 其它

Mini 容器学习笔记4——组件的生命周期(应用篇)

2010-07-06 13:54 543 查看
Mini容器支持6中生命周期类型:

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容器官方网站:

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: