简体   繁体   English

DI:不同控制器的相同接口的不同实现

[英]DI: Different implementations of the same interface for the different controllers

I have two controllers:我有两个控制器:

namespace V1
{
    [ApiVersion("1.0")]
    public class SomeController : Controller
    {
        public SomeController(IProvider provider) { }
    }
}

namespace V2
{
    [ApiVersion("2.0")]
    public class SomeController : Controller
    {
        public SomeController(IProvider provider) { }
    }
}

And the following services:以及以下服务:

public interface IStorage { }
public interface IProvider { }
public class V1Storage : IStorage { }
public class V2Storage : IStorage { }
public class Provider : IProvider
{
    public Provider(IStorage storage) { }
}

For the first version of controller I need to use V1Storage , for the second - V2Storage .对于控制器的第一个版本,我需要使用V1Storage ,对于第二个版本 - V2Storage I know there is a way to replace DefaultControllerFactory but what is the best way to implement it?我知道有一种方法可以替换DefaultControllerFactory但实现它的最佳方法是什么?

Use AddControllersAsServices to help with this.使用AddControllersAsServices来帮助解决这个问题。 The documentation describes that this:该文档描述了这一点:

Registers discovered controllers as services in the IServiceCollection.将发现的控制器注册为 IServiceCollection 中的服务。

Here's a complete example:这是一个完整的例子:

void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddControllersAsServices();

    services.AddTransient(sp => new V1.SomeController(new Provider(new V1Storage())));
    services.AddTransient(sp => new V2.SomeController(new Provider(new V2Storage())));
}

By re-registering V1.SomeController and V2.SomeController after the call to AddControllersAsServices , you take control of exactly how they are constructed, passing in an IProvider implementation configured with the required IStorage implementation for each controller.通过在调用AddControllersAsServices之后重新注册V1.SomeControllerV2.SomeController ,您可以准确地控制它们的构造方式,传入一个IProvider实现,该实现配置为每个控制器所需的IStorage实现。

Andrew Lock describes this approach in Controller activation and dependency injection in ASP.NET Core MVC . Andrew Lock 在 ASP.NET Core MVC中的控制器激活和依赖注入中描述了这种方法。

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

相关问题 如何在没有条件/上下文DI的情况下将不同的实现注入相同的接口? - How to inject different implementations to same interface without conditional/context DI? 使用Autofac将接口的不同实现注入到不同的控制器中 - Injecting different implementations of an interface into different controllers with Autofac 使用ninject绑定同一接口的不同实现 - Bind different implementations of same Interface using ninject 子类继承相同接口的不同实现 - Child classes inheriting different implementations of the same interface 具有不同类型的接口的实现? - Implementations of an Interface with Different Types? 在多个构造函数参数中注入具有相同接口的不同实现 - Inject different implementations that have same interface in multiple constructor parameters 一个接口与 DI 的多种实现 - Multiple implementations for one interface with DI 为不同的实现提供相同类型的不同配置? - Supplying different configurations of the same type to different implementations? 减少针对不同接口实现的NUnit测试中的重复 - Reduce duplication in NUnit tests for different implementations of interface 在运行时将命令的不同实现注入命令 - Inject different implementations of an Interface to a command at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM