通过具有命名注册依赖项的 xml 配置时的 Unity 解析问题

发布于 2024-10-07 05:45:41 字数 1338 浏览 0 评论 0原文

我有一个采用通用类型的接口

interface IIFace<T>

然后我有几个实现

public class IFaceImp1 : IIFace<MyObj> {}
public class IFaceImp2 : IIFace<MyObj> {}

我定义了一个视图模型,该视图模型被注入到该视图模型中,该接口的特定类型的对象...

public MainViewModel(IIFace<MyObj> scrapper) { }

然后我配置Unity来填充它

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

这工作正常 问题是,现在我想创建命名注册,以便稍后我可以通过名称解析它们...所以我命名了它,并首先创建了另一个注册,但这不起作用。我将问题简化为:

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

当尝试 Resolve<>("FirstImplementation") 时,这也不起作用。但是,如果我删除 name 属性并使用 Resolve<>() 重载,它就可以正常工作。 关于可能发生的事情有什么想法,有一个名字不起作用吗?

I have an interface which takes a generic type

interface IIFace<T>

Then i have several implementations

public class IFaceImp1 : IIFace<MyObj> {}
public class IFaceImp2 : IIFace<MyObj> {}

I defined a viewmodel which gets injected into it an object of this interface for a specific type...

public MainViewModel(IIFace<MyObj> scrapper) { }

And then i configure Unity to populate it

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

This works fine
The problem is, now i wanted to create named registrations, so that later i can resolve them by name... So I named it, and created another one first, bu t that didn't work. I reduced the problem to this :

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

Which also doesn't work... wh en trying to Resolve<>("FirstImplementation"). However, if I remove the name attribute and use the Resolve<>() overload, it works fine..
Any ideas about what could be happening, that having a name this doesn't work??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苏佲洛 2024-10-14 05:45:41

好的,我知道问题出在哪里了
我需要使用标签,在 xml 中构建我的对象模型
最终结果与此类似:

<containers>
    <container>

        <register type="ViewModelBase" mapTo="MainViewModel" name="MockMainViewModel">
          <constructor>
            <param name="imp">
              <dependency name="FirstImplementation" />
            </param>
          </constructor>
        </register>

        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

这样,此配置告诉 Unity,当使用 Resolve("MockMainViewModel") 调用时,实例化它,并使用指定的值解析(也通过依赖项)其构造函数中需要的参数名称(“第一次实现”)
我基本上不知道这个标签,但无论如何,这对于遇到同样问题并且不知道为什么解决方案无法自动工作以及什么是正确方法的人来说可能很有用。

Ok, i found out what was the problem
I needed to use the tag, building my object model in the xml
the end result is something similar to this :

<containers>
    <container>

        <register type="ViewModelBase" mapTo="MainViewModel" name="MockMainViewModel">
          <constructor>
            <param name="imp">
              <dependency name="FirstImplementation" />
            </param>
          </constructor>
        </register>

        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

This way, this configuration is telling Unity to, when called with Resolve("MockMainViewModel"), instantiate it, and resolve (through dependency too) the parameter that needs in its constructor, with the specified name ("FirstImplementation")
I was basically unaware of the tag, but anyway this may be useful to someone that comes up with the same problem and doesn't know why the resolution fails to work automatically, and what's the right approach.

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