Dynamic choice of interface implementation using IoC

I have a situation where the implementation of an interface is determined at runtime. For example, I check the string and then determine which subclass to use, without IoC, it looks like this:

if (fruitStr == "Apple")
{
    new AppleImpl().SomeMethod();
}
else
{
    new BananaImpl().SomeMethod();
}

Both classes AppleImpland BananaImplrepresent the implementation of the same interface, for example IFruit.

How can this be done with IoC / Dependency Injection, especially in Castle Windsor ?

+4
source share
1 answer

Injection Dependency, StackOverflow.

, , , .

, Factory, Strategy . , - factory, , . .

+6

Source: https://habr.com/ru/post/1623211/


All Articles