国产一区二区精品-国产一区二区精品久-国产一区二区精品久久-国产一区二区精品久久91-免费毛片播放-免费毛片基地

千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

手機(jī)站
千鋒教育

千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

千鋒教育

掃一掃進(jìn)入千鋒手機(jī)站

領(lǐng)取全套視頻
千鋒教育

關(guān)注千鋒學(xué)習(xí)站小程序
隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

當(dāng)前位置:首頁(yè)  >  技術(shù)干貨  > Unity容器中的對(duì)象生存期管理

Unity容器中的對(duì)象生存期管理

來(lái)源:千鋒教育
發(fā)布人:qyf
時(shí)間: 2022-08-18 17:28:00 1660814880

  IoC 容器的對(duì)象生存期管理

  如果你一直在使用 IoC 容器,你可能已經(jīng)使用過(guò)了一些對(duì)象生存期管理模型(Object Lifetime Management)。通過(guò)對(duì)對(duì)象生存期的管理,將使對(duì)象的復(fù)用成為可能。同時(shí)其使容器可以控制如何創(chuàng)建和管理對(duì)象實(shí)例。

  Unity 提供的對(duì)象生存期管理模型是通過(guò)從抽象類 LifetimeManager 的派生類來(lái)完成。Unity 將為每個(gè)類型的注冊(cè)創(chuàng)建生存期管理器。每當(dāng) UnityContainer 需要?jiǎng)?chuàng)建一個(gè)新的對(duì)象實(shí)例時(shí),將首先檢測(cè)該對(duì)象類型的生存期管理器,是否已有一個(gè)對(duì)象實(shí)例可用。如果沒(méi)有對(duì)象實(shí)例可用,則 UnityContainer 將基于配置的信息構(gòu)造該對(duì)象實(shí)例并將該對(duì)象交予對(duì)象生存期管理器。

  LifetimeManager

  LifetimeManager 是一個(gè)抽象類,其實(shí)現(xiàn)了 ILifetimePolicy 接口。該類被作為所有內(nèi)置或自定義的生存期管理器的父類。它定義了 3 個(gè)方法:

  GetValue - 返回一個(gè)已經(jīng)存儲(chǔ)在生存期管理器中對(duì)象實(shí)例。

  SetValue - 存儲(chǔ)一個(gè)新對(duì)象實(shí)例到生存期管理器中。

  RemoveValue - 從生存期管理器中將已存儲(chǔ)的對(duì)象實(shí)例刪除。UnityContainer 的默認(rèn)實(shí)現(xiàn)將不會(huì)調(diào)用此方法,但可在定制的容器擴(kuò)展中調(diào)用。

  Unity 內(nèi)置了 6 種生存期管理模型,其中有 2 種即負(fù)責(zé)對(duì)象實(shí)例的創(chuàng)建也負(fù)責(zé)對(duì)象實(shí)例的銷毀(Dispose)。

  TransientLifetimeManager - 為每次請(qǐng)求生成新的類型對(duì)象實(shí)例。 (默認(rèn)行為)

  ContainerControlledLifetimeManager - 實(shí)現(xiàn) Singleton 對(duì)象實(shí)例。 當(dāng)容器被 Disposed 后,對(duì)象實(shí)例也被 Disposed。

  HierarchicalifetimeManager - 實(shí)現(xiàn) Singleton 對(duì)象實(shí)例。但子容器并不共享父容器實(shí)例,而是創(chuàng)建針對(duì)字容器的 Singleton 對(duì)象實(shí)例。當(dāng)容器被 Disposed 后,對(duì)象實(shí)例也被 Disposed。

  ExternallyControlledLifetimeManager - 實(shí)現(xiàn) Singleton 對(duì)象實(shí)例,但容器僅持有該對(duì)象的弱引用(WeakReference),所以該對(duì)象的生存期由外部引用控制。

  PerThreadLifetimeManager - 為每個(gè)線程生成 Singleton 的對(duì)象實(shí)例,通過(guò) ThreadStatic 實(shí)現(xiàn)。

  PerResolveLifetimeManager - 實(shí)現(xiàn)與 TransientLifetimeManager 類似的行為,為每次請(qǐng)求生成新的類型對(duì)象實(shí)例。不同之處在于對(duì)象實(shí)例在 BuildUp 過(guò)程中是可被重用的。

  Code Double

  public interface IExample : IDisposable

  {

  void SayHello();

  }

  public class Example : IExample

  {

  private bool _disposed = false;

  private readonly Guid _key = Guid.NewGuid();

  public void SayHello()

  {

  if (_disposed)

  {

  throw new ObjectDisposedException("Example",

  string.Format("{0} is already disposed!", _key));

  }

  Console.WriteLine("{0} says hello in thread {1}!", _key,

  Thread.CurrentThread.ManagedThreadId);

  }

  public void Dispose()

  {

  if (!_disposed)

  {

  _disposed = true;

  }

  }

  }

  TransientLifetimeManager

  TransientLifetimeManager 是 Unity 默認(rèn)的生存期管理器。其內(nèi)部的實(shí)現(xiàn)都為空,這就意味著每次容器都會(huì)創(chuàng)建和返回一個(gè)新的對(duì)象實(shí)例,當(dāng)然容器也不負(fù)責(zé)存儲(chǔ)和銷毀該對(duì)象實(shí)例。

  private static void TestTransientLifetimeManager()

  {

  IExample example;

  using (IUnityContainer container = new UnityContainer())

  {

  container.RegisterType(typeof(IExample), typeof(Example),

  new TransientLifetimeManager());

  // each one gets its own instance

  container.Resolve().SayHello();

  example = container.Resolve();

  }

  // container is disposed but Example instance still lives

  // all previously created instances weren't disposed!

  example.SayHello();

  Console.ReadKey();

  }

1

  ContainerControlledLifetimeManager

  ContainerControlledLifetimeManager 將為 UnityContainer 及其子容器提供一個(gè) Singleton 的注冊(cè)類型對(duì)象實(shí)例。其只在第一次請(qǐng)求某注冊(cè)類型時(shí)創(chuàng)建一個(gè)新的對(duì)象實(shí)例,該對(duì)象實(shí)例將被存儲(chǔ)到生存期管理器中,并且一直被重用。當(dāng)容器析構(gòu)時(shí),生存期管理器會(huì)調(diào)用 RemoveValue 將存儲(chǔ)的對(duì)象銷毀。

  Singleton 對(duì)象實(shí)例對(duì)應(yīng)每個(gè)對(duì)象類型注冊(cè),如果同一對(duì)象類型注冊(cè)多次,則將為每次注冊(cè)創(chuàng)建單一的實(shí)例。

  private static void TestContainerControlledLifetimeManager()

  {

  IExample example;

  using (IUnityContainer container = new UnityContainer())

  {

  container.RegisterType(typeof(IExample), typeof(Example),

  new ContainerControlledLifetimeManager());

  IUnityContainer firstSub = null;

  IUnityContainer secondSub = null;

  try

  {

  firstSub = container.CreateChildContainer();

  secondSub = container.CreateChildContainer();

  // all containers share same instance

  // each resolve returns same instance

  firstSub.Resolve().SayHello();

  // run one resolving in other thread and still receive same instance

  Thread thread = new Thread(

  () => secondSub.Resolve().SayHello());

  thread.Start();

  container.Resolve().SayHello();

  example = container.Resolve();

  thread.Join();

  }

  finally

  {

  if (firstSub != null) firstSub.Dispose();

  if (secondSub != null) secondSub.Dispose();

  }

  }

  try

  {

  // exception - instance has been disposed with container

  example.SayHello();

  }

  catch (ObjectDisposedException ex)

  {

  Console.WriteLine(ex.Message);

  }

  Console.ReadKey();

  }

