0

我刚刚意识到,您不能像这样注册依赖注入:

services.AddScoped(typeof(IConcreteRepository<,>), typeof(DepartmentRepository));

DI 禁止使用泛型接口注册非泛型类。

public interface IConcreteRepository<TEntity, TId> : IRepository<TEntity, TId> where TEntity : class
{}

public abstract class BaseEntityFrameworkRepository<TEntity, TId> : IConcreteRepository<TEntity, TId> where TEntity : class where TId: struct
{}

public class DepartmentRepository : BaseEntityFrameworkRepository<Department, int>
{}

是否有任何 3rd 方 DI 库可以使注册成为DepartmentRepository可能IConcreteRepository<,>

无论库是什么,我都需要解决这个问题——如果可以使用默认的 .NET Core 3 框架,那就太好了,但是如果有任何 3rd 方库可以使这成为可能,我也可以。


编辑:

我应该提一下,像这样注册会引发以下异常:

An error occurred while accessing the Microsoft.Extensions.Hosting services. 

Error: Open generic service type 'CompanyName.ApplicationName.DAL.Repository.Contracts.IConcreteRepository`2[TEntity,TId]' requires registering an open generic implementation type. (Parameter 'descriptors').
4

1 回答 1

1

您应该将存储库注册为封闭的通用接口:

services.AddScoped<IConcreteRepository<Department, int>, DepartmentRepository>();
于 2020-06-05T16:14:30.130 回答