Question

I didn't understand that in all examples of IOC i could find, one interface can only have one implemented class in the dictionary of class mapping. Whatever DI or Service locator

It's a little bit stupid in my mind, no?

Was it helpful?

Solution

All the DI Containers I walk through in my book support multiple registrations of the same interface:

Castle Windsor

container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<Steak>());
container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<SauceBéarnaise>());

Autofac

builder.RegisterType<SauceBéarnaise>().As<IIngredient>();
builder.RegisterType<Steak>().As<IIngredient>();

Unity

container.RegisterType<IIngredient, Steak>();
container.RegisterType<IIngredient, SauceBéarnaise>("sauce");

There are more containers that support this; these are only examples. My book contains much more detailed examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top