The wildcards ‘*’ and ‘?’ are supported.
namespace Pure.DI.UsageTests.Advanced.TagOnMemberScenario;
interface IDependency;
class AbcDependency : IDependency;
class XyzDependency : IDependency;
interface IService
{
IDependency Dependency { get; }
}
class Service : IService
{
public required IDependency Dependency { init; get; }
}
DI.Setup(nameof(Composition))
.Bind().To<AbcDependency>()
.Bind(Tag.OnMember<Service>(nameof(Service.Dependency)))
.To<XyzDependency>()
.Bind<IService>().To<Service>()
// Specifies to create the composition root named "Root"
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependency.ShouldBeOfType<XyzDependency>();
Warning
Each potentially injectable argument, property, or field contains an additional tag. This tag can be used to specify what can be injected there. This will only work if the binding type and the tag match. So while this approach can be useful for specifying what to enter, it can be more expensive to maintain and less reliable, so it is recommended to use attributes like [Tag(...)]
instead.
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 Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new Service() { Dependency = new XyzDependency() };
}
}
}
Class diagram:
classDiagram
class Composition {
<<partial>>
+IService Root
}
XyzDependency --|> IDependency
class XyzDependency {
+XyzDependency()
}
Service --|> IService
class Service {
+Service()
+IDependency Dependency
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
Composition ..> Service : IService Root
Service *-- XyzDependency : IDependency