All properties or fields marked with the required keyword automatically become injected dependencies.
interface IDependency;
class Dependency : IDependency;
interface IService
{
string Name { get; }
IDependency Dependency { get; }
}
class Service : IService
{
public required string ServiceNameField;
public string Name => ServiceNameField;
// The required property will be injected automatically
// without additional effort
public required IDependency Dependency { get; init; }
}
DI.Setup(nameof(Composition))
.Arg<string>("name")
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition(name: "My Service");
var service = composition.Root;
service.Dependency.ShouldBeOfType<Dependency>();
service.Name.ShouldBe("My Service");
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
private readonly string _argName;
[OrdinalAttribute(10)]
public Composition(string name)
{
_argName = name ?? throw new ArgumentNullException(nameof(name));
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_argName = _root._argName;
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new Service() { ServiceNameField = _argName, Dependency = new Dependency() };
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService Root
Service o-- String : Argument "name"
Service *-- Dependency : IDependency
namespace Pure.DI.UsageTests.Basics.RequiredPropertiesOrFieldsScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service()
+String ServiceNameField
+IDependency Dependency
}
}
namespace System {
class String {
}
}