Skip to content

Latest commit

 

History

History
122 lines (97 loc) · 2.88 KB

tag-on-a-constructor-argument.md

File metadata and controls

122 lines (97 loc) · 2.88 KB

Tag on a constructor argument

CSharp

The wildcards ‘*’ and ‘?’ are supported.

namespace Pure.DI.UsageTests.Advanced.TagOnConstructorArgScenario;


interface IDependency;

class AbcDependency : IDependency;

class XyzDependency : IDependency;

class Consumer<T>(IDependency myDep)
{
    public IDependency Dependency { get; } = myDep;
}

interface IService
{
    IDependency Dependency1 { get; }

    IDependency Dependency2 { get; }
}

class Service(
    IDependency dependency1,
    Consumer<string> consumer)
    : IService
{
    public IDependency Dependency1 { get; } = dependency1;

    public IDependency Dependency2 => consumer.Dependency;
}

DI.Setup(nameof(Composition))
    .Bind(Tag.OnConstructorArg<Service>("dependency1"))
    .To<AbcDependency>()
    .Bind(Tag.OnConstructorArg<Consumer<TT>>("myDep"))
    .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.Dependency1.ShouldBeOfType<AbcDependency>();
service.Dependency2.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(new AbcDependency(), new Consumer<string>(new XyzDependency()));
    }
  }
}

Class diagram:

classDiagram
	class Composition {
		<<partial>>
		+IService Root
	}
	class ConsumerᐸStringᐳ {
		+Consumer(IDependency myDep)
	}
	AbcDependency --|> IDependency
	class AbcDependency {
		+AbcDependency()
	}
	XyzDependency --|> IDependency
	class XyzDependency {
		+XyzDependency()
	}
	Service --|> IService
	class Service {
		+Service(IDependency dependency1, ConsumerᐸStringᐳ consumer)
	}
	class IDependency {
		<<interface>>
	}
	class IService {
		<<interface>>
	}
	ConsumerᐸStringᐳ *--  XyzDependency : IDependency
	Composition ..> Service : IService Root
	Service *--  AbcDependency : IDependency
	Service *--  ConsumerᐸStringᐳ : ConsumerᐸStringᐳ
Loading