Hi,
I had a controller POST action that is called List that accepts a status variable which can be the following values { "all", "active", "inactive}. Then I made repository calls based on the the value of "status" inside of the controller. The controller looked like this:
[HttpPost]
public ActionResult List(string status)
{
return View(GetJobTitlesByStatus(status));
}
private IList<JobTitle> GetJobTitlesByStatus(string status)
{
IList<JobTitle> jobTitles;
switch (status)
{
case "All":
jobTitles = jobTitleRepository.GetAll();
break;
case "Active":
jobTitles = jobTitleRepository.GetActive();
break;
case "Inactive":
jobTitles = jobTitleRepository.GetInactive();
break;
default:
jobTitles = new List<JobTitle>();
break;
}
}
I decided that the code in the switch statement was too much to be inside of a controller, so I extracted this out into a service and this service then makes the appropriate repository calls. For example:
public class JobTitleService : JobTitleRepository, IJobTitleService, IJobTitleRepository
{
public JobTitleService(ISession session) : base(session) { }
public IList<JobTitle> GetJobTitlesByStatus(string status)
{
IList<JobTitle> jobTitles;
switch (status)
{
case "All":
jobTitles = base.GetAll();
break;
case "Active":
jobTitles = base.GetActive();
break;
case "Inactive":
jobTitles = base.GetInactive();
break;
default:
jobTitles = new List<JobTitle>();
break;
}
return jobTitles;
}
}
I personally think this works great, expecially since I'm using dependency injection to get the service into the controller. I have the following questions:
1) Do you think it is a good idea to extract the switch statement logic from the controller?
2) Do you think that having JobTitleService inherit from JobTitleRepository is better than
having an IJobTitleRepository passed into the constructor of the Service (using dependency injection)?
3) Is there a special name for the Design Pattern used for the the JobTitleService?