[英]Inject specific values to specific constructor parameters with Simple Injector
[英]Inject two specific interface implementations into constructor with Simple Injector
我有IConnector
接口。 SomeConnector
说,并有一些实现。 我的用例看起来像:
public class Worker : IWorker
{
public Worker(IConnector dataConnector, IConnector transactionConnector) {}
}
public class SomeConnector : IConnector
{
public SomeConnector(IConnectorContext connectorContext) {}
}
在Worker
构造函数中,我需要具有IConnector
两个实例。 不仅有两个实例,而且有两个使用它们自己的上下文创建的特定实例。 我该如何进行注册?
更新1
添加ConnectorContext
实现
public class SomeConnectorContext : IConnectorContext
{
public List<string> Types { get; }
public int DataTimeoutSeconds { get; }
public string Key { get; }
public string ConnectorName { get; }
public SomeConnectorContext(
List<string> types, int dataTimeoutSeconds, string key, string connectorName)
{
Types = types;
DataTimeoutSeconds = dataTimeoutSeconds;
Key = key;
ConnectorName = connectorName;
}
}
更新2
实际上,我需要某种基于config的条件注册。 例如:
switch (workerType)
{
// the following code is obviously incorrect, because I
// don't know how make registrations properly in this case.
case "worker1":
//create context for dataConnector based on config.
Container.Register<IConnectorContext>(new SomeConnectorContext(...));
//this is dataConnector. and should use dataConnector context.
Container.Register<IConnector, SomeConnector>();
//create context for transactionConnector based on config.
Container.Register<IConnectorContext>(new SomeConnectorContext(...));
//this is transactionConnector. and should use transactionConnector context.
Container.Register<IConnector, SomeConnector>();
Container.Register<IWorker, Worker1>();
break;
//in the following case Worker2 needs only one Connector
case "worker2":
//create context for allPurposeConnector based on config.
Container.Register<IConnectorContext>(new SomeConnectorContext(...));
//this is allPurposeConnector. and should use allPurposeConnector context.
Container.Register<IConnector, SomeConnector>();
Container.Register<IWorker, Worker2>();
break;
}
Update3添加workerType分配示例。
workerType是一个配置值。 例如,可以这样设置: workerType = Properties.Settings.Default.WorkerType;
我看到2个选项。
您可以使用lambda手动关联您的依赖项。 这意味着您仅注册IWorker
并手动构建它及其依赖项。 这将禁用自动装配,但是会产生非常简单的代码,如以下示例所示:
switch (workerType)
{
case "worker1":
var context1 = new SomeConnectorContext(...);
var context2 = new SomeConnectorContext(...);
Container.Register<IWorker>(() => Worker1(
new SomeConnector(context1),
new SomeConnector(context2)));
break;
case "worker2":
var context1 = new SomeConnectorContext(...);
Container.Register<IWorker>(() => Worker1(
new SomeConnector(context1)));
break;
}
您的第二个选择是将SomeConnector
注册为条件注册。 这使您既SomeConnector
注册SomeConnector
,也可以将它们链接到它们相应的构造函数参数。 这使IWorker
可以自动连接,但确实会导致更复杂的注册,如下所示:
switch (workerType)
{
case "worker1":
var context1 = new SomeConnectorContext(...);
Container.RegisterConditional<SomeConnector>(
Lifestyle.Transient.CreateRegistration(
() => new SomeConnector(context1), container),
c => c.Consumer.Target.Name == "dataConnector");
var context2 = new SomeConnectorContext(...);
Container.RegisterConditional<SomeConnector>(
Lifestyle.Transient.CreateRegistration(
() => new SomeConnector(context2), container),
c => c.Consumer.Target.Name == "transactionConnector");
Container.Register<IWorker, Worker1>();
break;
case "worker2":
Container.Register<IConnectorContext>(new SomeConnectorContext(...));
Container.Register<IConnector, SomeConnector>();
Container.Register<IWorker, Worker2>();
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.