Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top