StructureMap IOC 命名实例帮助
StructureMap IOC 存在问题。我希望检索基于标签或名称实现相同接口的对象的不同具体实现。
internal static class InstanceHelper
{
internal enum Taxonomy
{
Foo,
Bar
}
static InstanceHelper()
{
// Initialize the container
ObjectFactory.Initialize(x =>
{
x.For<IMyInterface>().Use<ObjectA>().Named(Taxonomy.Foo.ToString());
x.For<IMyInterface>().Use<ObjectB>().Named(Taxonomy.Bar.ToString());
});
}
internal static IMyInterface GetInstanceByTaxonomy(Taxonomy taxonomy)
{
// this raises an exception
ObjectFactory.GetNamedInstance<IMyInterface>(taxonomy.ToString());
}
}
在这方面文档不太好,似乎所有示例都已弃用...使用版本 2.6.1.0
谢谢。
Having a problem with StructureMap IOC. I wish to retrieve different concrete implementations of objects that implement the same interface based on labels or names.
internal static class InstanceHelper
{
internal enum Taxonomy
{
Foo,
Bar
}
static InstanceHelper()
{
// Initialize the container
ObjectFactory.Initialize(x =>
{
x.For<IMyInterface>().Use<ObjectA>().Named(Taxonomy.Foo.ToString());
x.For<IMyInterface>().Use<ObjectB>().Named(Taxonomy.Bar.ToString());
});
}
internal static IMyInterface GetInstanceByTaxonomy(Taxonomy taxonomy)
{
// this raises an exception
ObjectFactory.GetNamedInstance<IMyInterface>(taxonomy.ToString());
}
}
Documentation is not to good in this regard, seems like all the examples out there are deprecated... using version 2.6.1.0
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
AddInstances
方法添加您的命名实例这对我有用,但是我确实同意文档有时在版本之间可能会产生很大的误导,并且我第一次花了一段时间才明白这一点。
Thy using the
AddInstances
method to add your named instancesThis works for me, however I do agree the documentation can sometimes be quite misleading between versions and it took me a while to get to this the first time around.
原始方式和 amarsuperstar 的方式似乎都有效,问题在于类“ObjectA”的范围已被声明为内部,以及内部构造函数。我将其更改为 Public,现在 IOC 可以看到它...StructureMap 程序集中发生了逆反射。呵呵,我真傻。
Both the original way and amarsuperstar's way appears to actually work, the problem was that the scope of class 'ObjectA' had been declared as internal, along with an internal constructor. I changed this to Public and now IOC can see it... inverse reflection is happening within the StructureMap assembly. Doh silly me.