문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top