6

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.

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

4 Comments

Besides delaying the execution, is there any other benefit?
@Ricky, delaying the execution is the main idea behind the command pattern: In object-oriented programming, the command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time.
@Dimitrow: Appreciate your reference
What? Looking at the above, what on earth has any of that got to do with command pattern? How does ActionResult help me stack them for later undo() or anything like that?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.