This question is primarily about good design.
Suppose I have a controller action, such as DeletePage, that can be called in two separate views of the same controller. Assuming that the deletion logic is not contained in the action itself, and some conditional checks and the like that cause the correct business logic, it makes no sense to duplicate the deletion action structure when I can instead have a private method that returns the ActionResult that I call in both actions that may cause deletion. My question is: where is the best place to put a reusable method like this? Right now, I'm just noting their privacy and sticking to them in the area of โโthe controller class, but maybe a sealed inner class will make more sense for such a method - or somewhere else entirely.
Thoughts?
public ActionResult EditPage(int id, FormCollection formCollection)
{
var page = _pagesRepository.GetPage(id);
if (page == null)
return View("NotFound");
if (page.IsProtected)
return View("IllegalOperation");
if (formCollection["btnSave"] != null)
{
}
else if (formCollection["btnDelete"] != null)
{
return DeletePage(page);
}
return RedirectToAction("Index");
}
public ActionResult DeletePage(int id)
{
var page = _pagesRepository.GetPage(id);
if (page == null)
return View("NotFound");
return DeletePage(page);
}
private RedirectToRouteResult DeletePage(Page page)
{
if(page != null && !page.IsProtected)
{
_pagesRepository.Delete(page);
_pagesRepository.Save();
FlashMessage(string.Format(PageForms.PageDeleted, page.Name), MessageType.Success);
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}