Using Open Generics with Funq

I have the following interface:

public interface IWriter<in TId, TViewModel>

Why there are many different implementations, such as:

public class RedisWriter<TId, TViewModel> : IWriter<TId, TViewModel> 

I would like to add an instance of this class to the service constructor:

public class MyService : Service
{
    private readonly IWriter<Guid, OrderViewModel> _writer;

    public MyService(IWriter<Guid, OrderViewModel> writer)
    {
        _writer = writer;
    }
}

Note that type type parameters are closed where the instance is required.

I see no way to do this in Funq. Is it possible? Or do other IOCs allow such use?

+4
source share
1 answer

Funq does not support the creation of private implementations based on open generic registrations. With Funq, you have to explicitly register all closed shared implementations that applications require. For instance:

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

?

, DI , , Simple Injector, Autofac, Castle Windsor, Ninject StructureMap.

+2

All Articles