This example demonstrates how to create and initialize an instance manually. At the compilation stage, the set of dependencies that the object to be created needs is determined. In most cases, this happens automatically, according to the set of constructors and their arguments, and does not require additional customization efforts. But sometimes it is necessary to manually create and/or initialize an object, as in lines of code:
using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<IDependency>(ctx =>
{
// Some logic for creating an instance:
ctx.Inject(out Dependency dependency);
dependency.Initialize();
return dependency;
})
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("MyService");
var composition = new Composition();
var service = composition.MyService;
service.Dependency.IsInitialized.ShouldBeTrue();
interface IDependency
{
bool IsInitialized { get; }
}
class Dependency : IDependency
{
public bool IsInitialized { get; private set; }
public void Initialize() => IsInitialized = true;
}
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
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
There are scenarios where manual control over the creation process is required, such as:
- When additional initialization logic is needed
- When complex construction steps are required
- When specific object states need to be set during creation
Important
The method Inject()
cannot be used outside of the binding setup.
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;
}
public IService MyService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
IDependency transientIDependency1;
// Some logic for creating an instance:
Dependency localDependency95 = new Dependency();
localDependency95.Initialize();
transientIDependency1 = localDependency95;
return new Service(transientIDependency1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Composition ..> Service : IService MyService
IDependency *-- Dependency : Dependency
Service *-- IDependency : IDependency
namespace Pure.DI.UsageTests.Basics.FactoryScenario {
class Composition {
<<partial>>
+IService MyService
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}