@@ -4,18 +4,154 @@ namespace Unity
44{
55 public static class FactoryLifetime
66 {
7+ /// <summary>
8+ /// Singleton lifetime creates globally unique singleton. Any Unity container
9+ /// tree (parent and all the children) is guaranteed to have only one global
10+ /// singleton for the registered type.
11+ /// </summary>
12+ /// <remarks>
13+ /// <para>Registering a type with singleton lifetime always places the registration
14+ /// at the root of the container tree and makes it globally available for all
15+ /// the children of that container. It does not matter if registration takes
16+ /// places at the root of child container the destination is always the root node.</para>
17+ /// <para>Repeating the registration on any of the child nodes with singleton lifetime
18+ /// will always override the root registration.</para>
19+ /// </remarks>
20+ /// <value>A new instance of a <see cref="SingletonLifetimeManager"/> object.</value>
721 public static IFactoryLifetimeManager Singleton => new SingletonLifetimeManager ( ) ;
822
23+ /// <summary>
24+ /// Unity returns the same instance each time the Resolve(...) method is called or when
25+ /// the dependency mechanism injects the instance into other classes.
26+ /// </summary>
27+ /// <remarks>
28+ /// Per Container lifetime allows a registration of an existing or resolved object as
29+ /// a scoped singleton in the container it was created or registered. In other words this
30+ /// instance is unique within the container it war registered with. Child or parent
31+ /// containers could have their own instances registered for the same contract.
32+ /// </remarks>
33+ /// <value>A new instance of a <see cref="ContainerControlledLifetimeManager"/> object.</value>
934 public static IFactoryLifetimeManager PerContainer => new ContainerControlledLifetimeManager ( ) ;
1035
36+ /// <summary>
37+ /// Unity returns a unique value for each child container.
38+ /// </summary>
39+ /// <remarks>
40+ /// <para>
41+ /// The Unity container allows creating hierarchies of child containers. This lifetime
42+ /// creates local singleton for each level of the hierarchy. So, when you resolve a
43+ /// type and this container does not have an instance of that type, the container will
44+ /// create new instance. Next type the type is resolved the same instance will be returned.
45+ /// </para>
46+ /// <para>
47+ /// If a child container is created and requested to resolve the type, the child container
48+ /// will create a new instance and store it for subsequent resolutions. Next time the
49+ /// child container requested to resolve the type, it will return stored instance.
50+ /// </para>
51+ /// <para>If you have multiple children, each will resolve its own instance.</para>
52+ /// </remarks>
53+ /// <value>A new instance of a <see cref="HierarchicalLifetimeManager"/> object.</value>
1154 public static IFactoryLifetimeManager Hierarchical => new HierarchicalLifetimeManager ( ) ;
1255
56+ /// <summary>
57+ /// Unity returns a unique value for each scope.
58+ /// </summary>
59+ /// <remarks>
60+ /// <para>
61+ /// The Unity container allows creating hierarchies of child containers. This lifetime
62+ /// creates local singleton for each level of the hierarchy. So, when you resolve a
63+ /// type and this container does not have an instance of that type, the container will
64+ /// create new instance. Next type the type is resolved the same instance will be returned.
65+ /// </para>
66+ /// <para>
67+ /// If a child container is created and requested to resolve the type, the child container
68+ /// will create a new instance and store it for subsequent resolutions. Next time the
69+ /// child container requested to resolve the type, it will return stored instance.
70+ /// </para>
71+ /// <para>If you have multiple children, each will resolve its own instance.</para>
72+ /// </remarks>
73+ /// <value>A new instance of a <see cref="HierarchicalLifetimeManager"/> object.</value>
74+ public static ITypeLifetimeManager Scoped => new HierarchicalLifetimeManager ( ) ;
75+
76+ /// <summary>
77+ /// This lifetime keeps a reference to an instance only for the duration of one resolution call
78+ /// </summary>
79+ /// <remarks>
80+ /// This type of lifetime is useful when you need to pass the same instance of the dependency
81+ /// to a different nodes of the resolution graph.
82+ /// </remarks>
83+ /// <example>
84+ /// Consider this scenario:
85+ /// <code>
86+ /// class a {}
87+ ///
88+ /// class b
89+ /// {
90+ /// b(a arg1)
91+ /// {...}
92+ /// }
93+ ///
94+ /// class c
95+ /// {
96+ /// c(a arg1, b arg2)
97+ /// {...}
98+ /// }
99+ /// </code>
100+ /// <para>
101+ /// When you resolve type `c`, it depends on type `b` and type `a`. Type `b`, in turn,
102+ /// also depends on type `a`, and both types, `c` and `b`, require `a` to be the same instance.
103+ /// </para>
104+ /// <para>
105+ /// If type `a` is a singleton, the logic is easy. But if you require each instance of
106+ /// `c` to have a unique `a`, you could use per resolve lifetime. The instance of `a`
107+ /// will act as a singleton only during that one resolution. Next call to resolve the
108+ /// dependent type will create a new object.
109+ /// </para>
110+ /// <para>
111+ /// In the case of recursion, the singleton behavior is still applies and prevents circular dependency
112+ /// </para>
113+ /// </example>
114+ /// <value>A new instance of a <see cref="PerResolveLifetimeManager"/> object.</value>
13115 public static IFactoryLifetimeManager PerResolve => new PerResolveLifetimeManager ( ) ;
14116
117+ /// <summary>
118+ /// Per thread lifetime means a new instance of the registered <see cref="System.Type"/>
119+ /// will be created once per each thread. In other words, if a Resolve{T}() method is
120+ /// called on a thread the first time, it will return a new object. Each subsequent
121+ /// call to Resolve{T}(), or when the dependency mechanism injects instances of
122+ /// the type into other classes on the same thread, the container will return the
123+ /// same object.
124+ /// </summary>
125+ /// <value>A new instance of a <see cref="PerThreadLifetimeManager"/> object.</value>
15126 public static IFactoryLifetimeManager PerThread => new PerThreadLifetimeManager ( ) ;
16127
128+ /// <summary>
129+ /// This lifetime creates and returns a new instance of the requested type for each call
130+ /// to the Resolve(...) method.
131+ /// </summary>
132+ /// <remarks>
133+ /// Transient lifetime is a default lifetime of the Unity container. As the name implies it
134+ /// lasts very short period of time, actually, no time at all. In the Unity container terms,
135+ /// having transient lifetime is the same as having no lifetime manager at all.
136+ /// </remarks>
137+ /// <value>An instance of a <see cref="TransientLifetimeManager"/> object.</value>
17138 public static IFactoryLifetimeManager Transient { get ; } = new TransientLifetimeManager ( ) ;
18139
140+ /// <summary>
141+ /// This lifetime is similar to <see cref="TransientLifetimeManager"/> with exception
142+ /// how the container holds references to created objects.
143+ /// </summary>
144+ /// <remarks>
145+ /// <para>
146+ /// On each call to the Resolve{T}() method a container will create a new objects.
147+ /// If the objects implements <see cref="System.IDisposable"/>, the container will hold a
148+ /// reference to the interface and will dispose the object when the container goes out of scope.
149+ /// </para>
150+ /// <para>
151+ /// This lifetime is particularly useful in session based designs with child containers
152+ /// associated with the session</para>
153+ /// </remarks>
154+ /// <value>A new instance of a <see cref="ContainerControlledTransientManager"/> object.</value>
19155 public static IFactoryLifetimeManager PerContainerTransient => new ContainerControlledTransientManager ( ) ;
20156 }
21157}
0 commit comments