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 Lock _lock;
private XyzDependency? _singletonXyzDependency45;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
}
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 accumulator48 = new MyAccumulator();
AbcDependency perBlockAbcDependency4 = new AbcDependency();
if (_root._singletonXyzDependency45 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonXyzDependency45 is null)
{
XyzDependency _singletonXyzDependency45Temp;
_singletonXyzDependency45Temp = new XyzDependency();
accumulator48.Add(_singletonXyzDependency45Temp);
Thread.MemoryBarrier();
_root._singletonXyzDependency45 = _singletonXyzDependency45Temp;
}
}
}
AbcDependency transientAbcDependency3 = new AbcDependency();
using (_lock.EnterScope())
{
accumulator48.Add(transientAbcDependency3);
}
Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency45!, perBlockAbcDependency4);
using (_lock.EnterScope())
{
accumulator48.Add(transientService1);
}
return (transientService1, accumulator48);
}
}
[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:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
AbcDependency --|> IDependency : typeof(Pure.DI.UsageTests.Advanced.AccumulatorScenario.AbcDependency)
XyzDependency --|> IDependency : typeof(Pure.DI.UsageTests.Advanced.AccumulatorScenario.XyzDependency)
Composition ..> ValueTupleᐸIServiceˏMyAccumulatorᐳ : ValueTupleᐸIServiceˏMyAccumulatorᐳ Root
ValueTupleᐸIServiceˏMyAccumulatorᐳ *-- Service : IService
ValueTupleᐸIServiceˏMyAccumulatorᐳ *-- MyAccumulator : MyAccumulator
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
namespace Pure.DI.UsageTests.Advanced.AccumulatorScenario {
class AbcDependency {
+AbcDependency()
}
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 IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class MyAccumulator {
}
class Service {
+Service(IDependency dependency1, IDependency dependency2, IDependency dependency3)
}
class XyzDependency {
+XyzDependency()
}
}
namespace System {
class ValueTupleᐸIServiceˏMyAccumulatorᐳ {
<<struct>>
}
}