Using LightInject for Open Generic Types

I have asked a question on StackOverflow regarding open generics and Funq and it looks like I am drawing a blank. As shown in the code sample I provided, I really want to be able to pass an IWriter<TId, TViewModel> into a class that inherits from ServiceStack.Service.

It looks like LightInject will wire up types with open generic parameters. Is there any way, if I additionally use LightInject just for this one use case, that I can pass in a resolved type into the Service constructor?

The answer on StackOverflow question is correct and why I didn’t leave an additional answer, Open Generic Types aren’t supported in Funq where each concrete Type must have it’s own registration, so the answer presents one solution:

c.Register<IWriter<Guid, OrderViewModel>>(c => 
    new RedisWriter<Guid, OrderViewModel>());
c.Register<IWriter<int, UserViewModel>>(c => 
    new RedisWriter<int, UserViewModel>());

Although I think it’s bad design to be injecting on open Generic Type dependencies like this, you could just register a single factory and create the types from that, e.g:

c.Register(c => new RedisWriter(c.Resolve<IRedisClientsManager>()));

Than in your Service use it to resolve whatever you need to:

public class MyServices : Service
     public RedisWriter RedisWriter { get; set; }

    public object Any(Request request)
        var redisOrderWriter = RedisWriter.Create<Guid,OrderViewModel>();

This way there’s no magic and resolving dependencies is still fast and accessed by key.

The IContainerAdapter is what lets you control how dependencies are resolved so you could create a custom implementation that attempts to resolve the dependency from LightInject or any other IOC. By default Funq will check the Adapter for the dependency first, if it doesn’t exist it will try to resolve it from itself.