using Shouldly;
using Pure.DI;
using System.Collections.Generic;
DI.Setup(nameof(Composition))
.Bind().To<Dependency<TT>>()
.Bind().To<Service<TT>>()
// Composition root
.Root<IService<string>>("Root");
var composition = new Composition();
var service = composition.Root;
var dependencies = service.Dependencies;
dependencies.Count.ShouldBe(2);
dependencies[0].Id.ShouldBe(33);
dependencies[1].Id.ShouldBe(99);
interface IDependency<out T>
{
int Id { get; }
}
class Dependency<T>(int id) : IDependency<T>
{
public int Id { get; } = id;
}
interface IService<out T>
{
IReadOnlyList<IDependency<T>> Dependencies { get; }
}
class Service<T>(Func<int, IDependency<T>> dependencyFactoryWithArgs): IService<T>
{
public IReadOnlyList<IDependency<T>> Dependencies { get; } =
[
dependencyFactoryWithArgs(33),
dependencyFactoryWithArgs(99)
];
}
Running this code sample locally
- Make sure you have the .NET SDK 9.0 or later is installed
dotnet --list-sdk
- Create a net9.0 (or later) console application
dotnet new console -n Sample
dotnet add package Pure.DI
dotnet add package Shouldly
- Copy the example code into the Program.cs file
You are ready to run the example 🚀
dotnet run
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
private readonly Lock _lock;
[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<string> Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
int overInt320;
Func<int, IDependency<string>> perBlockFunc1;
var localLockObject142 = new Object();
Func<int, IDependency<string>> localFactory143 = new Func<int, IDependency<string>>((int localArg19) =>
{
lock (localLockObject142)
{
overInt320 = localArg19;
IDependency<string> localValue144 = new Dependency<string>(overInt320);
return localValue144;
}
});
perBlockFunc1 = localFactory143;
return new Service<string>(perBlockFunc1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
ServiceᐸStringᐳ --|> IServiceᐸStringᐳ
DependencyᐸStringᐳ --|> IDependencyᐸStringᐳ
Composition ..> ServiceᐸStringᐳ : IServiceᐸStringᐳ Root
ServiceᐸStringᐳ o-- "PerBlock" FuncᐸInt32ˏIDependencyᐸStringᐳᐳ : FuncᐸInt32ˏIDependencyᐸStringᐳᐳ
FuncᐸInt32ˏIDependencyᐸStringᐳᐳ *-- DependencyᐸStringᐳ : IDependencyᐸStringᐳ
DependencyᐸStringᐳ o-- "PerResolve" Int32 : Int32
namespace Pure.DI.UsageTests.Generics.GenericInjectionsAsRequiredWithArgumentsScenario {
class Composition {
<<partial>>
+IServiceᐸStringᐳ Root
}
class DependencyᐸStringᐳ {
+Dependency(Int32 id)
}
class IDependencyᐸStringᐳ {
<<interface>>
}
class IServiceᐸStringᐳ {
<<interface>>
}
class ServiceᐸStringᐳ {
+Service(FuncᐸInt32ˏIDependencyᐸStringᐳᐳ dependencyFactoryWithArgs)
}
}
namespace System {
class FuncᐸInt32ˏIDependencyᐸStringᐳᐳ {
<<delegate>>
}
class Int32 {
<<struct>>
}
}