In vNext of ASP.NET MVC is the ability to use POCO classes as controllers. No need to inherit a controller class.
By default, as long as your class is public, non-abstract, has a Controller suffix and is defined in an assembly that references any of the MVC assemblies (Microsoft.AspNet.Mvc.Core, Microsoft.AspNet.Mvc etc), it will be discovered as a valid controller. An example is shown below.
But you cannot do much with just a POCO class – since you typically need access to things like current HTTP request, View Data or current Principal. However, this can be achieved as the POCO controllers support convention-based property injection. This is achieved throughDefaultControllerFactory, which looks for the following properties:
- – ActionContext and injects an instance of ActionContext there
- – ViewData, and injects an instance of ViewDataDictionary there
- – Url, and injects an instance of IUrlHelper there
So we could modify our controller accordingly:
And you have all the contextual information you might need – for example, HttpRequest and Principal are both hanging off ActionContext.
In addition to that, the helpers, such as the aforementioned IUrlHelper or IActionResultHelper, can also be constructor injected, since ASP.NET vNext has dependency injection all the way through.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class DummyController
{
private readonly IUrlHelper _urlHelper;
private readonly IActionResultHelper _actionResultHelper;
public DummyController(IActionResultHelper actionResultHelper, IUrlHelper urlHelper)
{
_actionResultHelper = actionResultHelper;
_urlHelper = urlHelper;
}
//omitted
}
|