【问题标题】:Unity to Resolve By NameUnity 按名称解决
【发布时间】:2012-08-31 04:57:17
【问题描述】:

我试图让 Unity 成为工厂模式中的工厂,因此根据命名实例返回不同的接口实现。我有以下代码

公共静态类工厂 { 私有静态只读 IUnityContainer 容器;

    static Factory()
    {
        container = new UnityContainer();
        container
            .AddNewExtension<EnterpriseLibraryCoreExtension>()
            .AddNewExtension<Interception>()
            .RegisterType<IInterceptionBehavior, LoggingBehaviour>()

            //register the implemantation1
            .RegisterType<IMessageFactory, implemantation1>("implemantation1")
            .RegisterType<IMessageFactory, implemantation1>(new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"]))
            .RegisterType<IMessageFactory, implemantation1>(new InjectionProperty("ServerPort", int.Parse(ConfigurationManager.AppSettings["Port"])))
            .RegisterType<IMessageFactory, implemantation1>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))

            //register the implemantation2
            .RegisterType<IMessageFactory, implemantation2>("implemantation2")
            .RegisterType<IMessageFactory, implemantation2>(new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"]))
            .RegisterType<IMessageFactory, implemantation2>(new InjectionProperty("Port", int.Parse(ConfigurationManager.AppSettings["Port"])))
            .RegisterType<IMessageFactory, implemantation2>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))
            ;
    }

    /// Instantiates a data provider class
    public static T CreateFactory<T>(string name)
    {
        return container.Resolve<T>(name);
    }
}

当我尝试通过使用“implemantation2”或“implemantation1”调用 CreateFactory 方法来解析容器时,我收到以下错误: InvalidOperationException - 无法构造类型 String。您必须配置容器以提供此值。

我不知道我做错了什么,任何帮助将不胜感激。

乔什

【问题讨论】:

    标签: c#-4.0 unity-container enterprise-library factory factory-pattern


    【解决方案1】:

    答案是.....

        static Factory()
        {
            container = new UnityContainer();
            container
                .AddNewExtension<EnterpriseLibraryCoreExtension>()
                .AddNewExtension<Interception>()
                .RegisterType<IInterceptionBehavior, LoggingBehaviour>()
    
                //register the implemantation1
                .RegisterType<IMessageFactory, implemantation1>("implemantation1", new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"]))
                .RegisterType<IMessageFactory, implemantation1>("implemantation1", new InjectionProperty("Port", int.Parse(ConfigurationManager.AppSettings["Port"])))
                .RegisterType<IMessageFactory, implemantation1>("implemantation1",
                    new Interceptor<InterfaceInterceptor>(),
                    new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))
    
                //register the implemantation2
                .RegisterType<IMessageFactory, implemantation2>("implemantation2", new InjectionProperty("WSIPServer", ConfigurationManager.AppSettings["WSIPServer"]))
                .RegisterType<IMessageFactory, implemantation2>("implemantation2", new InjectionProperty("WSIPServerPort", int.Parse(ConfigurationManager.AppSettings["WSIPServerPort"])))
                .RegisterType<IMessageFactory, implemantation2>("implemantation2",
                    new Interceptor<InterfaceInterceptor>(),
                    new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))
                ;
        }
    

    【讨论】:

    • 这不只是将IMessageFactory 注册到implementation1 并使用new Interceptor&lt;InterfaceInterceptor&gt;(), new InterceptionBehavior(...) 作为名称"implementation1" 吗?除非我误解了 UnityContainer 的工作原理,这是完全有可能的,我请大家指正,否则我不知道您将如何使用 new InjectionPropery(...) 获得 implementation1
    【解决方案2】:

    您必须将所有注入设置放在一个注册调用中:

    .RegisterType<IMessageFactory, implemantation1>(
              "implemantation1",
              new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"]),
              new InjectionProperty("ServerPort", int.Parse(ConfigurationManager.AppSettings["Port"]),
              new Interceptor<InterfaceInterceptor>(),
              new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多