推荐ASP.NET MVC ActionResult作为抽象类? [英] Reasoning behind ASP.NET MVC ActionResult being an abstract class?

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

问题描述

ASP.NET MVC 中, ActionResult class,它是控制器中由action方法返回的所有结果的基础,被定义为具有单一方法(© Microsoft)的抽象类:

  public abstract void ExecuteResult(ControllerContext context); 

你能想到这个设计的任何具体原因吗?具体来说,对我来说似乎有点奇怪,那么




  • 没有 IActionResult 接口,

  • ,如果有这样的界面,那么该类就不需要了。


$ b $毕竟,如果这是一个接口而不是抽象类,则不需要扩展一个基类,以创建一个新的 ActionResult - 一个只需要正确地实现 IActionResult 。在一个世界上,错误的语言,没有多重继承,这个优势似乎对我来说很重要。

解决方案

接口非常适合允许一个实现多个合同的类,例如当你知道一个类型必须是两个不同的东西。在某些情况下,这可能会鼓励创建一个责任太多的类型。



行动结果有一个责任,似乎没有任何场景,您需要一个对象既可以是动作结果和<其他的东西即使你这样做,也可以通过组合来做。所以在这种情况下,我们与ABS一起使用,以便在我们的RTM之后,如果需要,可以更改弹性。



但是,如果有一个特定的情况我们阻止哪个界面会更好,我们会考虑一下。我们可以随时以不破坏的方式执行。



您甚至可以通过编写自己的操作调用者来实现,只需要您实现IActionInvoker(一个接口),并且该调用者可以检查您自己的IActionResult而不是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

  • there is no IActionResult interface,
  • and that the class would not be required at all, if there was such an interface.

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.

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.

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.

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天全站免登陆