using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind().As(Lifetime.Singleton).To<Facade>()
.Bind().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.DoSomething();
interface IDependency<T>
{
public void DoSomething();
}
class Dependency<T> : IDependency<T>
{
public void DoSomething()
{
}
}
class Facade
{
[Bind(typeof(IDependency<TT>))]
public IDependency<T> GetDependency<T>() => new Dependency<T>();
}
interface IService
{
public void DoSomething();
}
class Service(IDependency<int> dep) : IService
{
public void DoSomething() => dep.DoSomething();
}
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;
private readonly Lock _lock;
private Facade? _singletonFacade43;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonFacade43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonFacade43 is null)
{
_root._singletonFacade43 = new Facade();
}
}
}
IDependency<int> transientIDependency1;
Facade localInstance_1182D12744 = _root._singletonFacade43;
transientIDependency1 = localInstance_1182D12744.GetDependency<int>();
return new Service(transientIDependency1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Composition ..> Service : IService Root
Service *-- IDependencyᐸInt32ᐳ : IDependencyᐸInt32ᐳ
IDependencyᐸInt32ᐳ o-- "Singleton" Facade : Facade
namespace Pure.DI.UsageTests.Basics.BindAttributeForGenericTypeScenario {
class Composition {
<<partial>>
+IService Root
}
class Facade {
+Facade()
}
class IDependencyᐸInt32ᐳ {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependencyᐸInt32ᐳ dep)
}
}