Skip to content

Conventional resolution

Peter Csajtai edited this page Dec 28, 2019 · 8 revisions

When you enable the conventional resolution with the TreatParameterAndMemberNameAsDependencyName() configuration option, the container will treat the member and method parameter names as their dependency identifier.

Usage

First, you have to enable the conditional resolution:

var container = new StashboxContainer(c => c.TreatParameterAndMemberNameAsDependencyName());

Parameters

Let's say, you have a service class like this:

class Drizzt
{
    public Drizzt(IWeapon icingdeath, IWeapon twinkle)
    { }
}

Then, if you register your weapon classes with names like this:

container.Register<Drizzt>()
         .Register<IWeapon, Icingdeath>(c => c.WithName("icingdeath"))
         .Register<IWeapon, Twinkle>(c => c.WithName("twinkle"));

Then, when you resolve Drizzt, the container will choose that registration which has a matching name to the related parameter.

Members

You can achieve the same as above with member injection as well.

With a class like this:

class Drizzt
{
    public IWeapon Icingdeath { get; set; }
    public IWeapon Twinkle { get; set; }
}

And with the following registration part:

container.Register<Drizzt>(c => c.WithAutoMemberInjection())
         .Register<IWeapon, Icingdeath>(c => c.WithName("Icingdeath"))
         .Register<IWeapon, Twinkle>(c => c.WithName("Twinkle"));

The container will choose the appropriate weapon by matching the property names to the dependency names.