ASP.NET MVC的ActionResult如何实现Command Pattern
参考《Pro ASP.NET MVC 2 Framework》,ActionResult是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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
操作方法返回一个实例,该实例包含框架下一步需要执行的命令。这提供了一种将框架/管道代码的执行延迟到操作方法完成之后的方法,而不是从操作方法内部执行。
此命令由 ActionResult 抽象类表示并拥有 ExecuteResult 方法通过
ViewResult
和JsonResult
等具体命令实现: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
andJsonResult
:ActionResult 根本不是命令模式的实现。控制器上的操作最接近命令,而操作结果通常是对视图的更改。该命令更新模型,ActionResults 不更新模型。
我通过使用 ActionFilter 并将 Action、控制器名称和参数存储在历史记录中(例如:List<>)来实现 MVC 中的命令模式。一个控制器实现了撤消和重做,而所有其他控制器都遵循所有操作都以 Do_ 和 Undo_ 为前缀的约定。或者你可以传递一个额外的布尔值?撤消参数。
MVC中做Command的方式有很多种,但ActionResult却与之无关。
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.