我正在尝试让Unity成为工厂模式中的工厂,因此根据命名实例返回接口的不同实现。我有以下代码
公共静态类工厂 { 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);
}
}
当我尝试通过使用“implemantation2”或“implemantation1”调用CreateFactory方法来解析容器时,我收到以下错误: InvalidOperationException - 无法构造String类型。您必须配置容器以提供此值。
我不知道我做错了什么,非常感谢任何帮助。
约什
答案 0 :(得分:0)
答案是......
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>()))
;
}
答案 1 :(得分:0)
您必须将所有注射设置放入注册调用中:
.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>()))