mardi 20 juin 2017

Design Pattern for handling Microservices requests

I have an application which is based on Microservices. I have a Broker/Gateway API which receives HTTP requests. Then I have two more services Service A and Service B. When the Broker API receives a message it uses RabbitMQ to send it to the services. Then in each one of them I should handle it and process it differently depending on the message (execute different action).

This is the way I handle the messages currently:

Service A

    public string ProcessAsync(Message message)
    {
        //Put verification to check if the request comes from the Broker API

        switch (message.EventCode)
        {
            case "context 1":
                return action1();
            case "context 2":
                return action2();
            case "context 3":
                return action3();
            case "context 4":
                return action4();
            case "context 5":
                return action5();
            default:
                throw new Exception("Unknown message event code!");
        }
    }

It makes sense to use switch statement if I have no more than 5 - 10 different message types. But in my app I have 30. Writing such a big conditional statement is ugly and different to maintain. I am looking for a design pattern that is going to remove this problem from the application. Do you have any ideas? What do you think about State Pattern?

Aucun commentaire:

Enregistrer un commentaire