繁体   English   中英

Autofac:如何使用多个类型参数注册泛型?使用EFGenRepo <T,TKey>注册IGenRepo <T,TKey>

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

我正在尝试构建一个通用的repo并使用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();
}

这是实现该接口的类:

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<>));

我在这一行得到的编译错误是:

使用泛型类型“Domain.Concrete.EFGenRepo”需要2个类型参数。

我没有经常使用autofac,但是当我删除TKey泛型参数时,一切正常并且错误消息:“ 使用泛型类型'Domain.Concrete.EFGenRepo需要2个类型参数 ”已经消失了......使用T参数...有人可以告诉我如何正确设置它,更喜欢不改变我的IGenRepo接口和EFGenRepo类。

尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM