I usually make a factory (or provider as you have here) with something like this:
public class ConverterFactory : IConverterFactory
{
private readonly IResolutionRoot resolutionRoot;
public ConverterFactory(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public IConverter<TValue, TConverted> CreateConverter()
{
Type converterType = typeof(IConverter<,>).MakeGenericType(typeof(TValue), typeof(TConverted));
return this.resolutionRoot.Get(converterType);
}
}
I know we're not really supposed to be using Ninject to create things at runtime but really sometimes it's unavoidable. To get the singleton behaviour, just define InSingletonScope()
as normal.
If the types weren't generic then you can inject all implementations of the interface instead of the IResolutionRoot
then pick one at runtime. I have a parser factory a bit like this:
public class ParserFactory : IParserFactory
{
private readonly IEnumerable<IParser> parsers;
public ParserFactory(IEnumerable<IParser> parsers)
{
this.parsers = parsers;
}
public IParser CreateParser(string someInput)
{
foreach (var parser in this.parsers)
{
if (parser.CanParse(someInput))
{
return parser;
}
}
}
}
Ninject will automatically inject an IEnumerable
containing all concrete implementations of the interface, so adding new implementations is as simple as adding a mapping. I don't know how to do that with generic types though since you can't say IEnumerable<IConverter<,>>
.