Search code examples
c#-4.0unity-containerenterprise-libraryfactoryfactory-pattern

Unity to Resolve By Name


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;

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

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


Solution

  • And the answer is.....

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