Autofac: How do you register a generic with multiple type arguments? Registering IGenRepo with EFGenRepo

.net-4.5.2 asp.net-mvc-5.2 autofac c# entity-framework-6

Question

I am trying to build a generic repo and use autofac for testing. I have the following interface:

public interface IGenRepo<T, TKey> where T : class
{
    IQueryable<T> Items { get; }
    T find(TKey pk);
    RepoResult delete(TKey pk);
    RepoResult create(T item);
    RepoResult update(T item);
    RepoResult save();
}

And here is the class that implements that interface:

public class EFGenRepo<T, TKey> : IGenRepo<T, TKey> where T : class
{
    private PortalEntities context = new PortalEntities();

    public IQueryable<T> Items { get { return context.Set<T>().AsQueryable<T>(); } }

    public T find(TKey pk){}
    public RepoResult delete(TKey pk){}
    public RepoResult create(T item){}
    public RepoResult update(T item){}
    public RepoResult save(){}
    private RepoResult save(T item){}
}

Here's the registration I am using:

cb.RegisterGeneric(typeof(EFGenRepo<>)).As(typeof(IGenRepo<>));

The compile error I get on this line is:

Using the generic type 'Domain.Concrete.EFGenRepo` requires 2 type arguments.

I haven't used autofac a lot, but when I remove the TKey generic parameter it all works fine and the error message:"Using the generic type 'Domain.Concrete.EFGenRepo'requires 2 type arguments" is gone...while still using the T parameter...Can somebody tell me how to set it up correctly, prefering not to change my IGenRepo interface and EFGenRepo class.

1
4
6/4/2015 1:00:24 PM

Accepted Answer

Try

RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));
6
6/3/2015 12:09:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow