按名称解析的统一性 [英] Unity to Resolve By Name

查看:305
本文介绍了按名称解析的统一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get Unity to be my factory in a factory pattern, hence return different implementations of an Interface depending upon the named instance. I have the following code

public static class Factory
{
private static readonly IUnityContainer container;

public static class Factory { private static readonly IUnityContainer container;

    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);
    }
}



当我尝试通过调用CreateFactory方法与implemantation2或implemantation1我得到以下错误:
InvalidOperationException - 类型String无法构造。您必须配置容器以提供此值。

When I try to resolve the container by calling the CreateFactory method with "implemantation2" or "implemantation1" I get the following error: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.

我不知道我在做什么错误任何帮助将非常感激。

I don't know what I'm doing wrong any help would be greatly appreciated.

Josh

推荐答案

答案是.....

    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>()))
            ;
    }

这篇关于按名称解析的统一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