推理背后的ASP.NET MVC的ActionResult是一个抽象类? [英] Reasoning behind ASP.NET MVC ActionResult being an abstract class?

查看:104
本文介绍了推理背后的ASP.NET MVC的ActionResult是一个抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC ,在的ActionResult 类,是基地由一个控制器操作方法返回的所有结果,被定义为单一方法的抽象类(及复印件;微软):

In ASP.NET MVC, the ActionResult class, which is the base for all results returned by action methods from a controller, is defined as an abstract class with the single method (© Microsoft):

public abstract void ExecuteResult(ControllerContext context);

您能想到的这个设计任何特殊的理由?具体而言,它似乎有点怪我,那

Can you think of any specific reasons for this design? Specifically, it seems a bit weird to me, that


  • 没有 IActionResult 接口

  • ,而且类不会在所有被要求,如果有这样的接口。

毕竟,如果这是一个接口而不是抽象类,就没有必要为了创建一个新的的ActionResult 扩展一个基类 - 一会只是要落实 IActionResult 正常。在这个世界上,犯错的语言,没有多重继承,这种优势似乎对我来说非常重要。

After all, if this was an interface instead of that abstract class, there would be no need to extend a base class in order to create a new ActionResult - one would just have to implement IActionResult properly. In a world, err language, without multiple inheritance, this advantage would seem quite important to me.

推荐答案

接口是伟大的,允许一个类实现多个合同,如当你知道一个类型必须是两个不同的东西。在某些情况下,这可以鼓励创建具有太多的责任一个类型。

Interfaces are great for allowing a class to implement multiple contracts, such as when you know that a type must be two different things. In some cases, this can encourage creating a type that has too many responsibilities.

行动的结果有一个责任,它似乎并不像会有任何情况下,你需要一个对象既行动结果的的东西。即使你做到了,它可能通过组成有关。因此,在这种情况下,我们有ABS去使我们有更大的灵活性后我们RTM如果需要进行更改。

Action results have a single responsibility and it didn't seem like there would be any scenario where you need an object to be both an action result and something else. Even if you did, it's possible to do via composition. So in this case, we went with ABS to allow us greater flexibility after we RTM to make changes if necessary.

但是,如果有就是我们正在阻止一个特定的场景,其中一个接口将是preferable,我们会考虑的。我们总是可以做到这一点在后面,这不是打破的方式。

However, if there's a specific scenario we're blocking in which an interface would be preferable, we'll consider it. We can always do it later in a manner that's not breaking.

您甚至可以通过编写自己的行动调用程序,只需要您实现IActionInvoker(接口)和调用者可以检查自己的IActionResult而非的ActionResult自己做。

You can even do it yourself by writing your own action invoker, which only requires you to implement IActionInvoker (an interface) and that invoker could check for your own IActionResult rather than ActionResult.

这篇关于推理背后的ASP.NET MVC的ActionResult是一个抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