I'm having major troubles trying to get rid of this switch statement. To put a little of context first, I'm working with asynchronous batch operations. These operations can be applied to any entity in the system. You just have to send a different namespace.
Example:
"jobs": [
{
namespace: "client",
request: {"id": 10, "name": "john"}
},
{
namespace: "invoice",
request: {"id": 99, "number": "F20170101"}
}]
After I send those jobs, the service returns data similar, but with a response key like the following:
results: [
{
namespace: "client",
request: {id: 10, name: "john"},
response: {success: false}
},
{
namespace: "invoice",
request: {id: 99, number: "F20170101"},
response: {success: true, s_id: 3310}
}]
Now here comes the funny part
I have to process the response, and based on the namespace, I have to select the appropriate repository to update our database records. So far I have this smelly thing:
if (namespace == 'client') {
customerRepository.doSomething()
} elseif (namespace == 'invoice') {
invoiceRepository.doSomethingTotallyDifferent(withDiffParams)
}
Now this clearly breaks Open/Close principle, and I can't figure out how to solve this by removing the switch
(actually a if
/elseif
spaghetti thingy, but it's the same). Thanks in advance.
PS: this is not a specific language question, so please refrain to provide answers that applies only to a specific language.