zoukankan      html  css  js  c++  java
  • Unity Lifetime

    There are 6 types of Lifetime Managers available to work with.

    TransientLifetimeManager. For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve or ResolveAll method. This lifetime manager is used by default for all types registered using the RegisterType, method unless you specify a different lifetime manager.

    ContainerControlledLifetimeManager which registers an existing object as a singleton instance. For this lifetime manager Unity returns the same instance of the registered type or object each time you call the Resolve orResolveAll method or when the dependency mechanism injects instances into other classes. 

    HierarchicalLifetimeManagerFor this lifetime manager, as for theContainerControlledLifetimeManager, Unity returns the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. The distinction is that when there are child containers, each child resolves its own instance of the object and does not share one with the parent. When resolving in the parent, the behavior is like a container controlled lifetime; when resolving the parent and the child you have different instances with each acting as a container-controlled lifetime.

    PerResolveLifetimeManager. For this lifetime manager the behavior is like a TransientLifetimeManager, but also provides a signal to the default build plan, marking the type so that instances are reused across the build-up object graph. In the case of recursion, the singleton behavior applies where the object has been registered with thePerResolveLifetimeManager. The following example uses the PerResolveLifetimeManager

    PerThreadLifetimeManager. For this lifetime manager Unity returns, on a per-thread basis, the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. This lifetime manager effectively implements a singleton behavior for objects on a per-thread basis. PerThreadLifetimeManager returns different objects from the container for each thread.

    ExternallyControlledLifetimeManager. The ExternallyControlled LifetimeManager class provides generic support for externally managed lifetimes. This lifetime manager allows you to register type mappings and existing objects with the container so that it maintains only a weak reference to the objects it creates when you call theResolve or ResolveAll method or when the dependency mechanism injects instances into other classes based on attributes or constructor parameters within that class. This allows other code to maintain the object in memory or dispose it and enables you to maintain control of the lifetime of existing objects or allow some other mechanism to control the lifetime.

    With this we have seen how to resolve Dependencies using Unity frame work and significance of Lifetime Managers.

    Origin Url:http://www.codeproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager

  • 相关阅读:
    MSSQL Rebuild(重建)索引
    FileSystemWatcher触发多次Change事件的解决办法 .
    .NET和SQL Server中“空值”辨析 (DBNull与Null的区别)
    给 C# 开发者的代码审查清单
    sql server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件
    远程MSMQ
    .net中的多线程
    C#使用Monitor类、Lock和Mutex类进行多线程同步
    如果是除去末尾特定字符或字符串:TrimEnd方法性能优于Remove方法
    C#编程总结(四)多线程应用
  • 原文地址:https://www.cnblogs.com/mumue/p/2804721.html
Copyright © 2011-2022 走看看