I have the following registrations prior to the one my question is about:
container.Register<ISettings>();
container.Register<Connection1>();
container.Register<Connection2>();
Connection1 and Connection2 both implement an the interface IConnection. To simplify my case ISettings just contains a property UseConnection1.
What I want is to register a mapping or delegate for service type IConnection that expects ISettings as a dependency and returns either Connection1 or Connection2 as its implementation type.
RegisterMapping itself does not have such capabilities due to the fact, that one can't use any depending services to specify the mapping dynamically.
RegisterDelegate on the other hand has the capabilities to get a specific depending service, but expects the user to return the implementation itself, not its type, like that:
container.RegisterDelegate<ISettings, IResolver, IConnection>((settings, resolver) =>
{
var serviceType = settings.UseConnection1 ? typeof(Connection1) : typeof(Connection2);
return resolver.Resolve<IConnection>(serviceType);
});
As seen, that implementation would be got from the IResolver which is far from perfect, as described in the DryIoc wiki.
What I wish to do is something like this:
container.RegisterSomething<IConnection>((ISettings settings) => settings.UseConnection1 ? typeof(Connection1) : typeof(Connection2));
Is this possible in any way with DryIoc?