using Pure.DI;
DI.Setup(nameof(Composition))
.RootArg<MyData<TT>>("complexArg")
.Bind<IService<TT2>>().To<Service<TT2>>()
// Composition root
.Root<IService<TT3>>("GetMyService");
var composition = new Composition();
IService<int> service = composition.GetMyService<int>(
new MyData<int>(33, "Just contains an integer value 33"));
record MyData<T>(T Value, string Description);
interface IService<out T>
{
T? Val { get; }
}
class Service<T> : IService<T>
{
// The Dependency attribute specifies to perform an injection,
// the integer value in the argument specifies
// the ordinal of injection
[Dependency]
public void SetDependency(MyData<T> data) =>
Val = data.Value;
public T? Val { get; private set; }
}
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
- Add reference to NuGet package
dotnet add package Pure.DI
- 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;
[OrdinalAttribute(128)]
public Composition()
{
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IService<T> GetMyService<T>(MyData<T> complexArg)
{
if (complexArg is null) throw new ArgumentNullException(nameof(complexArg));
Service<T> transientService0 = new Service<T>();
transientService0.SetDependency(complexArg);
return transientService0;
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
ServiceᐸTᐳ --|> IServiceᐸTᐳ
Composition ..> ServiceᐸTᐳ : IServiceᐸTᐳ GetMyServiceᐸTᐳ(Pure.DI.UsageTests.Basics.ComplexGenericRootArgScenario.MyData<T> complexArg)
ServiceᐸTᐳ o-- MyDataᐸTᐳ : Argument "complexArg"
namespace Pure.DI.UsageTests.Basics.ComplexGenericRootArgScenario {
class Composition {
<<partial>>
+IServiceᐸTᐳ GetMyServiceᐸTᐳ(Pure.DI.UsageTests.Basics.ComplexGenericRootArgScenario.MyData<T> complexArg)
}
class IServiceᐸTᐳ {
<<interface>>
}
class MyDataᐸTᐳ {
<<record>>
}
class ServiceᐸTᐳ {
+Service()
+SetDependency(MyDataᐸTᐳ data) : Void
}
}