Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasoning behind ASP.NET MVC ActionResult being an abstract class? [closed]

Tags:

asp.net-mvc

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.

like image 720
hangy Avatar asked Nov 29 '08 19:11

hangy


People also ask

What is ActionResult () it is an abstract class?

What is an ActionResult? ActionResult is an abstract class that represents the result of an action method. The class itself inherits from System. Object, and only adds one additional abstract method: ExecuteResult, which is an abstract method that the derived classes of ActionResult will implement themselves.

Why do we use ActionResult in MVC?

The ActionResult method works as a return type of any controller method in the MVC. It acts as the base class of Result classes. It is used to return the models to the Views, file streams, and also redirect to the controllers. It is the responsibility of the Controller that connects the component.

What is ActionResult () in MVC?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result.

What type of class is ActionResult?

The ActionResult class is the base class for all action results. You decide which type of action result to return based on the task that the action method is performing. A common action result is obtained by calling the View method, which returns an instance of the ViewResult class.


1 Answers

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.

like image 119
Haacked Avatar answered Sep 22 '22 03:09

Haacked