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.
Try
RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With