Accumulators allow you to accumulate instances of certain types and lifetimes.
interface IAccumulating;
class MyAccumulator : List<IAccumulating>;
interface IDependency;
class AbcDependency : IDependency, IAccumulating;
class XyzDependency : IDependency, IAccumulating;
interface IService;
class Service(
[Tag(typeof(AbcDependency))] IDependency dependency1,
[Tag(typeof(XyzDependency))] IDependency dependency2,
IDependency dependency3)
: IService, IAccumulating;
DI.Setup(nameof(Composition))
.Accumulate<IAccumulating, MyAccumulator>(Lifetime.Transient, Lifetime.Singleton)
.Bind<IDependency>().As(Lifetime.PerBlock).To<AbcDependency>()
.Bind<IDependency>(Tag.Type).To<AbcDependency>()
.Bind<IDependency>(Tag.Type).As(Lifetime.Singleton).To<XyzDependency>()
.Bind<IService>().To<Service>()
.Root<(IService service, MyAccumulator accumulator)>("Root");
var composition = new Composition();
var (service, accumulator) = composition.Root;
accumulator.Count.ShouldBe(3);
accumulator[0].ShouldBeOfType<XyzDependency>();
accumulator[1].ShouldBeOfType<AbcDependency>();
accumulator[2].ShouldBeOfType<Service>();
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
private readonly object _lock;
private XyzDependency? _singletonXyzDependency41;
[OrdinalAttribute(20)]
public Composition()
{
_root = this;
_lock = new object();
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}
public (IService service, MyAccumulator accumulator) Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var accumulator44 = new MyAccumulator();
AbcDependency perBlockAbcDependency4 = new AbcDependency();
if (_root._singletonXyzDependency41 == null)
{
lock (_lock)
{
if (_root._singletonXyzDependency41 == null)
{
XyzDependency _singletonXyzDependency41Temp;
_singletonXyzDependency41Temp = new XyzDependency();
accumulator44.Add(_singletonXyzDependency41Temp);
Thread.MemoryBarrier();
_root._singletonXyzDependency41 = _singletonXyzDependency41Temp;
}
}
}
AbcDependency transientAbcDependency3 = new AbcDependency();
lock (_lock)
{
accumulator44.Add(transientAbcDependency3);
}
Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency41!, perBlockAbcDependency4);
lock (_lock)
{
accumulator44.Add(transientService1);
}
return (transientService1, accumulator44);
}
}
[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}.");
}
private readonly static int _bucketSize;
private readonly static Pair<Type, IResolver<Composition, object>>[] _buckets;
static Composition()
{
var valResolver_0000 = new Resolver_0000();
Resolver<(IService service, MyAccumulator accumulator)>.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((IService service, MyAccumulator accumulator)), 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<(IService service, MyAccumulator accumulator)>, IResolver<Composition, object>
{
public override (IService service, MyAccumulator accumulator) Resolve(Composition composition)
{
return composition.Root;
}
public override (IService service, MyAccumulator accumulator) ResolveByTag(Composition composition, object tag)
{
switch (tag)
{
case null:
return composition.Root;
default:
return base.ResolveByTag(composition, tag);
}
}
object IResolver<Composition, object>.Resolve(Composition composition)
{
return Resolve(composition);
}
object IResolver<Composition, object>.ResolveByTag(Composition composition, object tag)
{
return ResolveByTag(composition, tag);
}
}
}
Class diagram:
classDiagram
class Composition {
<<partial>>
+ValueTupleᐸIServiceˏMyAccumulatorᐳ Root
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
+ object Resolve(Type type, object? tag)
}
class MyAccumulator
class ValueTupleᐸIServiceˏMyAccumulatorᐳ {
+ValueTuple(IService item1, MyAccumulator item2)
}
AbcDependency --|> IDependency
class AbcDependency {
+AbcDependency()
}
XyzDependency --|> IDependency : typeof(Pure.DI.UsageTests.Advanced.AccumulatorScenario.XyzDependency)
class XyzDependency {
+XyzDependency()
}
Service --|> IService
class Service {
+Service(IDependency dependency1, IDependency dependency2, IDependency dependency3)
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
ValueTupleᐸIServiceˏMyAccumulatorᐳ *-- Service : IService
ValueTupleᐸIServiceˏMyAccumulatorᐳ *-- MyAccumulator : MyAccumulator
Composition ..> ValueTupleᐸIServiceˏMyAccumulatorᐳ : ValueTupleᐸIServiceˏMyAccumulatorᐳ Root
Service *-- AbcDependency : typeof(Pure.DI.UsageTests.Advanced.AccumulatorScenario.AbcDependency) IDependency
Service o-- "Singleton" XyzDependency : typeof(Pure.DI.UsageTests.Advanced.AccumulatorScenario.XyzDependency) IDependency
Service o-- "PerBlock" AbcDependency : IDependency