Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IActionResult misunderstanding

I just downloaded music store (microsoft sample projct) source code that based on ASP.NET 5. I don't understand there why developers from Microsoft use IActionResult interface as a return type in controllers.

What is a reason of IActionResult interface usage? Why don't just use ActionResult type.

enter image description here

like image 570
Vnuuk Avatar asked Nov 04 '15 19:11

Vnuuk


2 Answers

See this post about IActionResult vs. ActionResult: http://forums.asp.net/post/5980446.aspx

IActionResult allows a wider range of return types, including any custom code that implements the IActionResult interface. ActionResult is limited only to those classes which extend the ActionResult abstract class (which you could also do with custom code, but using an interface allows for something like multiple inheritance, while extending a class does not).

like image 163
R. Salisbury Avatar answered Sep 21 '22 17:09

R. Salisbury


Using interfaces as parameters will allow you to use dependency injection to obtain its dependencies and those dependencies can be replaced with mock implementations when testing which implement those interfaces.

like image 26
gomac Avatar answered Sep 20 '22 17:09

gomac