using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependency.ShouldBe(service.Dependency);
interface IDependency;
class Dependency : IDependency;
interface IService
{
IDependency Dependency { get; }
}
class Service(Lazy<IDependency> dependency) : IService
{
public IDependency Dependency => dependency.Value;
}
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
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 Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Func<IDependency> perBlockFunc2 = new Func<IDependency>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
() =>
{
IDependency localValue108 = new Dependency();
return localValue108;
});
Lazy<IDependency> transientLazy1;
// Injects an instance factory
Func<IDependency> localFactory109 = perBlockFunc2;
// Creates an instance that supports lazy initialization
transientLazy1 = new Lazy<IDependency>(localFactory109, true);
return new Service(transientLazy1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Dependency --|> IDependency
Service --|> IService
Composition ..> Service : IService Root
Service *-- LazyᐸIDependencyᐳ : LazyᐸIDependencyᐳ
LazyᐸIDependencyᐳ o-- "PerBlock" FuncᐸIDependencyᐳ : FuncᐸIDependencyᐳ
FuncᐸIDependencyᐳ *-- Dependency : IDependency
namespace Pure.DI.UsageTests.BCL.LazyScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(LazyᐸIDependencyᐳ dependency)
}
}
namespace System {
class FuncᐸIDependencyᐳ {
<<delegate>>
}
class LazyᐸIDependencyᐳ {
}
}