Autofac: How do you register a generic type with multiple type arguments? Register IGenRepo <T, TKey> with EFGenRepo <T, TKey>

I am trying to create a shared 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 a class that implements this 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 I use registration:

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

The compilation error I get on this line is:

Using the generic type "Domain.Concrete.EFGenRepo" requires 2 type arguments.

autofac , TKey, : " " Domain.Concrete.EFGenRepo " 2 " ... T... - , , IGenRepo EFGenRepo.

+4
1

Try

RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));
+5

All Articles