简体   繁体   English

使用ASP.NET Core DI注册接口的通用和特定实现

[英]Register both generic and specific implementation of an interface with ASP.NET Core DI

I've read the Armen Shimoon's article ASP.NET Core: Factory Pattern Dependency Injection and I've decided to solve my ASP.NET Core DI problem using the technique suggested by him. 我已经阅读了Armen Shimoon的文章ASP.NET Core:Factory Pattern Dependency Injection ,我决定使用他建议的技术来解决我的ASP.NET Core DI问题。 I've got a generic interface: 我有一个通用的界面:

public interface IItemRepository<out T> where T : BaseItem

and its generic implementation: 及其通用实现:

public class ItemRepository<T> : IItemRepository<T> where T : BaseItem

I register it with: 我将其注册为:

services.AddSingleton(typeof(IItemRepository<>), typeof(ItemRepository<>));

But for Currency I've got a specific implementation: 但是对于Currency,我有一个具体的实现:

public class CurrencyRepository : ItemRepository<Currency>

(Curency is of the BaseItem type.) What I want is to register (Curency属于BaseItem类型。)我想要的是注册

CurrencyRepository

for 对于

IItemRepository<Currency>

and

ItemRepository<T>

for all other items that implement BaseItem . 对于实现BaseItem的所有其他项目。 I created a factory class to accomplish this: 我创建了一个工厂类来完成这个:

public class ItemRepositoryFactory : IServiceFactory> where T : BaseItem
{
    private readonly ApplicationDbContext _context;

    public ItemRepositoryFactory(ApplicationDbContext context)
    {
        _context = context;
    }

    public ItemRepository Build()
    {
        if (typeof(T) == typeof(Currency))
            return new CurrencyRepository(_context) as ItemRepository;

        return new ItemRepository(_context);
    }
}

but I don't know how to register it with IServiceCollection . 但我不知道如何用IServiceCollection注册它。 Or maybe I'm not on the right way at all? 或者也许我不是正确的方式?

You can't register it explicitly. 您无法明确注册。 ASP.NET Core DI is meant to be simple and offer out of box DI/IoC experience and easy for other DI/IoC containers to plugin. ASP.NET Core DI意味着简单,提供开箱即用的DI / IoC体验,并且易于插入其他DI / IoC容器。

So the built-in IoC doesn't offer Auto-Registrations, Assembly scanning, Decorators or registration of all interfaces/sub types of the class. 因此,内置IoC不提供自动注册,装配扫描,装饰器或类的所有接口/子类型的注册。

For concrete implementations, there is a "workaround" 对于具体实现,有一个“解决方法”

services.AddScoped<IMyService,MyService>();
services.AddScoped<MyService>(provider => provider.GetService<IMyService>());

Then both MyService and IMyService inside a constructor would receive the same instance of the class. 然后构造函数中的MyServiceIMyService都将接收该类的相同实例。

But this doesn't work with open generics and is a manuell process. 但这不适用于开放式泛型并且是一个manuell过程。 For automatic discovery or registration you need a 3rd party IoC container, such as Autofac, StructureMap etc. 对于自动发现或注册,您需要第三方IoC容器,例如Autofac,StructureMap等。

But in your concrete sample it should be enough to register IItemRepository<Currency> inside your ConfigureServices method 但是在您的具体示例中,应该足以在ConfigureServices方法中注册IItemRepository<Currency>

services.AddScoped<IItemRepository<Currency>,CurrencyRepository>();

But actually the open generic should cover this case already, if you inject IItemRepository<Currency> into your services. 但实际上,如果将IItemRepository<Currency>注入到服务中,那么开放式泛型应该已经涵盖了这种情况。

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

相关问题 向ASP.NET Core依赖注入中的非通用实现注册通用接口 - Register a generic interface with a non generic implementation in ASP.NET Core Dependency Injection 如何在 ASP NET Core 中使用 DI 注入特定的接口实现 - How to inject specific interface implementation using DI in ASP NET Core 如何使用 .NET CORE DI 为同一接口注册多个实现 - How to register multiple implementation for same interface using .NET CORE DI ASP.NET Core DI:如果将作用域服务注册为服务类型和实现类型,则解析相同的实例 - ASP.NET Core DI: Resolve same instance if scoped service is registered both as service type and implementation type 在ASP.NET Core DI中添加实现而不是其接口 - Adding a implementation rather than its interface in ASP.NET Core DI 向ASP.NET Core的DI注册NLog目标(具有依赖项) - Register NLog target (with dependencies) with ASP.NET Core's DI ASP.NET 核心,基于通用存储库接口的通用 controller 实现 - ASP.NET Core, generic controller implementation based on generic repository interface 使用Base实现asp.net core 2.0将DI添加到DbContext - Adding DI to DbContext with Base implementation asp.net core 2.0 在asp.net核心DI中添加通用服务 - adding generic services in asp.net core DI ASP.NET Core 中的 Serilog DI,要注入哪个 ILogger 接口? - Serilog DI in ASP.NET Core, which ILogger interface to inject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM