麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > Unity容器中的對象生存期管理

Unity容器中的對象生存期管理

來源:千鋒教育
發布人:qyf
時間: 2022-08-18 17:28:00 1660814880

  IoC 容器的對象生存期管理

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

  Unity 提供的對象生存期管理模型是通過從抽象類 LifetimeManager 的派生類來完成。Unity 將為每個類型的注冊創建生存期管理器。每當 UnityContainer 需要創建一個新的對象實例時,將首先檢測該對象類型的生存期管理器,是否已有一個對象實例可用。如果沒有對象實例可用,則 UnityContainer 將基于配置的信息構造該對象實例并將該對象交予對象生存期管理器。

  LifetimeManager

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

  GetValue - 返回一個已經存儲在生存期管理器中對象實例。

  SetValue - 存儲一個新對象實例到生存期管理器中。

  RemoveValue - 從生存期管理器中將已存儲的對象實例刪除。UnityContainer 的默認實現將不會調用此方法,但可在定制的容器擴展中調用。

  Unity 內置了 6 種生存期管理模型,其中有 2 種即負責對象實例的創建也負責對象實例的銷毀(Dispose)。

  TransientLifetimeManager - 為每次請求生成新的類型對象實例。 (默認行為)

  ContainerControlledLifetimeManager - 實現 Singleton 對象實例。 當容器被 Disposed 后,對象實例也被 Disposed。

  HierarchicalifetimeManager - 實現 Singleton 對象實例。但子容器并不共享父容器實例,而是創建針對字容器的 Singleton 對象實例。當容器被 Disposed 后,對象實例也被 Disposed。

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

  PerThreadLifetimeManager - 為每個線程生成 Singleton 的對象實例,通過 ThreadStatic 實現。

  PerResolveLifetimeManager - 實現與 TransientLifetimeManager 類似的行為,為每次請求生成新的類型對象實例。不同之處在于對象實例在 BuildUp 過程中是可被重用的。

  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 默認的生存期管理器。其內部的實現都為空,這就意味著每次容器都會創建和返回一個新的對象實例,當然容器也不負責存儲和銷毀該對象實例。

  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 及其子容器提供一個 Singleton 的注冊類型對象實例。其只在第一次請求某注冊類型時創建一個新的對象實例,該對象實例將被存儲到生存期管理器中,并且一直被重用。當容器析構時,生存期管理器會調用 RemoveValue 將存儲的對象銷毀。

  Singleton 對象實例對應每個對象類型注冊,如果同一對象類型注冊多次,則將為每次注冊創建單一的實例。

  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 共享相同的對象實例,包括在子容器中。而 HierarchicalLifetimeManager 只在同一個容器內共享,每個子容器都有其單獨的對象實例。

  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 中的對象實例的生存期限將有 UnityContainer 外部的實現控制。此生存期管理器內部直存儲了所提供對象實例的一個 WeakReference。所以如果 UnityContainer 容器外部實現中沒有對該對象實例的強引用,則該對象實例將被 GC 回收。再次請求該對象類型實例時,將會創建新的對象實例。

  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 模式下,編譯器不會優化本地變量,所以引用有可能還存在。而在 Release 模式下會優化。

4

  PerThreadLifetimeManager

  PerThreadLifetimeManager 模型提供“每線程單實例”功能。所有的對象實例在內部被存儲在 ThreadStatic 的集合。容器并不跟蹤對象實例的創建并且也不負責 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

  更多關于unity培訓的問題,歡迎咨詢千鋒教育在線名師。千鋒教育擁有多年IT培訓服務經驗,采用全程面授高品質、高體驗培養模式,擁有國內一體化教學管理及學員服務,助力更多學員實現高薪夢想。

tags:
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 在线观看亚洲专区| 污动漫3d| 淫术の馆在动漫在线播放| 日本三级电影网址| lover视频无删减免费观看| 无码日韩精品一区二区免费| 一区在线观看| 成人免费视频观看无遮挡| 国产成人精品亚洲一区| 大女小娟二女小妍| 好紧我太爽了视频免费国产| 亲密爱人免费观看完整版| 久草香蕉视频| 影音先锋男人站| 粉色视频在线播放| 日本夫妇交换| 四虎成人影院网址| 高清三级毛片| 亚洲精品永久www忘忧草| 人人爽在线| 三级一级片| 国产twink男同chinese| 把极品白丝班长啪到腿软| 女人国产香蕉久久精品| 夫醉酒被公侵犯的电影中字版| 岛国片免费在线观看| 8x视频在线观看| 久久久久久久久国产| 樱花草在线社区www| 天天操夜夜操| 日韩欧美卡一卡二卡新区| 处破女18分钟完整版| 国产69久久精品成人看| 中文在线观看永久免费| 女人18岁毛片| 最新欧美精品一区二区三区| 欧美在线第一二三四区| 永久黄网站色视频免费观看 | 国产一区二区在线视频| 第一福利官方导航| 天天爱夜夜操|