[英]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
注入Consumer1
和Service2
注入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.