mercredi 24 octobre 2018

Best pattern to check conditions and show related messages in a ASP.NET MVC

I have a ASP.NET MVC 5 project including following parts:

Index View

@model IEnumerable<BusinessLogic.Process>
@*do something ...*@

Controller

public ActionResult Index(int processId)
{           
    return View(Fac.GetChildrenProcess(processId));
}

Facade class

public List<Process> GetChildrenProcess (int processId)
{
    Process process = new ProcessContoller().GetInfo(processId);
    if (process == null)
      return null;
    List<Process> children = new ProcessContoller().GetListByParentId(processId);
    return children;
}

ShowMessage

private ActionResult ShowMessage(string messageText, DisplayMessageType messageType)
{
    ViewBag.CssClass = messageType.ToString();
    ViewBag.MessageContent = messageText;
    return PartialView("_ShowMessage");
}

According to follow SOC pattern, I tent to check if condition if (process == null) in a class like Facade not in my controller (Index action).

But I cope with a problem to show message to user in best pattern. For example if processId is invalid in action, if (process == null) is true and I should show a message like Selected process is invalid. To show a message, I have a Partial view like _ShowMessage.

  • I can't check the condition if (process == null) in my controller because of following SOC pattern.
  • I can't show my message in Facade because is a class not a controller.
  • I can't return some Boolean value from GetChildrenProcess because it should return a List.
  • I can pass a Boolean value by ref and if (process == null) is false, I show my message. (I think it's bad pattern)

So, I am curies about best pattern for checking a condition that is similar to validation and showing a message.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire