Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic selection of interface implementation using IoC

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

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

Both classes AppleImpl and BananaImpl are implementation of the same interface, say IFruit.

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

like image 342
darcyy Avatar asked Jan 07 '16 07:01

darcyy


People also ask

What is dynamic interface implementation in Java?

Dynamic Interface Implementation. The IDynamicInterfaceImplementor interface defines a contract to create the type of the runtime generated proxy. It has only one method, CreateType which takes two Type parameters, the first one represents the interface to implement and the other one indicates the base class' type.

How to implement the IoC container using Autofac?

First of all create one Console application to implement the sample example and provide a reference of Autofac from the NuGet Package Manager. Here is the package. Once you provide a reference, you will find the following reference in the references section of the application. We are now ready to implement the IoC container using autofac.

Is it possible to have multiple implementations of an interface?

In fact, in ASP.NET Core, when you register multiple implementations for an interface, the constructor can be injected with a collection of that interface, which is all registered implementations. Of course, in the project, we absolutely can not write hard code like this, we want to select a specific implementation through the configuration file.

What happens when you inject a collection of interface implementations?

When you inject a collection of interface implementations, every single implementation will be instantiated. So if you only use one implementation in your application after the application started, please register the implementation according to the configuration in DI container (Startup.Configure).


1 Answers

This is the single most-asked question about Dependency Injection, and gets asked over and over again on StackOverflow.

In short, it is best to use patterns to solve runtime creation rather than trying to use the container for more than composing object graphs, which is all it is designed for.

There are several patterns that can be used for this, but among the best options are to use Abstract Factory, Strategy, or a combination of the two. The exact solution depends on how the instance will be used - use a factory if you will be needing several short-lived instances and want to discard them after use, or use a strategy if you need to use the instances over and over again in a loop without having to recreate them each time. The combination is a tradeoff between high performance and low memory consumption.

like image 178
NightOwl888 Avatar answered Nov 15 '22 00:11

NightOwl888