简体   繁体   English

通用接口、实现和依赖注入——实现类型不能转换为服务类型

[英]Generic Interface, Implementation and Dependency Injection - Implementation Type Can't Be Converted To Service Type

I have the following generics (code removed for brevity, I don't think its necessary for the question):我有以下泛型(为简洁起见,删除了代码,我认为这个问题没有必要):

// ## Entity interface

public interface IEntity<TPrimaryKey>
{
    TPrimaryKey Id { get; set; }
}

// ## Entity implementations

public class Entity<TPrimaryKey> : IEntity<TPrimaryKey> { ... }

public class Entity : Entity<string> { ... }

// ## Repo interfaces

public interface IAsyncRepository<TPrimaryKey, TEntity>
    where TEntity : Entity<TPrimaryKey> { ... }

public interface IAsyncRepository<TEntity> : IAsyncRepository<string, TEntity>
    where TEntity : Entity<string> {...}

// ## Repo implementations

public class AsyncRepository<TPrimaryKey, TEntity>
    : IAsyncRepository<TPrimaryKey, TEntity>
        where TEntity : Entity<TPrimaryKey> { ... }

public class AsyncRepository<TEntity> : AsyncRepository<string, TEntity>
    where TEntity : Entity { ... }

I then dependency-inject the AsyncRepositories as follows:然后我依赖注入AsyncRepositories如下:

services.AddScoped(typeof(IAsyncRepository<>), typeof(AsyncRepository<>));
services.AddScoped(typeof(IAsyncRepository<,>), typeof(AsyncRepository<,>));

However, when I attempt to inject into my razor page using @inject IAsyncRepository<Account> accountRepository , I get an error saying:但是,当我尝试使用@inject IAsyncRepository<Account> accountRepository注入我的razor页面时,我收到一条错误消息:

System.ArgumentException: Implementation type 'AsyncRepository`1[Account]' can't be converted to service type 'IAsyncRepository`1[Account]' System.ArgumentException:无法将实现类型“AsyncRepository`1[Account]”转换为服务类型“IAsyncRepository`1[Account]”

But, if I change the declaration of the final AsyncRepository class as follows then it works:但是,如果我按如下方式更改最终AsyncRepository类的声明,则它可以工作:

public class AsyncRepository<TEntity> : IAsyncRepository<TEntity>
    where TEntity : Entity

Unfortunately I now have code duplication as I need to re-implement the interface.不幸的是,我现在有代码重复,因为我需要重新实现接口。

Is there any way around this?有没有办法解决?

Your repository must implement IAsyncRepository<TEntity> : update your AsyncRepository<TEntity> declaration with :您的存储库必须实现IAsyncRepository<TEntity> :使用以下内容更新您的AsyncRepository<TEntity>声明:

public class AsyncRepository<TEntity> : AsyncRepository<string, TEntity>,
IAsyncRepository<TEntity>
where TEntity : Entity { ... }

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

相关问题 ArgumentException:无法将实现类型“UserService”转换为服务类型“IUserService” - ArgumentException: Implementation type 'UserService' can't be converted to service type 'IUserService' 依赖注入提供通用服务接口最具体实现的机制 - Mechanism for Dependency Injection to Provide the Most Specific Implementation of a Generic Service Interface C#依赖注入多个相同泛型类型的实现 - C# dependency injection multiple implementation of the same Generic Type 使用泛型类型将实现强制转换为接口 - Casting implementation to the interface with generic type 具有指定类型的接口的通用实现 - Generic Implementation of interface with specified type 使用依赖注入时如何将通用接口解析为通用实现? - How to resolve generic interface to generic implementation when using dependency injection? 将具体实现类型转换为通用接口类型 - Convert concrete implementation type to generic interface type 返回具有不同泛型类型的泛型接口实现 - Return generic interface implementation with different generic type 具有两种类型 arguments 的通用接口的依赖注入 - Dependency injection for generic interface with two type arguments 通用接口实现:通用返回类型和通用输入类型 - Generic interface implementation: Generic return type and generic input type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM