using Pure.DI;
DI.Setup(nameof(Composition))
.RootArg<TT>("someArg")
.Bind<IService<TT>>().To<Service<TT>>()
// Composition root
.Root<IService<TT>>("GetMyService");
var composition = new Composition();
IService<int> service = composition.GetMyService<int>(someArg: 33);
interface IService<out T>
{
T? Dependency { 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(T dependency) =>
Dependency = dependency;
public T? Dependency { 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<T3> GetMyService<T3>(T3 someArg)
{
if (someArg is null) throw new ArgumentNullException(nameof(someArg));
Service<T3> transientService0 = new Service<T3>();
transientService0.SetDependency(someArg);
return transientService0;
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
ServiceᐸT3ᐳ --|> IServiceᐸT3ᐳ
Composition ..> ServiceᐸT3ᐳ : IServiceᐸT3ᐳ GetMyServiceᐸT3ᐳ(T3 someArg)
ServiceᐸT3ᐳ o-- T3 : Argument "someArg"
namespace Pure.DI.UsageTests.Basics.GenericRootArgScenario {
class Composition {
<<partial>>
+IServiceᐸT3ᐳ GetMyServiceᐸT3ᐳ(T3 someArg)
}
class IServiceᐸT3ᐳ {
<<interface>>
}
class ServiceᐸT3ᐳ {
+Service()
+SetDependency(T3 dependency) : Void
}
}