using Pure.DI;
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
// Specifies to use CancellationToken from the argument
// when resolving a composition root
.RootArg<CancellationToken>("cancellationToken")
// Composition root
.Root<Task<IService>>("GetMyServiceAsync");
var composition = new Composition();
// Resolves composition roots asynchronously
var service = await composition.GetMyServiceAsync(CancellationToken.None);
interface IDependency;
class Dependency : IDependency;
interface IService;
class Service(IDependency dependency) : IService;
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 Task<IService> GetMyServiceAsync(CancellationToken cancellationToken)
{
Func<IService> perBlockFunc1 = new Func<IService>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
() =>
{
IService localValue76 = new Service(new Dependency());
return localValue76;
});
TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None;
TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None;
TaskScheduler transientTaskScheduler5 = TaskScheduler.Default;
TaskFactory<IService> perBlockTaskFactory2;
CancellationToken localCancellationToken77 = cancellationToken;
TaskCreationOptions localTaskCreationOptions78 = transientTaskCreationOptions3;
TaskContinuationOptions localTaskContinuationOptions79 = transientTaskContinuationOptions4;
TaskScheduler localTaskScheduler80 = transientTaskScheduler5;
perBlockTaskFactory2 = new TaskFactory<IService>(localCancellationToken77, localTaskCreationOptions78, localTaskContinuationOptions79, localTaskScheduler80);
Task<IService> transientTask0;
// Injects an instance factory
Func<IService> localFactory81 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService> localTaskFactory82 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory82.StartNew(localFactory81);
return transientTask0;
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Dependency --|> IDependency
Service --|> IService
Composition ..> TaskᐸIServiceᐳ : TaskᐸIServiceᐳ GetMyServiceAsync(System.Threading.CancellationToken cancellationToken)
Service *-- Dependency : IDependency
TaskᐸIServiceᐳ o-- "PerBlock" FuncᐸIServiceᐳ : FuncᐸIServiceᐳ
TaskᐸIServiceᐳ o-- "PerBlock" TaskFactoryᐸIServiceᐳ : TaskFactoryᐸIServiceᐳ
FuncᐸIServiceᐳ *-- Service : IService
TaskFactoryᐸIServiceᐳ *-- TaskScheduler : TaskScheduler
TaskFactoryᐸIServiceᐳ *-- TaskCreationOptions : TaskCreationOptions
TaskFactoryᐸIServiceᐳ *-- TaskContinuationOptions : TaskContinuationOptions
TaskFactoryᐸIServiceᐳ o-- CancellationToken : Argument "cancellationToken"
namespace Pure.DI.UsageTests.Basics.AsyncRootScenario {
class Composition {
<<partial>>
+TaskᐸIServiceᐳ GetMyServiceAsync(System.Threading.CancellationToken cancellationToken)
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}
namespace System {
class FuncᐸIServiceᐳ {
<<delegate>>
}
}
namespace System.Threading {
class CancellationToken {
<<struct>>
}
}
namespace System.Threading.Tasks {
class TaskContinuationOptions {
<<enum>>
}
class TaskCreationOptions {
<<enum>>
}
class TaskFactoryᐸIServiceᐳ {
}
class TaskScheduler {
<<abstract>>
}
class TaskᐸIServiceᐳ {
}
}