This example shows how to create and initialize an instance manually in a simplified form. When you use a lambda function to specify custom instance initialization logic, each parameter of that function represents an injection of a dependency. Starting with C# 10, you can also put the Tag(...)
attribute in front of the parameter to specify the tag of the injected dependency.
interface IDependency
{
DateTimeOffset Time { get; }
bool IsInitialized { get; }
}
class Dependency : IDependency
{
public DateTimeOffset Time { get; private set; }
public bool IsInitialized { get; private set; }
public void Initialize(DateTimeOffset time)
{
Time = time;
IsInitialized = true;
}
}
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
DI.Setup(nameof(Composition))
.Bind("now datetime").To(_ => DateTimeOffset.Now)
// Injects Dependency and DateTimeOffset instances
// and performs further initialization logic
// defined in the lambda function
.Bind<IDependency>().To((
Dependency dependency,
[Tag("now datetime")] DateTimeOffset time) =>
{
dependency.Initialize(time);
return dependency;
})
.Bind().To<Service>()
// Composition root
.Root<IService>("MyService");
var composition = new Composition();
var service = composition.MyService;
service.Dependency.IsInitialized.ShouldBeTrue();
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
[OrdinalAttribute(20)]
public Composition()
{
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
}
public IService MyService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Dependency transientDependency1;
Dependency localDependency43 = new Dependency();
DateTimeOffset transientDateTimeOffset3 = DateTimeOffset.Now;
DateTimeOffset localTime44 = transientDateTimeOffset3;
localDependency43.Initialize(localTime44);
transientDependency1 = localDependency43;
return new Service(transientDependency1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService MyService
Service *-- Dependency : IDependency
Dependency *-- DateTimeOffset : "now datetime" DateTimeOffset
namespace Pure.DI.UsageTests.Basics.SimplifiedFactoryScenario {
class Composition {
<<partial>>
+IService MyService
}
class Dependency {
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}
namespace System {
class DateTimeOffset {
<<struct>>
}
}