2

  HierarchicalLifetimeManager

  HierarchicalLifetimeManager 類衍生自 ContainerControlledLifetimeManager,其繼承了父類的所有行為。與父類的不同之處在于子容器中的生存期管理器行為。ContainerControlledLifetimeManager 共享相同的對(duì)象實(shí)例,包括在子容器中。而 HierarchicalLifetimeManager 只在同一個(gè)容器內(nèi)共享,每個(gè)子容器都有其單獨(dú)的對(duì)象實(shí)例。

  private static void TestHierarchicalLifetimeManager()

  {

  IExample example;

  using (IUnityContainer container = new UnityContainer())

  {

  container.RegisterType(typeof(IExample), typeof(Example),

  new HierarchicalLifetimeManager());

  IUnityContainer firstSub = null;

  IUnityContainer secondSub = null;

  try

  {

  firstSub = container.CreateChildContainer();

  secondSub = container.CreateChildContainer();

  // each subcontainer has its own instance

  firstSub.Resolve().SayHello();

  secondSub.Resolve().SayHello();

  container.Resolve().SayHello();

  example = firstSub.Resolve();

  }

  finally

  {

  if (firstSub != null) firstSub.Dispose();

  if (secondSub != null) secondSub.Dispose();

  }

  }

  try

  {

  // exception - instance has been disposed with container

  example.SayHello();

  }

  catch (ObjectDisposedException ex)

  {

  Console.WriteLine(ex.Message);

  }

  Console.ReadKey();

  }

3

  ExternallyControlledLifetimeManager

  ExternallyControlledLifetimeManager 中的對(duì)象實(shí)例的生存期限將有 UnityContainer 外部的實(shí)現(xiàn)控制。此生存期管理器內(nèi)部直存儲(chǔ)了所提供對(duì)象實(shí)例的一個(gè) WeakReference。所以如果 UnityContainer 容器外部實(shí)現(xiàn)中沒(méi)有對(duì)該對(duì)象實(shí)例的強(qiáng)引用,則該對(duì)象實(shí)例將被 GC 回收。再次請(qǐng)求該對(duì)象類型實(shí)例時(shí),將會(huì)創(chuàng)建新的對(duì)象實(shí)例。

  private static void TestExternallyControlledLifetimeManager()

  {

  IExample example;

  using (IUnityContainer container = new UnityContainer())

  {

  container.RegisterType(typeof(IExample), typeof(Example),

  new ExternallyControlledLifetimeManager());

  // same instance is used in following

  container.Resolve().SayHello();

  container.Resolve().SayHello();

  // run garbate collector. Stored Example instance will be released

  // beacuse there is no reference for it and LifetimeManager holds

  // only WeakReference

  GC.Collect();

  // object stored targeted by WeakReference was released

  // new instance is created!

  container.Resolve().SayHello();

  example = container.Resolve();

  }

  example.SayHello();

  Console.ReadKey();

  }

  需要注意,在 Debug 模式下,編譯器不會(huì)優(yōu)化本地變量,所以引用有可能還存在。而在 Release 模式下會(huì)優(yōu)化。

4

  PerThreadLifetimeManager

  PerThreadLifetimeManager 模型提供“每線程單實(shí)例”功能。所有的對(duì)象實(shí)例在內(nèi)部被存儲(chǔ)在 ThreadStatic 的集合。容器并不跟蹤對(duì)象實(shí)例的創(chuàng)建并且也不負(fù)責(zé) Dispose。

  private static void TestPerThreadLifetimeManager()

  {

  IExample example;

  using (IUnityContainer container = new UnityContainer())

  {

  container.RegisterType(typeof(IExample), typeof(Example),

  new PerThreadLifetimeManager());

  Actionaction = delegate(int sleep)

  {

  // both calls use same instance per thread

  container.Resolve().SayHello();

  Thread.Sleep(sleep);

  container.Resolve().SayHello();

  };

  Thread thread1 = new Thread((a) => action.Invoke((int)a));

  Thread thread2 = new Thread((a) => action.Invoke((int)a));

  thread1.Start(50);

  thread2.Start(50);

  thread1.Join();

  thread2.Join();

  example = container.Resolve();

  }

  example.SayHello();

  Console.ReadKey();

  }

