gpt4 book ai didi

c# - Autofac:如何注册具有多个类型参数的泛型?将 IGenRepo 注册到 EFGenRepo

转载 作者:太空狗 更新时间:2023-10-30 01:03:07 25 4
gpt4 key购买 nike

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

下面是实现该接口(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){}
}

这是我正在使用的注册:

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

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

Using the generic type 'Domain.Concrete.EFGenRepo` requires 2 type arguments.

我没有经常使用 autofac,但是当我删除 TKey 泛型参数时,它一切正常并且出现错误消息:“使用泛型类型‘Domain.Concrete.EFGenRepo’需要 2 个类型参数"消失了......同时仍在使用 T 参数......有人可以告诉我如何正确设置它,而不是更改我的 IGenRepo 接口(interface)和 EFGenRepo 类。

最佳答案

尝试

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

关于c# - Autofac:如何注册具有多个类型参数的泛型?将 IGenRepo<T, TKey> 注册到 EFGenRepo<T, TKey>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com