I have the following:
public interface IConverter<TValue, TConverted> { } public interface IConverterProvider { IConverter<TValue, TConverted> GetConverter<TValue, TConverted>(); }
With an example of binding when setting up:
Bind<IConverter<System.Int32, System.String>>().To<Int32ToStringConverter>(); Bind<IConverter<System.Guid, System.String>>().To<GuidToStringConverter>();
So, I have a set of fixed converters and duplicates.
[Question] My question is, how do I go about implementing IConverterProvider and introduce a dictionary of available bindings mapped to singles? Or, in other words, how to avoid the locator service pattern at run time.
Currently, I just use the NInject kernel to resolve it every time, but I consider it an anti-pattern. I would like it to be like this:
public class ConverterProvider : IConverterProvider { private Dictionary<Type, object> _converters; public ConverterProvider(Dictionary<Type, object> converters) { _converters = converters; } public IConverter<TValue, TConverted> GetConverter<TValue, TConverted>() { var fullTypeResolve = typeof (IConverter<,>).MakeGenericType(typeof (TValue), typeof (TConverted)); return _converters.Where(x => x.Key == fullTypeResolve).Select(x=>x.Value).Cast<IConverter<TValue, TConverted>>().Single(); } }
But this really requires that I can resolve and get a list of all IConverter <,> from the dependency input kernel, and my previous attempts to do this from NInject were not successful.