惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
A
Arctic Wolf
Scott Helme
Scott Helme
S
Securelist
Schneier on Security
Schneier on Security
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
Security Latest
Security Latest
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
Check Point Blog
F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
H
Hacker News: Front Page
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
Vercel News
Vercel News
A
About on SuperTechFans
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security Affairs
Y
Y Combinator Blog
T
The Exploit Database - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Forbes - Security
Forbes - Security
IT之家
IT之家
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
K
Kaspersky official blog
Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
MyScale Blog
MyScale Blog
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
D
Docker
G
GRAHAM CLULEY

博客园 - Timothy Ye

扩展方法使用小结 IIS7中Host WCF遇到的问题 博客园字体美化小技巧 - Timothy Ye - 博客园 个人闲置物品挥泪处理(8月27日更新) Visual Studio IDE配色方案 HTC Touch Pro 的重力感应功能 C# IDE Mobile – Write your C# code anywhere! Visual Studio 2010 Beta1 is available for download 小黑降温记 Windows 7 新特性体验 换了新鼠标 Unity 学习笔记(2) -- 配置文件的使用 Unity 学习笔记(1) -- Unity简介及简单使用 Microsoft My Phone 体验 SQL中的日期计算 快速开启、屏蔽Vista下的UAC提示 Dynamic Plugins Manager (五) Plugins Manager 源码下载 Dynamic Plugins Manager (四) 插件及Demo源码下载 Dynamic Plugins Manager (三) Demo
Unity 学习笔记(3) -- 生命周期管理
Timothy Ye · 2009-04-28 · via 博客园 - Timothy Ye

2009-04-28 23:34  Timothy Ye  阅读(774)  评论()    收藏  举报

Unity根据我们在注册类型的时候所指定的生命周期来管理注册类型的创建和解析。当我们在向容器中注册类型的时候,如果我们没有显式的指明该类型的生命周期管理器(下称:Lifetime Manager),默认的情况下,容器会为我们注册的类型创建一个生命周期短暂的管理器。这样一来,当我们每次调用容器的Resolve方法或者ResolveAll方法,或者采用依赖机制注入实例到其他的类中的时候,容器都会为我们创建新的实例,并且容器不会保存对实例的引用。

容器的RegisterType函数包含多个支持泛型的重载,同时还包含了一一对应的非泛型重载。其中的重载函数,我们可以看到类似下面的声明:

 1: RegisterType<TFrom, TTo>(LifetimeManager lifetime)

参数中的LifetimeManager类型,就是用来控制注册类型生命周期的。Unity中提供了几个继承自LifetimeManager的类作为注册类型的生命周期管理器。在目前的Unity 1.2版本中,共提供了3个LifetimeManager供我们直接在代码中调用。此外,我们也可以实现自己的LifetimeManager,不过必须实现LifetimeManager所必须的一些方法。

1.ContainerControlledLifetimeManager:

   容器负责管理注册类型的生命周期,并且注册类型的生命周期和容器一致。当离开容器的作用域范围时,容器会被销毁,同时其中的注册类型也会被销毁。或者,当容器被显式销毁时,其中的注册类型也同样被销毁。但是在容器有效的作用域范围中,当我们使用Resolve方法,或者使用ResolveAll方法获取我们的注册类型的时候,容器会在第一次调用时,创建实例,但是在后面的调用中,我们始终只会得到相同的实例。我们可以利用这种Manager来实现Singleton模式。

2.ExternallyControlledLifetimeManager:

和上一种方式类似,不过容器在不会保留对注册类型的强引用,而只是保留弱引用。也就是说,如果这个注册类型没有在其他地方被强引用的时候,那么这个注册类型可能会被GC给干掉。

3.PerThreadLifetimeManager:

容器保证在每个线程中返回同一个对象实例,那么在不同的线程中,得到的实例是不同的。

下面我们可以试验一下不同的LifetimeManager的效果

a. 默认的情况,不显示的指明LifetimeManager

代码:

 1: namespace UnityDemo
 2: {
 3:  class Program
 4:  {
 5:  static void Main(string[] args)
 6:  {
 7:  IUnityContainer container = new UnityContainer();
 8:  container.RegisterType<INotify, EmailNotify>();
 9:  
 10:  INotify notify1 = container.Resolve<INotify>();
 11:  
 12:  INotify notify2 = container.Resolve<INotify>();
 13:  
 14:  Console.WriteLine("notify1:" + notify1.GetHashCode());
 15:  Console.WriteLine("notify2:" + notify2.GetHashCode());
 16:  
 17:  Console.ReadLine();
 18:  }
 19:  }
 20: }

运行结果:

4

从结果我们可以看到,默认情况下不指定LifetimeManager的时候,每次我们获得的Instance其实不是同一个Instance.

b.使用ContainerControlledLifetimeManager

代码:

 1: namespace UnityDemo
 2: {
 3:  class Program
 4:  {
 5:  static void Main(string[] args)
 6:  {
 7:  IUnityContainer container = new UnityContainer();
 8:  container.RegisterType<INotify, EmailNotify>(new ContainerControlledLifetimeManager());
 9:  
 10:  INotify notify1 = container.Resolve<INotify>();
 11:  
 12:  INotify notify2 = container.Resolve<INotify>();
 13:  
 14:  Console.WriteLine("notify1:" + notify1.GetHashCode());
 15:  Console.WriteLine("notify2:" + notify2.GetHashCode());
 16:  
 17:  Console.ReadLine();
 18:  }
 19:  }
 20: }

运行结果:

5
使用ContainerControlledLifetimeManager,在Container生命中期内,我们得到的Instance是同一个。

c.使用PerThreadLifetimeManager

代码:

 1: namespace UnityDemo
 2: {
 3:  class Program
 4:  {
 5:  static void Main(string[] args)
 6:  {
 7:  IUnityContainer container = new UnityContainer();
 8:  container.RegisterType<INotify, EmailNotify>(new PerThreadLifetimeManager());
 9:  
 10:  Thread thread1 = new Thread(new ParameterizedThreadStart(ThreadProc1));
 11:  Thread thread2 = new Thread(new ParameterizedThreadStart(ThreadProc2));
 12:  
 13:  thread1.Start(container);
 14:  thread2.Start(container);
 15:  
 16:  Console.ReadLine();
 17:  }
 18:  
 19:  static void ThreadProc1(object obj)
 20:  {
 21:  INotify notify1 = (obj as IUnityContainer).Resolve<INotify>();
 22:  INotify notify2 = (obj as IUnityContainer).Resolve<INotify>();
 23:  
 24:  Console.WriteLine("ThreadProc1 --- notify1:" + notify1.GetHashCode());
 25:  Console.WriteLine("ThreadProc1 --- notify2:" + notify2.GetHashCode());
 26:  
 27:  }
 28:  
 29:  static void ThreadProc2(object obj)
 30:  {
 31:  INotify notify1 = (obj as IUnityContainer).Resolve<INotify>();
 32:  INotify notify2 = (obj as IUnityContainer).Resolve<INotify>();
 33:  
 34:  Console.WriteLine("ThreadProc2 --- notify1:" + notify1.GetHashCode());
 35:  Console.WriteLine("ThreadProc2 --- notify2:" + notify2.GetHashCode());
 36:  }
 37:  }
 38: }
运行结果:
6
 
 在结果中可以看到,同一个线程中连续两次获取到的是同一个Instance,而不同的线程中获取到的Instance不一样。