StructureMap IOC 命名实例帮助

发布于 2024-09-25 09:07:12 字数 807 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2024-10-02 09:07:12

您使用 AddInstances 方法添加您的命名实例

ObjectFactory.Initialize(x =>
{
    x.For<IMyInterface>().AddInstances(i =>
    {
        i.Type<ObjectA>().Named("Foo");
        i.Type<ObjectB>().Named("Bar");
    });
});

这对我有用,但是我确实同意文档有时在版本之间可能会产生很大的误导,并且我第一次花了一段时间才明白这一点。

Thy using the AddInstances method to add your named instances

ObjectFactory.Initialize(x =>
{
    x.For<IMyInterface>().AddInstances(i =>
    {
        i.Type<ObjectA>().Named("Foo");
        i.Type<ObjectB>().Named("Bar");
    });
});

This 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.

空心↖ 2024-10-02 09:07:12

原始方式和 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文