ASP.NET MVC - Reusing Actions

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);
}

// Reusable Action
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");
}
+3
3

, . , void/bool/etc, , RedirectToAction()? , , .

public ActionResult DeletePage(int id)
{
        var page = _pagesRepository.GetPage(id);

        if (page == null)
                return View("NotFound");

        DeletePage(page);
        return RedirectToAction("Index");
}

//reusable method
private void DeletePage(Page page)
{
    //your same validation/save logic here
}

DeletePage , / . ActionResult , , .

+4

-, , ActionResult. . DeletePage(Page page) .

.

+1

Personally, I agree with Kurt. The concept of deleting an unprotected page should be separate from what action the controller should take. Secondly, it confuses the code that should happen when the page is protected. In one action, it is redirected to the index, in the second, it is redirected to the Illegal Management view. Personally, I would do something like ...

public ActionResult DeletePage(int id) {
    var page = _pagesRepository.GetPage(id);
    if (!PageIsValidForDeletion(page)) {
         string invalidPageView = FindViewForInvalidPage(page);
         return View(invalidPageView);
    }
    DeletePage(page);
    return RedirectToAction("Index");
}
+1
source

Source: https://habr.com/ru/post/1715474/


All Articles