This example illustrates dependency injection with parameterized factory functions using Pure.DI, where dependencies are created with runtime-provided arguments. The scenario features a service that generates dependencies with specific IDs passed during instantiation.
using Shouldly;
using Pure.DI;
using System.Collections.Generic;
DI.Setup(nameof(Composition))
.Bind().To<Dependency>()
.Bind().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
var dependencies = service.Dependencies;
dependencies.Count.ShouldBe(2);
dependencies[0].Id.ShouldBe(33);
dependencies[1].Id.ShouldBe(99);
interface IDependency
{
int Id { get; }
}
class Dependency(int id) : IDependency
{
public int Id { get; } = id;
}
interface IService
{
IReadOnlyList<IDependency> Dependencies { get; }
}
class Service(Func<int, IDependency> dependencyFactoryWithArgs): IService
{
public IReadOnlyList<IDependency> Dependencies { get; } =
[
dependencyFactoryWithArgs(33),
dependencyFactoryWithArgs(99)
];
}
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
Key components:
Dependency
class accepts an int id constructor argument, stored in itsId
property.Service
receivesFunc<int, IDependency>
delegate, enabling creation of dependencies with dynamic values.Service
creates two dependencies using the factory – one with ID33
, another with ID99
.
Delayed dependency instantiation:
- Injection of dependencies requiring runtime parameters
- Creation of distinct instances with different configurations
- Type-safe resolution of dependencies with constructor arguments
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
{
int overInt320;
Func<int, IDependency> perBlockFunc1;
var localLockObject97 = new Object();
Func<int, IDependency> localFactory98 = new Func<int, IDependency>((int localArg13) =>
{
lock (localLockObject97)
{
overInt320 = localArg13;
IDependency localValue99 = new Dependency(overInt320);
return localValue99;
}
});
perBlockFunc1 = localFactory98;
return new Service(perBlockFunc1);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Dependency --|> IDependency
Service --|> IService
Composition ..> Service : IService Root
Dependency *-- Int32 : Int32
Service o-- "PerBlock" FuncᐸInt32ˏIDependencyᐳ : FuncᐸInt32ˏIDependencyᐳ
FuncᐸInt32ˏIDependencyᐳ *-- Dependency : IDependency
namespace Pure.DI.UsageTests.Basics.InjectionOnDemandWithArgumentsScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency(Int32 id)
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(FuncᐸInt32ˏIDependencyᐳ dependencyFactoryWithArgs)
}
}
namespace System {
class FuncᐸInt32ˏIDependencyᐳ {
<<delegate>>
}
class Int32 {
<<struct>>
}
}