Repository navigation
Conditional resolution
Peter Csajtai edited this page Mar 14, 2017
·
19 revisions
If you want to specify in which cases you want to inject some special dependencies, you can use the conditional resolution feature of Stashbox.
class Wulfgar : IBarbarian
{
[Dependency]
public IWeapon Weapon { get; set; }
}
container.PrepareType<IWeapon, AegisFang>().WhenDependantIs<Wulfgar>().Register();The constraint above indicates that Stashbox will choose the
AegisFangimplementation of theIWeaponinterface whenWulfgaris being resolved.
If you set an attribute filter for your registration, Stashbox will inject it only, if the dependency is decorated with that attribute:
class NeutralGoodAttribute : Attribute { }
class ChaoticEvilAttribute : Attribute { }
class Drizzt : IDrow
{
[Dependency, NeutralGood]
public IPatron Patron { get; set; }
}
class Yvonnel : IDrow
{
[Dependency, ChaoticEvil]
public IPatron Patron { get; set; }
}
container.PrepareType<IPatron, Mielikki>().WhenHas<NeutralGood>().Register();
container.PrepareType<IPatron, Lolth>().WhenHas<ChaoticEvil>().Register();You can specify custom filters yourself if you decorate your registration with When(...) calls e.g.:
class Drizzt : IDrow
{
public IPatron Patron { get; set; }
}
class Yvonnel : IDrow
{
public IPatron Patron { get; set; }
}
container.PrepareType<IPatron, Mielikki>().When(t => t.ParentType.Equals(typeof(Drizzt))).Register();
container.PrepareType<IPatron, Lolth>().When(t => t.ParentType.Equals(typeof(Yvonnel))).Register();- Service registration
- Factory registration
- Assembly registration
- Composition
- Fluent registration api
- Service resolution
- Resolution by attributes
- Conventional resolution
- Delegate resolution
- Conditional resolution
- Multi resolution
- Lifetimes
- Generics
- Generic wrappers
- Decorators
- Resolvers
- Scopes
- Container configuration
- Container diagnostics
- Exceptions