【发布时间】:2017-02-15 09:07:12
【问题描述】:
我正在尝试设置一个容器来注册一个在其构造函数中接受IDictionary<string, IInterface> 的类,因此在我的 IoC 中,我想知道如何获取命名实例(如果该功能在 SI 中可用)。我想要实现的一个例子是:
container.Register<IOtherInterface>(() =>
new OtherInterface(new Dictionary<string, IInterface>
{
{ "a", container.GetInstance<IInterface>("named-a") },
{ "b", container.GetInstance<IInterface>("named-b") },
});
有什么方法可以配置吗?
更新:
接口的实现是具有不同参数的同一个对象,这是我在 SI 文档https://simpleinjector.readthedocs.io/en/latest/howto.html#resolve-instances-by-key 中无法看到的示例。我想避免这样一个事实,即我必须为字典中的每个值实例化一个新接口(..with the relevant parameters...),这就是为什么我认为命名注册在 SI 中是可用的。
也许我的方法是错误的,我应该尝试以不同的方式建立依赖关系。
使用示例:
public class OtherInterface : IOtherInterface
{
private readonly IDictionary<string, IInterface> _interfces;
public OtherInterface(IDictionary<string, IInterface> interfaces)
{
_interfaces = interfaces;
}
public void DoSomething(MyRequest request)
{
if(_interfaces.ContainKey(request.SelectMyInterface))
{
_interfaces[request.SelectMyInterface].DoSpecificStuff();
}
}
}
我可以扩展接口 IInterface 并在此处应用策略模式,使用类似 Applies(string type) 的方法,但我过去曾在 Ninject 中使用字典方法。
【问题讨论】:
-
关于键控注册、字典和工厂here 的文档非常丰富。如果本文档没有回答您的问题,请更新您的问题并提供更多详细信息(不要忘记发表评论,以通知任何读者您的问题已更改)。
-
根据我的实际情况进行了更新。我已经阅读了有关键控注册的信息,但找不到与此特定案例相关的任何内容
-
OtherInterface如何确定何时使用named-a或named-b?它如何确定密钥a和b? -
在其公共方法调用中作为对象的属性传递
-
你能发布一些显示这个的代码吗?
标签: c# ioc-container simple-injector