So I'm new to generics. But generics seem like a great way to reduce code. here is the scenario. I have a MVC Web API.
http://www.google.com/{controller}/{chartType}/{id}
NOTE: id is optional
I have several chart types which return similar objects:
- HourlyDeviceChart
- HourlyUsersCharrt
- HourlyAvgProcessingTime etc..
I would like to have just one method that evaluates the chart type parameter and executes the corresponding actions. instead of 8 or 10 methods.
I would be accepting if my design needs some refactoring. I'm open to suggestions. The idea here is to reduce some code. I would hate to have 10 methods exposed in the Web API and then 10 more corresponding methods in another class. Just seems redundant.
As always your suggestions are welcomed!
The method exposed by the API:
IEnumerable<T> GetChart(string chartType)
{
switch(chartType)
{
case "DeviceChart":
return repository.HourlyDeviceChart();
break;
case "UserChart":
return repository.HourlyUsersChart();
break;
}
}
//Then the class that handles all the work would look something like the below
IEnumerable<HourlyDeviceChart> HourlyDeviceChart()
{
// select appropriate items from the queue
// populate HourlyDeviceChart object
// add object to list
// return HourlyDeviceChart list
}
IEnumerable<UserDeviceChart> HourlyUsersChart()
{
// do more of the same
}