interface IDependency<T>;
class AbcDependency<T> : IDependency<T>;
class XyzDependency<T> : IDependency<T>;
interface IService<T>
{
ImmutableArray<IDependency<T>> Dependencies { get; }
}
class Service<T>(IEnumerable<IDependency<T>> dependencies) : IService<T>
{
public ImmutableArray<IDependency<T>> Dependencies { get; }
= [..dependencies];
}
DI.Setup(nameof(Composition))
.Bind<IDependency<TT>>().To<AbcDependency<TT>>()
.Bind<IDependency<TT>>("Xyz").To<XyzDependency<TT>>()
.Bind<IService<TT>>().To<Service<TT>>()
// Composition roots
.Root<IService<int>>("IntRoot")
.Root<IService<string>>("StringRoot");
var composition = new Composition();
var intService = composition.IntRoot;
intService.Dependencies.Length.ShouldBe(2);
intService.Dependencies[0].ShouldBeOfType<AbcDependency<int>>();
intService.Dependencies[1].ShouldBeOfType<XyzDependency<int>>();
var stringService = composition.StringRoot;
stringService.Dependencies.Length.ShouldBe(2);
stringService.Dependencies[0].ShouldBeOfType<AbcDependency<string>>();
stringService.Dependencies[1].ShouldBeOfType<XyzDependency<string>>();
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
}
public IService<string> StringRoot
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerable<IDependency<string>> EnumerationOf_perBlockIEnumerable1()
{
yield return new AbcDependency<string>();
yield return new XyzDependency<string>();
}
IEnumerable<IDependency<string>> perBlockIEnumerable1 = EnumerationOf_perBlockIEnumerable1();
return new Service<string>(perBlockIEnumerable1);
}
}
public IService<int> IntRoot
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerable<IDependency<int>> EnumerationOf_perBlockIEnumerable1()
{
yield return new AbcDependency<int>();
yield return new XyzDependency<int>();
}
IEnumerable<IDependency<int>> perBlockIEnumerable1 = EnumerationOf_perBlockIEnumerable1();
return new Service<int>(perBlockIEnumerable1);
}
}
[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) % 4));
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) % 4));
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<string>>.Value = valResolver_0000;
var valResolver_0001 = new Resolver_0001();
Resolver<IService<int>>.Value = valResolver_0001;
_buckets = Buckets<Type, IResolver<Composition, object>>.Create(
4,
out _bucketSize,
new Pair<Type, IResolver<Composition, object>>[2]
{
new Pair<Type, IResolver<Composition, object>>(typeof(IService<string>), valResolver_0000)
,new Pair<Type, IResolver<Composition, object>>(typeof(IService<int>), valResolver_0001)
});
}
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<string>>
{
public override IService<string> Resolve(Composition composition)
{
return composition.StringRoot;
}
public override IService<string> ResolveByTag(Composition composition, object tag)
{
switch (tag)
{
case null:
return composition.StringRoot;
default:
return base.ResolveByTag(composition, tag);
}
}
}
private sealed class Resolver_0001: Resolver<IService<int>>
{
public override IService<int> Resolve(Composition composition)
{
return composition.IntRoot;
}
public override IService<int> ResolveByTag(Composition composition, object tag)
{
switch (tag)
{
case null:
return composition.IntRoot;
default:
return base.ResolveByTag(composition, tag);
}
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
ServiceᐸStringᐳ --|> IServiceᐸStringᐳ
ServiceᐸInt32ᐳ --|> IServiceᐸInt32ᐳ
AbcDependencyᐸStringᐳ --|> IDependencyᐸStringᐳ
XyzDependencyᐸStringᐳ --|> IDependencyᐸStringᐳ : "Xyz"
AbcDependencyᐸInt32ᐳ --|> IDependencyᐸInt32ᐳ
XyzDependencyᐸInt32ᐳ --|> IDependencyᐸInt32ᐳ : "Xyz"
Composition ..> ServiceᐸStringᐳ : IServiceᐸStringᐳ StringRoot
Composition ..> ServiceᐸInt32ᐳ : IServiceᐸInt32ᐳ IntRoot
ServiceᐸStringᐳ o-- "PerBlock" IEnumerableᐸIDependencyᐸStringᐳᐳ : IEnumerableᐸIDependencyᐸStringᐳᐳ
ServiceᐸInt32ᐳ o-- "PerBlock" IEnumerableᐸIDependencyᐸInt32ᐳᐳ : IEnumerableᐸIDependencyᐸInt32ᐳᐳ
IEnumerableᐸIDependencyᐸStringᐳᐳ *-- AbcDependencyᐸStringᐳ : IDependencyᐸStringᐳ
IEnumerableᐸIDependencyᐸStringᐳᐳ *-- XyzDependencyᐸStringᐳ : "Xyz" IDependencyᐸStringᐳ
IEnumerableᐸIDependencyᐸInt32ᐳᐳ *-- AbcDependencyᐸInt32ᐳ : IDependencyᐸInt32ᐳ
IEnumerableᐸIDependencyᐸInt32ᐳᐳ *-- XyzDependencyᐸInt32ᐳ : "Xyz" IDependencyᐸInt32ᐳ
namespace Pure.DI.UsageTests.BCL.EnumerableGenericsScenario {
class AbcDependencyᐸInt32ᐳ {
+AbcDependency()
}
class AbcDependencyᐸStringᐳ {
+AbcDependency()
}
class Composition {
<<partial>>
+IServiceᐸInt32ᐳ IntRoot
+IServiceᐸStringᐳ StringRoot
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
+ object Resolve(Type type, object? tag)
}
class IDependencyᐸInt32ᐳ {
<<interface>>
}
class IDependencyᐸStringᐳ {
<<interface>>
}
class IServiceᐸInt32ᐳ {
<<interface>>
}
class IServiceᐸStringᐳ {
<<interface>>
}
class ServiceᐸInt32ᐳ {
+Service(IEnumerableᐸIDependencyᐸInt32ᐳᐳ dependencies)
}
class ServiceᐸStringᐳ {
+Service(IEnumerableᐸIDependencyᐸStringᐳᐳ dependencies)
}
class XyzDependencyᐸInt32ᐳ {
+XyzDependency()
}
class XyzDependencyᐸStringᐳ {
+XyzDependency()
}
}
namespace System.Collections.Generic {
class IEnumerableᐸIDependencyᐸInt32ᐳᐳ {
<<interface>>
}
class IEnumerableᐸIDependencyᐸStringᐳᐳ {
<<interface>>
}
}