A partial class can contain setup code.
interface IDependency
{
long Id { get; }
}
class Dependency(long id) : IDependency
{
public long Id { get; } = id;
}
class Service(
[Tag("name with id")] string name,
IDependency dependency1,
IDependency dependency2)
{
public string Name { get; } = name;
public IDependency Dependency1 { get; } = dependency1;
public IDependency Dependency2 { get; } = dependency2;
}
// The partial class is also useful for specifying access modifiers to the generated class
public partial class Composition
{
private readonly string _serviceName = "";
private long _id;
// Customizable constructor
public Composition(string serviceName)
: this()
{
_serviceName = serviceName;
}
private long GenerateId() => Interlocked.Increment(ref _id);
// In fact, this method will not be called at runtime
[Conditional("DI")]
void Setup() =>
DI.Setup()
.Bind<IDependency>().To<Dependency>()
.Bind<long>().To(_ => GenerateId())
.Bind<string>("name with id").To(
_ => $"{_serviceName}_{GenerateId()}")
.Root<Service>(
"Root",
kind: RootKinds.Internal);
}
var composition = new Composition("Abc");
var service = composition.Root;
service.Dependency1.Id.ShouldBe(1);
service.Dependency2.Id.ShouldBe(2);
service.Name.ShouldBe("Abc_3");
The partial class is also useful for specifying access modifiers to the generated class.
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;
}
internal Service Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
long transientInt645 = GenerateId();
long transientInt644 = GenerateId();
string transientString1 = $"{_serviceName}_{GenerateId()}";
return new Service(transientString1, new Dependency(transientInt645), new Dependency(transientInt644));
}
}
[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<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:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Dependency --|> IDependency
Composition ..> Service : Service Root
Service *-- String : "name with id" String
Service *-- "2 " Dependency : IDependency
Dependency *-- Int64 : Int64
namespace Pure.DI.UsageTests.Advanced.PartialClassScenario {
class Composition {
<<partial>>
~Service Root
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
+ object Resolve(Type type, object? tag)
}
class Dependency {
+Dependency(Int64 id)
}
class IDependency {
<<interface>>
}
class Service {
}
}
namespace System {
class Int64 {
<<struct>>
}
class String {
}
}