更新时间:2022-09-15 23:14:28
Unity includes six lifetime managers that you can use directly in your code, but you can create your own lifetime managers to implement specific lifetime scenarios. Unity includes the following lifetime managers:
If you registered a type mapping using configuration or using the RegisterType method, Unity creates a new instance of the registered type during the first call to the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. Subsequent requests return the same instance.
If you registered an existing instance of an object using the RegisterInstance method, the container returns the same instance for all calls to Resolve or ResolveAll or when the dependency mechanism injects instances into other classes, provided that one of the following is true:
Once you have a reference to the proper container, call the RegisterInstance method of that container to register the existing object. Specify as the registration type an interface that the object implements, an object type from which the target object inherits, or the concrete type of the object.
The following example creates a named (non-default) mapping by specifying the name, Email and uses the default lifetime.
C#
myContainer.RegisterInstance<EmailService>("Email", myEmailService);
Visual Basic
myContainer.RegisterInstance(Of EmailService)("Email", myEmailService)
When the container is disposed, it calls the Dispose method of the object and allows it to be garbage collected. Therefore, you must ensure that your code does not maintain a reference to the object.
C#
IUnityContainer Parent = new UnityContainer(); IUnityContainer child = Parent.CreateChildContainer(); MyType newInstance = new MyType(); Parent.RegisterInstance<MyType>(newInstance, new HierarchicalLifetimeManager());
Visual Basic
Dim Parent As IUnityContainer = New UnityContainer() Dim child As IUnityContainer = Parent.CreateChildContainer() Dim newInstance As New MyType() Parent.RegisterInstance(Of MyType)(newInstance, New HierarchicalLifetimeManager())
C#
public void ViewIsReusedAcrossGraph() { var container = new UnityContainer() .RegisterType<IPresenter, MockPresenter>() .RegisterType<IView, View>(new PerResolveLifetimeManager()); var view = container.Resolve<IView>(); var realPresenter = (MockPresenter) view.Presenter; }
Visual Basic
Public Sub ViewIsReusedAcrossGraph() Dim container = New UnityContainer() _ .RegisterType(Of IPresenter, MockPresenter)() _ .RegisterType(Of IView, View)(New PerResolveLifetimeManager()) Dim view = container.Resolve(Of IView)() Dim realPresenter = DirectCast(view.Presenter, MockPresenter) End Sub
The following small object graph illustrates the per-build behavior for the example. MockPresenter inherits from IPresenter and contains a View and MockPresenter object. IView contains a Presenterobject and View class contains a Presenter dependency. View is reused across the graph per resolve call because View is registered with a PerResolveLifetimeManager.
C#
public interface IPresenter { } public class MockPresenter : IPresenter { public IView View { get; set; } public MockPresenter(IView view) { View = view; } } public interface IView { IPresenter Presenter { get; set; } } public class View : IView { [Dependency] public IPresenter Presenter { get; set; } }
Visual Basic
Public Interface IPresenter End Interface Public Class MockPresenter Implements IPresenter Private _View As IView Public Property View() As IView Get Return _View End Get Set(ByVal value As IView) _View = value End Set End Property Public Sub New(ByVal view__1 As IView) View = view__1 End Sub End Class Public Interface IView Property Presenter() As IPresenter End Interface Public Class View Implements IView Private _Presenter As IPresenter <Dependency()> _ Public Property Presenter() As IPresenter Get Return _Presenter End Get Set(ByVal value As IPresenter) _Presenter = value End Set End Property End Class
If you registered a type mapping using configuration or using the RegisterType method, Unity creates a new instance of the registered type the first time the type is resolved in a specified thread, either to answer a call to the Resolve or ResolveAll method for the registered type or to fulfill a dependency while resolving a different type. Subsequent resolutions on the same thread return the same instance.
PerThreadLifetimeManager returns the object desired or permits the container to create a new instance if no such object is currently stored for the current thread. A new instance is also created if called on a thread other than the one that set the value.
When using the PerThreadLifetimeManager, it is recommended that you use RegisterType and do not use RegisterInstance to register an object.
When you register an instance with the PerThreadLifetimeManager, the instance is registered only for the executing thread. Calls to Resolve on other threads result in per-thread singletons for container-built instances. If you request a type registered with the PerThreadLifetimeManager in any thread other than the thread it was registered for, the lifetime container for that object finds that there is no registered instance for that thread and, therefore, permits the container to build a new instance for that thread.
The result is that for threads other than the one registering the instance, the behavior is the same as if you registered the container lifetime with RegisterType.
This lifetime manager does not dispose the instances it holds. The thread object is reclaimed by garbage collection when the thread is disposed, but note that the object is not disposed in the sense that the Dispose method is not invoked.
Note:
Using the RegisterInstance method to register an existing object results in the same behavior as if you just registered the lifetime container with RegisterType. Therefore, it is recommended that you do not use the RegisterInstance method to register an existing object when using the non-default lifetime managers except for the thread in which the RegisterInstance was invoked.
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2011/08/11/2134582.html,如需转载请自行联系原作者