Autofac:如何注册具有多个类型参数的泛型?将 IGenRepo<T, TKey> 注册到 EFGenRepo<T, TKey>
Autofac: How do you register a generic with multiple type arguments? Registering IGenRepo<T, TKey> with EFGenRepo<T, TKey>
我正在尝试构建一个通用存储库并使用 autofac 进行测试。我有以下界面:
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();
}
这里是实现该接口的 class:
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){}
}
这是我正在使用的注册:
cb.RegisterGeneric(typeof(EFGenRepo<>)).As(typeof(IGenRepo<>));
我在这一行得到的编译错误是:
Using the generic type 'Domain.Concrete.EFGenRepo` requires 2
type arguments.
我没有经常使用 autofac,但是当我删除 TKey 通用参数时它一切正常并且错误消息:"Using the generic type 'Domain.Concrete.EFGenRepo'requires 2 type arguments" 消失了......同时仍然使用 T 参数......有人可以告诉我如何正确设置它,而不是更改我的 IGenRepo 接口和 EFGenRepo class.
尝试
RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));
我正在尝试构建一个通用存储库并使用 autofac 进行测试。我有以下界面:
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();
}
这里是实现该接口的 class:
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){}
}
这是我正在使用的注册:
cb.RegisterGeneric(typeof(EFGenRepo<>)).As(typeof(IGenRepo<>));
我在这一行得到的编译错误是:
Using the generic type 'Domain.Concrete.EFGenRepo` requires 2 type arguments.
我没有经常使用 autofac,但是当我删除 TKey 通用参数时它一切正常并且错误消息:"Using the generic type 'Domain.Concrete.EFGenRepo'requires 2 type arguments" 消失了......同时仍然使用 T 参数......有人可以告诉我如何正确设置它,而不是更改我的 IGenRepo 接口和 EFGenRepo class.
尝试
RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));