mercredi 11 mai 2016

Patterns and coding style to avoid using Switch statements in coding

Consider the code below. I want to create a service based on the type of binding. I have created a switch case for it, but I am not satisfied with the approach, I want to avoid switch statement for expandibility in future. Please suggest a better pattern or design to do this.

   public static object CreateBinding(string binding, object service)
    {
            switch (binding)
            {
                case "ServiceA":
                    ChannelFactory<IServiceA> ServiceFactoryA = new ChannelFactory<IServiceA> (binding);
                    service = ServiceFactoryA.CreateChannel();
                    break;

                case "ServiceB":
                    ChannelFactory<IServiceB> ServiceFactoryB = new ChannelFactory<IServiceA> (binding);
                    service = ServiceFactoryA.CreateChannel();
                    break;

                default:
                    ChannelFactory<IServiceC> ServiceFactoryC = new ChannelFactory<IServiceC> (binding);
                    service = ServiceFactoryC.CreateChannel();
                    break;
            }

            OpenChannel(service);

        return service;
     }

Aucun commentaire:

Enregistrer un commentaire