Specifying T[]
as the injection type allows instances from all bindings that implement the T
type to be injected.
using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<AbcDependency>()
.Bind<IDependency>(2).To<XyzDependency>()
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(2);
service.Dependencies[0].ShouldBeOfType<AbcDependency>();
service.Dependencies[1].ShouldBeOfType<XyzDependency>();
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
In addition to arrays, other collection types are also supported, such as:
- System.Memory
- System.ReadOnlyMemory
- System.Span
- System.ReadOnlySpan
- System.Collections.Generic.ICollection
- System.Collections.Generic.IList
- System.Collections.Generic.List
- System.Collections.Generic.IReadOnlyCollection
- System.Collections.Generic.IReadOnlyList
- System.Collections.Generic.ISet
- System.Collections.Generic.HashSet
- System.Collections.Generic.SortedSet
- System.Collections.Generic.Queue
- System.Collections.Generic.Stack
- System.Collections.Immutable.ImmutableArray
- System.Collections.Immutable.IImmutableList
- System.Collections.Immutable.ImmutableList
- System.Collections.Immutable.IImmutableSet
- System.Collections.Immutable.ImmutableHashSet
- System.Collections.Immutable.ImmutableSortedSet
- System.Collections.Immutable.IImmutableQueue
- System.Collections.Immutable.ImmutableQueue
- System.Collections.Immutable.IImmutableStack And of course this list can easily be supplemented on its own.
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
{
return new Service(new IDependency[2] { new AbcDependency(), new XyzDependency() });
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
AbcDependency --|> IDependency
XyzDependency --|> IDependency : 2
Service --|> IService
Composition ..> Service : IService Root
Service *-- ArrayᐸIDependencyᐳ : ArrayᐸIDependencyᐳ
ArrayᐸIDependencyᐳ *-- AbcDependency : IDependency
ArrayᐸIDependencyᐳ *-- XyzDependency : 2 IDependency
class ArrayᐸIDependencyᐳ {
<<array>>
}
namespace Pure.DI.UsageTests.BCL.ArrayScenario {
class AbcDependency {
+AbcDependency()
}
class Composition {
<<partial>>
+IService Root
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(ArrayᐸIDependencyᐳ dependencies)
}
class XyzDependency {
+XyzDependency()
}
}