@@ -13,6 +13,7 @@ namespace Unity.Lifetime
1313 public class LifetimeManagerFactory : ILifetimeFactoryPolicy
1414 {
1515 private readonly ExtensionContext _containerContext ;
16+ private readonly ILifetimeFactoryPolicy _policy ;
1617
1718 /// <summary>
1819 /// Create a new <see cref="LifetimeManagerFactory"/> that will
@@ -27,12 +28,36 @@ public LifetimeManagerFactory(ExtensionContext containerContext, Type lifetimeTy
2728 LifetimeType = lifetimeType ;
2829 }
2930
31+ /// <summary>
32+ /// Create a new <see cref="LifetimeManagerFactory"/> that will
33+ /// return instances of the given type, creating them by
34+ /// resolving through the container.
35+ /// </summary>
36+ /// <param name="policy">LifetimeManager to create.</param>
37+ public LifetimeManagerFactory ( ExtensionContext containerContext , ILifetimeFactoryPolicy policy )
38+ {
39+ _containerContext = containerContext ;
40+ _policy = policy ?? throw new ArgumentNullException ( nameof ( policy ) ) ;
41+ LifetimeType = policy . GetType ( ) ;
42+ }
43+
44+
3045 /// <summary>
3146 /// Create a new instance of <see cref="ILifetimePolicy"/>.
3247 /// </summary>
3348 /// <returns>The new instance.</returns>
3449 public ILifetimePolicy CreateLifetimePolicy ( )
3550 {
51+ if ( null != _policy )
52+ {
53+ var policy = _policy . CreateLifetimePolicy ( ) ;
54+ if ( policy is IDisposable )
55+ {
56+ _containerContext . Lifetime . Add ( policy ) ;
57+ }
58+ return policy ;
59+ }
60+
3661 var lifetime = typeof ( TransientLifetimeManager ) == LifetimeType
3762 ? new TransientLifetimeManager ( )
3863 : ( LifetimeManager ) _containerContext . Container . Resolve ( LifetimeType , null ) ;
0 commit comments