Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 735
Kip ei Avatar asked Jun 03 '15 12:06

Kip ei


1 Answers

Try

RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));
like image 79
max Avatar answered Sep 22 '22 14:09

max