简体   繁体   English

IoC / DI:当存在同一接口的多个实现时,如何注入特定实例

[英]IoC/DI: How to inject specific instance when there are multiple implementations of same interface

Assume we have two implementations of IService interface: 假设我们有两个IService接口的实现:

public interface IService { }

public class Service1 : IService { }

public class Service2 : IService { }

Then we have two classes which are depended on IService : 然后,我们有两个依赖于IService

public class Consumer1
{
    public Consumer1(IService svc) { }
}

public class Consumer2
{
    public Consumer2(IService svc) { }
}

Now, how can I inject Service1 into Consumer1 and Service2 into Consumer2 using Simple Injector as dependency container and without using a factory or separate interfaces? 现在,如何使用Simple Injector作为依赖关系容器而不使用工厂或单独的接口将Service1注入Consumer1Service2注入Consumer2

The generic pattern (not related to specific DI container) of how to archive this would be also great :) 如何存档的通用模式(与特定的DI容器无关)也很好:)

UPDATE UPDATE
I've already tried the context based injection with Simple Injector. 我已经尝试过使用简单注入器进行基于上下文的注入。 The documentation is very limited and I'm ended up with the following code: 该文档非常有限,最终得到以下代码:

container.RegisterConditional(
    typeof(IService),
    typeof(Service1),
    Lifestyle.Transient,
    c => c.Consumer.ImplementationType == typeof(Consumer1));

container.RegisterConditional(
    typeof(IService),
    typeof(Service2),
    Lifestyle.Transient,
    c => c.Consumer.ImplementationType == typeof(Consumer2));

container.Register<Consumer1>();
container.Register<Consumer2>();

but then I get an exception 但是我得到一个例外

The configuration is invalid. 配置无效。 Creating the instance for type Consumer1 failed. 创建类型为Consumer1的实例失败。 The constructor of type Consumer1 contains the parameter with name 'svc' and type IService that is not registered. Consumer1类型的构造函数包含名称为'svc'的参数和未注册的IService类型。 Please ensure IService is registered, or change the constructor of Consumer1. 请确保已注册IService,或更改Consumer1的构造函数。 1 conditional registration for IService exists that is applicable to IService, but its supplied predicate didn't return true when provided with the contextual information for Consumer1. 存在1个适用于IService的IService条件注册,但是在提供Consumer1的上下文信息时,其提供的谓词未返回true。

I've tried with different variations of predicate but without success... 我尝试了不同的谓词变体,但没有成功...

c => c.Consumer.Target.TargetType == typeof(Consumer1)
c => c.Consumer.ServiceType == typeof(Consumer1)

I'm unable to reproduce this problem with the latest version (3.1) of SI. 我无法使用最新版本的SI(3.1)重现此问题。 This test passes: 该测试通过:

[Test]
public void Test1()
{
    var container = new Container();

    container.RegisterConditional<IService, Service1>(
        c => c.Consumer.ImplementationType == typeof(Consumer1));
    container.RegisterConditional<IService, Service2>(
        c => c.Consumer.ImplementationType == typeof(Consumer2));
    container.Register<Consumer1>();
    container.Register<Consumer2>();

    container.Verify();

    var result1 = container.GetInstance<Consumer1>();
    var result2 = container.GetInstance<Consumer2>();

    Assert.IsInstanceOf<Service1>(result1.Svc);
    Assert.IsInstanceOf<Service2>(result2.Svc);
}

What version of SI are you using and are you sure you are using the correct instance of the container ? 您使用的是哪个版本的SI,您确定使用的是正确的container实例吗?

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

相关问题 如何在没有条件/上下文DI的情况下将不同的实现注入相同的接口? - How to inject different implementations to same interface without conditional/context DI? 一个接口与 DI 的多种实现 - Multiple implementations for one interface with DI 如何将一个接口的多个实现注入一个实现中 - How to Inject multiple Implementations of an Interface into one Implementation IOC / DI有2个实现相同接口的类 - IOC/DI with 2 classes that implement same interface 在多个构造函数参数中注入具有相同接口的不同实现 - Inject different implementations that have same interface in multiple constructor parameters IoC - 单一接口的多种实现支持 - IoC - Multiple implementations support for a single interface DI:不同控制器的相同接口的不同实现 - DI: Different implementations of the same interface for the different controllers 如何在 ASP NET Core 中使用 DI 注入特定的接口实现 - How to inject specific interface implementation using DI in ASP NET Core 如何在StructureMap DI和MVC 5中解析相同类型的多个实现 - How to resolve multiple implementations of the same type in StructureMap DI and MVC 5 如何从公共接口注入Spring.NET的多个实现? - How to inject with Spring.NET multiple implementations from common interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM