【问题标题】:Unity IOC ConfigurationUnity IOC 配置
【发布时间】:2008-12-16 11:08:56
【问题描述】:

我有课

public class Broker
{
    public Broker(string[] hosts, string endPoint, string port, Type remoteType)
    {
    }
}

我想使用 Unity XML 配置进行配置,我可以使用 C# 中的代码对其进行配置,如下所示,其中“容器”是我的 Unity 容器

            container.Configure<InjectedMembers>()
                .ConfigureInjectionFor<Broker>("myBroker",
                                                           new InjectionConstructor(hosts, endPoint, port, new InjectionParameter(typeof(IMyBrokeredObject))));

它会很高兴使用正常的统一调用来解决

container.Resolve("myBroker");

但目前我的 xml 无法解析最终参数 IMyBrokeredObject,我得到一个解析异常,因为 Unity 正在尝试解析类型,只需注入类型,就像在上面的代码中那样。

有什么想法吗?

【问题讨论】:

    标签: configuration unity-container


    【解决方案1】:

    你是否在配置文件中定义了类型:

    <unity>
    <typeAliases>
      <typeAlias alias="IMyBrokeredObject" type="MyAssembly.IMyBrokeredObject, MyAssembly" />
    </typeAliases>
    <containers>
          <container>
            <types>
              <!-- Views -->
              <type type="IMyBrokeredObject" mapTo="MyAssembly.MyBrokeredObjectImplementation, MyAssembly" />
    

    【讨论】:

      【解决方案2】:

      但我的问题是 IMyBrokeredObject 没有可用的实现,在此背景中实际发生的是代理提供给定接口的远程对象,实际实现在其他地方。

      在代码中,我可以通过提供“InjectionParameter”让容器提供代理,但我无法在 xml 配置中找到如何执行此操作。

      它很棘手,因为我不希望容器提供接口的实例,而是按原样实际传递接口,“InjectionParameter”是一个值的存储,存储的值是在创建对象时提交的容器,原样。我正在寻找的是创建 InjectionParameter 并为其赋值所需的配置 xml,如果可能的话?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多