I want to let Ninject resolve an instance of T based on a specific enum input value.
I have read about Ninject's factory extension, but I couldn't find any example having the factory resolve a specific class based on an enum.
Each class derives from a base class and that derived class has several, different interfaces that Ninject also has to resolve.
For example this is how the interface should look like:
public interface IProcessFactory
T Create<T>(ProcessIndex processIndex) where T : BaseProcess;
How can this be achieved ?
Best How To :
There are several options available to implement AbstractFactory using DI (Ninject).
After analyzing the options, I came up with the solution provided by Mark Seemann, see http://blog.ploeh.dk/2012/03/15/ImplementinganAbstractFactory/
The Container Based Factory solution is the one I chose, because:
- Performance: on demand DI resolve on request, no instances loaded in the constructor
- Easy for refactor: when we want to replace the current DI framework (Ninject) to a much better performer with (almost or even better) featureset, the only place to change are the calls inside the factory and not in the NinjectModules/Composition Root.
See also at SO: Simple Injector:Factory classes that need to create classes with dependencies