I have a function a plugin that is called whenever the WebAPI's "Plugin" endpoint is called in the main project, and that has to process the HTTP request. The request holds more information about what to do in method and query string, and I have for now copy&pasted the code to choose methods based on these:
public object OnCustomControllerCalled(Toolkit tk, HttpRequestMessage req)
{
if (req.Method == HttpMethod.Get)
{
if (req.RequestUri.Query.Contains("data=Customers"))
{
return MyPlugin.GetCustomers(tk);
}
else if(req.RequestUri.Query.Contains("data=UserManagement"))
{
return MyPlugin.GetUserManagement(tk);
}
...
}
else if (req.Method == HttpMethod.Post)
{
if (req.RequestUri.Query.Contains("data=Customers"))
{
return MyPlugin.PostCustomers(tk, req);
}
else if(req.RequestUri.Query.Contains("data=UserManagement"))
{
return MyPlugin.PostUserManagement(tk, req);
}
...
}
else if (req.Method == HttpMethod.Put)
{
if (req.RequestUri.Query.Contains("data=Customers"))
{
return MyPlugin.PutCustomers(tk, req);
}
...
}
else if (req.Method == HttpMethod.Delete)
{
...
I know that copy&paste is not a good approach to this problem. How am I supposed to make this easily extensible and less error-prone?
Throw reflection at it to get to the DRY principle?
Or would it make sense to have a single function MyPlugin.Customers
instead of one for each method, and let that single method contain the whole behaviour regarding the "customer" objects, and keep the differentiation between get, post, put and delete in that function?