If you want ready-made classes for tracking disposable objects in your libraries but don't want to create your own, you can add this package to your projects:
It contains attributes like Inject
and Inject<T>
that work for constructors and their arguments, methods and their arguments, properties and fields. They allow you to setup all injection parameters.
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; }
public IDependency SingleDependency { get; }
}
class Service(
Func<Own<IDependency>> dependencyFactory,
[Tag("single")] Func<Own<IDependency>> singleDependencyFactory)
: IService, IDisposable
{
private readonly Own<IDependency> _dependency = dependencyFactory();
private readonly Own<IDependency> _singleDependency = singleDependencyFactory();
public IDependency Dependency => _dependency.Value;
public IDependency SingleDependency => _singleDependency.Value;
public void Dispose()
{
_dependency.Dispose();
_singleDependency.Dispose();
}
}
partial class Composition
{
static void Setup() =>
DI.Setup()
.Bind().To<Dependency>()
.Bind("single").As(Lifetime.Singleton).To<Dependency>()
.Bind().To<Service>()
// Composition root
.Root<Service>("Root");
}
var composition = new Composition();
var root1 = composition.Root;
var root2 = composition.Root;
root1.Dependency.ShouldNotBe(root2.Dependency);
root1.SingleDependency.ShouldBe(root2.SingleDependency);
root2.Dispose();
// Checks that the disposable instances
// associated with root1 have been disposed of
root2.Dependency.IsDisposed.ShouldBeTrue();
// But the singleton is still not disposed of
root2.SingleDependency.IsDisposed.ShouldBeFalse();
// Checks that the disposable instances
// associated with root2 have not been disposed of
root1.Dependency.IsDisposed.ShouldBeFalse();
root1.SingleDependency.IsDisposed.ShouldBeFalse();
root1.Dispose();
// Checks that the disposable instances
// associated with root2 have been disposed of
root1.Dependency.IsDisposed.ShouldBeTrue();
// But the singleton is still not disposed of
root1.SingleDependency.IsDisposed.ShouldBeFalse();
composition.Dispose();
root1.SingleDependency.IsDisposed.ShouldBeTrue();
This package should also be included in a project:
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? _singletonDependency42;
[OrdinalAttribute(20)]
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 Service Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var accumulator48 = new Abstractions.Own();
Func<Abstractions.Own<IDependency>> perBlockFunc2 = new Func<Abstractions.Own<IDependency>>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
var accumulator48 = new Abstractions.Own();
if (_root._singletonDependency42 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonDependency42 is null)
{
_root._singletonDependency42 = new Dependency();
_root._disposables[_root._disposeIndex++] = _root._singletonDependency42;
}
}
}
Abstractions.Own<IDependency> perBlockOwn3;
// Creates the owner of an instance
Abstractions.Own localOwn18 = accumulator48;
IDependency localValue19 = _root._singletonDependency42!;
perBlockOwn3 = new Abstractions.Own<IDependency>(localValue19, localOwn18);
using (_lock.EnterScope())
{
accumulator48.Add(perBlockOwn3);
}
Abstractions.Own<IDependency> localValue17 = perBlockOwn3;
return localValue17;
});
Func<Abstractions.Own<IDependency>> perBlockFunc1 = new Func<Abstractions.Own<IDependency>>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
var accumulator48 = new Abstractions.Own();
Dependency transientDependency7 = new Dependency();
using (_lock.EnterScope())
{
accumulator48.Add(transientDependency7);
}
Abstractions.Own<IDependency> perBlockOwn5;
// Creates the owner of an instance
Abstractions.Own localOwn21 = accumulator48;
IDependency localValue22 = transientDependency7;
perBlockOwn5 = new Abstractions.Own<IDependency>(localValue22, localOwn21);
using (_lock.EnterScope())
{
accumulator48.Add(perBlockOwn5);
}
Abstractions.Own<IDependency> localValue20 = perBlockOwn5;
return localValue20;
});
Service transientService0 = new Service(perBlockFunc1, perBlockFunc2);
using (_lock.EnterScope())
{
accumulator48.Add(transientService0);
}
return transientService0;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Resolve<T>()
{
return Resolver<T>.Value.Resolve(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Resolve<T>(object? tag)
{
return Resolver<T>.Value.ResolveByTag(this, tag);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Type type)
{
var index = (int)(_bucketSize * ((uint)RuntimeHelpers.GetHashCode(type) % 1));
ref var pair = ref _buckets[index];
return pair.Key == type ? pair.Value.Resolve(this) : Resolve(type, index);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private object Resolve(Type type, int index)
{
var finish = index + _bucketSize;
while (++index < finish)
{
ref var pair = ref _buckets[index];
if (pair.Key == type)
{
return pair.Value.Resolve(this);
}
}
throw new InvalidOperationException($"{CannotResolveMessage} {OfTypeMessage} {type}.");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Type type, object? tag)
{
var index = (int)(_bucketSize * ((uint)RuntimeHelpers.GetHashCode(type) % 1));
ref var pair = ref _buckets[index];
return pair.Key == type ? pair.Value.ResolveByTag(this, tag) : Resolve(type, tag, index);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private object Resolve(Type type, object? tag, int index)
{
var finish = index + _bucketSize;
while (++index < finish)
{
ref var pair = ref _buckets[index];
if (pair.Key == type)
{
return pair.Value.ResolveByTag(this, tag);
}
}
throw new InvalidOperationException($"{CannotResolveMessage} \"{tag}\" {OfTypeMessage} {type}.");
}
public void Dispose()
{
int disposeIndex;
object[] disposables;
using (_lock.EnterScope())
{
disposeIndex = _disposeIndex;
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_singletonDependency42 = 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;
private readonly static int _bucketSize;
private readonly static Pair<Type, IResolver<Composition, object>>[] _buckets;
static Composition()
{
var valResolver_0000 = new Resolver_0000();
Resolver<Service>.Value = valResolver_0000;
_buckets = Buckets<Type, IResolver<Composition, object>>.Create(
1,
out _bucketSize,
new Pair<Type, IResolver<Composition, object>>[1]
{
new Pair<Type, IResolver<Composition, object>>(typeof(Service), valResolver_0000)
});
}
private const string CannotResolveMessage = "Cannot resolve composition root ";
private const string OfTypeMessage = "of type ";
private class Resolver<T>: IResolver<Composition, T>
{
public static IResolver<Composition, T> Value = new Resolver<T>();
public virtual T Resolve(Composition composite)
{
throw new InvalidOperationException($"{CannotResolveMessage}{OfTypeMessage}{typeof(T)}.");
}
public virtual T ResolveByTag(Composition composite, object tag)
{
throw new InvalidOperationException($"{CannotResolveMessage}\"{tag}\" {OfTypeMessage}{typeof(T)}.");
}
}
private sealed class Resolver_0000: Resolver<Service>
{
public override Service Resolve(Composition composition)
{
return composition.Root;
}
public override Service ResolveByTag(Composition composition, object tag)
{
switch (tag)
{
case null:
return composition.Root;
default:
return base.ResolveByTag(composition, tag);
}
}
}
}
Class diagram:
classDiagram
class Composition {
<<partial>>
+Service Root
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
+ object Resolve(Type type, object? tag)
}
Composition --|> IDisposable
class FuncᐸOwnᐸIDependencyᐳᐳ
class OwnᐸIDependencyᐳ
class Own
Dependency --|> IDependency
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
Composition ..> Service : Service Root
Service o-- "PerBlock" FuncᐸOwnᐸIDependencyᐳᐳ : FuncᐸOwnᐸIDependencyᐳᐳ
Service o-- "PerBlock" FuncᐸOwnᐸIDependencyᐳᐳ : "single" FuncᐸOwnᐸIDependencyᐳᐳ
FuncᐸOwnᐸIDependencyᐳᐳ o-- "PerBlock" OwnᐸIDependencyᐳ : OwnᐸIDependencyᐳ
FuncᐸOwnᐸIDependencyᐳᐳ o-- "PerBlock" OwnᐸIDependencyᐳ : "single" OwnᐸIDependencyᐳ
OwnᐸIDependencyᐳ *-- Own : Own
OwnᐸIDependencyᐳ *-- Dependency : IDependency
OwnᐸIDependencyᐳ *-- Own : Own
OwnᐸIDependencyᐳ o-- "Singleton" Dependency : "single" IDependency