Introduction
There is no need to discuss how popular MVC architecture is in Microsoft’s web development platform. We all know MVC stands for model, view and controller where controller is like glue between model and view. It holds the business logic, formulates data and decisions which view to call and which data to be attached to it.
Fine, we know its functionality and we became happy to derive our controller class from “Controller
” class and get our work done. We never thought why the controller
class is and how it derived? I too was less bothered at first during my MVC days, but started to explore more about controller and found the big hierarchy of controller
class. In this article, we will discuss hierarchy of controller
class and we will see how the class had derived from various base classes and implemented various interfaces. At first, we will discuss the hierarchy of controller
class and then we will see how we define our own controller
class from “Control
” class in MVC framework. So, let’s start our discussion.
IController is in Top Level
We can say that this is the top basest interface of controller
class. The interface is very simple and clean, contains only one signature. Here is the IController
interface.
namespace System.Web.Mvc
{
public interface IController
{
void Execute(RequestContext requestContext);
}
}
It contains single signatures which need to implement in its concrete implementation. The controller is located within System.Web.Mvc
namespace which is the primary DLL of MVC framework. The method takes request context as parameter.
ControllerBase Class which Implements IController
This is the next level of hierarchy where ControllerBase
class implements IController
interface. The ControllerBase
contains six properties, one parameter less constructor and three methods. The top most popular and well known properties ViewData
and ViewBag
are defined within this class by which we can send data from controller to view. We can check that ViewBag
is dynamic in nature which was introduced with MVC 3 and Viewdata
is Dictionary
in nature, we know that we need to store data in key value pair within ViewData
. Actually ViewBag
is nothing but an abstraction over ViewData
and internally they use the same mechanism to store data. ViewBag
gives flavor of dynamic characteristic. The rest of the functions and properties are described in comments. You can get the same view as you explore each class in Visual Studio. Point to remember is that, it’s an abstract
class and we cannot create an instance of this.
public abstract class ControllerBase : IController
{
protected ControllerBase();
public ControllerContext ControllerContext { get; set; }
public TempDataDictionary TempData { get; set; }
public bool ValidateRequest { get; set; }
public IValueProvider ValueProvider { get; set; }
[Dynamic]
public dynamic ViewBag { get; }
public ViewDataDictionary ViewData { get; set; }
protected virtual void Execute(RequestContext requestContext);
protected abstract void ExecuteCore();
protected virtual void Initialize(RequestContext requestContext);
}
Controller Implements IController and ControllerBase and Many More
Yes, we will see that Controller
class implements IController
and Derived
from ControllerBase
and many more. Here is the implementation information. The Controller
class is too abstract
class so we cannot create an instance of it. Actually the Controller
class contains lot of methods and properties. In this article, we are not interested in talking about them; we will just understand the hierarchy of Controller
class in ASP.NET MVC.
public abstract class Controller: ControllerBase, IActionFilter,
IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter,
IResultFilter, IAsyncController, IController, IAsyncManagerContainer
So, we are seeing that the Controller
class is implementing many other interfaces which contain various useful functions to work out controller properly. For example, the IDisposable
interface contains a method related to dispose the controller
object after creation and various filter related interfaces are implemented to implement filter in controller level.
At Last, Define Our Own Controller Class
This is the last phase of hierarchy. Here, we can define our own controller which will be derived from the Controller
class. The beauty of the hierarchy is, we are getting the readymade product and tool to start action immediately. As we are deriving our own Controller
class from Controller
class in Library
, we are capable of using all the functions and properties which were already implemented in Controller
class and BaseController
class. Here is the way to derive our own controller
class from Controller
class.
MyController : Controller
{
}
Now, the question may come in mind, Why not defined our own controller
class from BaseController
or why not implement all those interfaces in our own controller
class manually? Technically speaking, it’s possible to define control
class in same approach, let’s have a look. In this example, we have derived our controller
class from ControllerBase
and try to run the application. Before we execute the controller, the ExecuteCore()
will get executed and as we did not implement it’s implementation, it will throw an exception but always we can do it because we know MVC is open source application.
After ExecuteCore()
is done, it will execute Hello()
action. So the advantage is, Microsoft has already defined that stuff for us and we just need to use it.
Point to Remember
If you are new to MVC framework, then you may think that, why our Controller
class ends with “Controller
” suffix? The suffix is mandatory otherwise it will not be active. I wish to discuss the fact in another article.