diff --git a/src/Lifetime/ContainerControlledTransientManager.cs b/src/Lifetime/ContainerControlledTransientManager.cs new file mode 100644 index 00000000..c2292e60 --- /dev/null +++ b/src/Lifetime/ContainerControlledTransientManager.cs @@ -0,0 +1,34 @@ +using System; + +namespace Unity.Lifetime +{ + /// + /// A special lifetime manager which works like , + /// except container remembers all Disposable objects it created. Once container + /// is disposed all these objects are disposed as well. + /// + public class ContainerControlledTransientManager : LifetimeManager + { + public override void SetValue(object newValue, ILifetimeContainer container = null) + { + if (newValue is IDisposable disposable) + container?.Add(disposable); + } + + public override object GetValue(ILifetimeContainer container = null) + { + return null; + } + + public override void RemoveValue(ILifetimeContainer container = null) + { + } + + protected override LifetimeManager OnCreateLifetimeManager() + { + return this; + } + + public override bool InUse { get => false; set => base.InUse = false; } + } +}