-
Notifications
You must be signed in to change notification settings - Fork 10
Multi resolution
Peter Csajtai edited this page Nov 6, 2018
·
13 revisions
When you want to register multiple implementations of a service, you can differentiate them with a given name:
container.Register<ICreature, Dwarf>("Bruenor");
container.Register<ICreature, Drow>("Drizzt");
container.Register<ICreature, Human>("Catti-brie");
container.Register<ICreature, Barbarian>("Wulfgar");
container.Register<ICreature, Halfling>("Regis");
Then you can get all of your services by calling the ResolveAll()
method:
var companionsOfTheHall = container.ResolveAll<ICreature>();
You can also get all implementations injected into constructor/member/method:
class CompanionsOfTheHall
{
[Dependency]
public IReadOnlyCollection<ICreature> Members { get; set; }
public CompanionsOfTheHall(IEnumerable<ICreature> members)
{ }
//or
public CompanionsOfTheHall(ICreature[] members)
{ }
//or
public CompanionsOfTheHall(ICollection<ICreature> members)
{ }
//etc...
}
You can also specify in which form you want to get the requested collection:
var companions = container.Resolve<IEnumerable<ICreature>>();
//or
var companions = container.Resolve<ICreature[]>();
//or
var companions = container.Resolve<IReadOnlyCollection<ICreature>>();
//etc...
- 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