To use dependency implementation for a method, simply add the Ordinal attribute to that method, specifying the sequence number that will be used to define the call to that method:
interface IDependency;
class Dependency : IDependency;
interface IService
{
IDependency? Dependency { get; }
}
class Service : IService
{
// The Ordinal attribute specifies to perform an injection,
// the integer value in the argument specifies
// the ordinal of injection
[Ordinal(0)]
public void SetDependency(IDependency dependency) =>
Dependency = dependency;
public IDependency? Dependency { get; private set; }
}
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("MyService");
var composition = new Composition();
var service = composition.MyService;
service.Dependency.ShouldBeOfType<Dependency>();
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
[OrdinalAttribute(20)]
public Composition()
{
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
}
public IService MyService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Service transientService0 = new Service();
transientService0.SetDependency(new Dependency());
return transientService0;
}
}
}
Class diagram:
classDiagram
class Composition {
<<partial>>
+IService MyService
}
Service --|> IService
class Service {
+Service()
+SetDependency(IDependency dependency) : Void
}
Dependency --|> IDependency
class Dependency {
+Dependency()
}
class IService {
<<interface>>
}
class IDependency {
<<interface>>
}
Composition ..> Service : IService MyService
Service *-- Dependency : IDependency