Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How ActionResult of ASP.NET MVC implement Command Pattern

Refer to "Pro ASP.NET MVC 2 Framework", ActionResult is an implementation of Command Pattern. I'd like to know how the pattern is implemented here? Can you send me some light?

Thank you.

like image 921
Ricky Avatar asked Mar 24 '11 06:03

Ricky


2 Answers

An action method returns an instance that embodies a command that the framework needs to perform next. This provides a means for delaying the execution of framework/pipeline code until after the action method is complete, rather than from within the action method.

This command represented by the ActionResult abstract class and posses the ExecuteResult method which is implemented by concrete commands such as ViewResult and JsonResult:

enter image description here

like image 51
Darin Dimitrov Avatar answered Nov 28 '22 09:11

Darin Dimitrov


The ActionResult is not an implementation of command pattern at all. The Action on the Controller is the closest to the command, and the ActionResult is the alteration to the View, typically. The command updates models, ActionResults do not update models.

I implemented the command pattern in MVC by using an ActionFilter and storing the Action, Controller name, and Parameters in a history (eg: a List<>). One controller implemented Undo and Redo, while all other controllers followed the convention of having all actions prefixed with Do_ and Undo_. Alternatively you could pass an additional bool? undoing parameter.

There are many ways of doing Command in MVC, but ActionResult has nothing to do with it.

like image 43
Sentinel Avatar answered Nov 28 '22 09:11

Sentinel