To dispose all created singleton instances, simply dispose the composition instance:
using Shouldly;
using Pure.DI;
using static Pure.DI.Lifetime;
DI.Setup(nameof(Composition))
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
.Bind().As(Singleton).To<Dependency>()
.Bind().To<Service>()
.Root<IService>("Root");
IDependency dependency;
using (var composition = new Composition())
{
var service = composition.Root;
dependency = service.Dependency;
}
dependency.IsDisposed.ShouldBeTrue();
interface IDependency
{
bool IsDisposed { get; }
}
class Dependency : IDependency, IDisposable
{
public bool IsDisposed { get; private set; }
public void Dispose() => IsDisposed = true;
}
interface IService
{
public IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
Running this code sample locally
- Make sure you have the .NET SDK 9.0 or later is installed
dotnet --list-sdk
- Create a net9.0 (or later) console application
dotnet new console -n Sample
dotnet add package Pure.DI
dotnet add package Shouldly
- Copy the example code into the Program.cs file
You are ready to run the example 🚀
dotnet run
A composition class becomes disposable if it creates at least one disposable singleton instance.
The following partial class will be generated:
partial class Composition: IDisposable
{
private readonly Composition _root;
private readonly Lock _lock;
private object[] _disposables;
private int _disposeIndex;
private Dependency? _singletonDependency43;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
_disposables = new object[1];
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
_disposables = parentScope._disposables;
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonDependency43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonDependency43 is null)
{
_root._singletonDependency43 = new Dependency();
_root._disposables[_root._disposeIndex++] = _root._singletonDependency43;
}
}
}
return new Service(_root._singletonDependency43);
}
}
public void Dispose()
{
int disposeIndex;
object[] disposables;
using (_lock.EnterScope())
{
disposeIndex = _disposeIndex;
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_singletonDependency43 = null;
}
while (disposeIndex-- > 0)
{
switch (disposables[disposeIndex])
{
case IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (Exception exception)
{
OnDisposeException(disposableInstance, exception);
}
break;
}
}
}
partial void OnDisposeException<T>(T disposableInstance, Exception exception) where T : IDisposable;
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Composition --|> IDisposable
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService Root
Service o-- "Singleton" Dependency : IDependency
namespace Pure.DI.UsageTests.Lifetimes.DisposableSingletonScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}
namespace System {
class IDisposable {
<<abstract>>
}
}