5

  更多關(guān)于unity培訓(xùn)的問(wèn)題,歡迎咨詢千鋒教育在線名師。千鋒教育擁有多年IT培訓(xùn)服務(wù)經(jīng)驗(yàn),采用全程面授高品質(zhì)、高體驗(yàn)培養(yǎng)模式,擁有國(guó)內(nèi)一體化教學(xué)管理及學(xué)員服務(wù),助力更多學(xué)員實(shí)現(xiàn)高薪夢(mèng)想。

tags:
聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
請(qǐng)您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
免費(fèi)領(lǐng)取
今日已有369人領(lǐng)取成功
劉同學(xué) 138****2860 剛剛成功領(lǐng)取
王同學(xué) 131****2015 剛剛成功領(lǐng)取
張同學(xué) 133****4652 剛剛成功領(lǐng)取
李同學(xué) 135****8607 剛剛成功領(lǐng)取
楊同學(xué) 132****5667 剛剛成功領(lǐng)取
岳同學(xué) 134****6652 剛剛成功領(lǐng)取
梁同學(xué) 157****2950 剛剛成功領(lǐng)取
劉同學(xué) 189****1015 剛剛成功領(lǐng)取
張同學(xué) 155****4678 剛剛成功領(lǐng)取
鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
董同學(xué) 138****2867 剛剛成功領(lǐng)取
周同學(xué) 136****3602 剛剛成功領(lǐng)取
相關(guān)推薦HOT
軟件開(kāi)發(fā)管理流程中會(huì)出現(xiàn)哪些問(wèn)題?

一、需求不清需求不明確是導(dǎo)致項(xiàng)目失敗的主要原因之一。如果需求沒(méi)有清晰定義,開(kāi)發(fā)人員可能會(huì)開(kāi)發(fā)出不符合用戶期望的產(chǎn)品。二、通信不足溝通問(wèn)...詳情>>

2023-10-14 13:43:21
軟件定制開(kāi)發(fā)中的敏捷開(kāi)發(fā)是什么?

軟件定制開(kāi)發(fā)中的敏捷開(kāi)發(fā)是什么軟件定制開(kāi)發(fā)中的敏捷開(kāi)發(fā),從宏觀上看,是一個(gè)高度關(guān)注人員交互,持續(xù)開(kāi)發(fā)與交付,接受需求變更并適應(yīng)環(huán)境變化...詳情>>

2023-10-14 13:24:57
什么是PlatformIo?

PlatformIO是什么PlatformIO是一個(gè)全面的物聯(lián)網(wǎng)開(kāi)發(fā)平臺(tái),它為眾多硬件平臺(tái)和開(kāi)發(fā)環(huán)境提供了統(tǒng)一的工作流程,有效簡(jiǎn)化了開(kāi)發(fā)過(guò)程,并能兼容各種...詳情>>

2023-10-14 12:55:06
云快照與自動(dòng)備份有什么區(qū)別?

1、定義和目標(biāo)不同云快照的主要目標(biāo)是提供一種快速恢復(fù)數(shù)據(jù)的方法,它只記錄在快照時(shí)間點(diǎn)后的數(shù)據(jù)變化,而不是所有的數(shù)據(jù)。自動(dòng)備份的主要目標(biāo)...詳情>>

2023-10-14 12:48:59
服務(wù)器為什么要用Linux?

服務(wù)器為什么要用Linux作為服務(wù)器操作系統(tǒng)的優(yōu)選,Linux在眾多選擇中脫穎而出。Linux作為服務(wù)器操作系統(tǒng)的優(yōu)選,有其獨(dú)特的優(yōu)勢(shì)和特點(diǎn)。包括其...詳情>>

2023-10-14 12:34:11
快速通道