At any time, the default binding to the BCL type can be changed to your own:
using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind<IDependency[]>().To<IDependency[]>(_ =>
[new AbcDependency(), new XyzDependency(), new AbcDependency()]
)
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(3);
service.Dependencies[0].ShouldBeOfType<AbcDependency>();
service.Dependencies[1].ShouldBeOfType<XyzDependency>();
service.Dependencies[2].ShouldBeOfType<AbcDependency>();
interface IDependency;
class AbcDependency : IDependency;
class XyzDependency : IDependency;
interface IService
{
IDependency[] Dependencies { get; }
}
class Service(IDependency[] dependencies) : IService
{
public IDependency[] Dependencies { get; } = dependencies;
}
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
{
IDependency[] transient1 = [new AbcDependency(), new XyzDependency(), new AbcDependency()];
return new Service(transient1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Composition ..> Service : IService Root
Service *-- ArrayᐸIDependencyᐳ : ArrayᐸIDependencyᐳ
class ArrayᐸIDependencyᐳ {
<<array>>
}
namespace Pure.DI.UsageTests.BCL.OverridingBclBindingScenario {
class Composition {
<<partial>>
+IService Root
}
class IService {
<<interface>>
}
class Service {
+Service(ArrayᐸIDependencyᐳ dependencies)
}
}