I am back with some exciting and interesting stuff in my bag of tricks that contains a classy MVC tutorial for beginners with all the goodies in it, that a MVC beginner needs to know. So let's see what I have.
Agenda
- Quick Reference
- A Brief Introduction
- The Controllers Role
- Mind Twisters | Controller
- Controller Hierarchy
- Base Classes
- Home Controller
- Account Controller
- Creating a Simple Controller
- Interview Questions | Controller
- Pin Points
- Summary
Quick Reference
From my perspective, I think it would be better to start this series with Controllers with a very basic definition and quick introduction. As I said earlier, this series is purely for beginners. So depending on my knowledge and hands-on experience with MVC, I tried my best to represent this in a very easy manner.
Before proceeding any further, I suggest you go through the following article:
http://www.c-sharpcorner.com/UploadFile/2072a9/Asp-Net-mvc4-a-walk-through/
So let's begin.
A Brief Introduction
If we talk about the development phase of MVC then we can see it is all about the Graphical User Interface (GUI). Whenever a user does something, there is a process that always gives it’s corresponding reaction towards the user’s action. That simple process was nothing but a Controller.
A Controller is responsible for listening for input from the user and responding to it in a specific manner.
The Controllers Role
Controllers within the entire MVC are responsible for responding to the user’s input, or I can say it is the central part of MVC that directly or indirectly deals with:
In general, a Controller decides what to do and to represent that using a View. The Controller performs the following set of actions one by one:
- Receives a request
- Based on request parameters, it decides activities
- List up the tasks to be done
- Forward it to the View
(This image shows how the data is actually transformed from one state to another inside a MVC and responds to a user’s request.)
Some Common Mind Twisters
In this article I have also listed some very useful and important Mind Twisters with respect to MVC, the Controller that a beginner needs to understand in my opinion if he/she is just beginning to go through MVC.
So let’s explore these twisters one by one.
MT1: If there was no Controller
The answer to this twister is very easy. Just for a moment think of your application without the business logic that glues your model and view together. (In the parallel world there is the concept of a front-end controller, this concept is nearly the same as making MVC Controller-less.)
MT2: Why to use a Controller
There can be several views, I am presenting a few common and simple views:
- Three words, "Separation of concerns"
- A bridge between "Model’ and "View’
- For the sake of your business logic
MT3: How a Controller works with both a View and a ModelA Controller handles a request from a View and updates the Model with corresponding changes. This results in a change of the Model’s current state.
MT4: Alternatives to Controllers
MVC is a complete concept in itself, but depending on the development work we have alternatives to MVC in the following forms:
- Model-View-Presenter (MVP)
- Front Controller
- Supervising Controller
- Passive View
(If you guys have any other mind twisters In your mind then let me know and I’ll update the twisters here with it.)
Controller Hierarchy
The following is a typical Controller hierarchy:
Base Controller Classes
Before going any further, first let’s explore the base classes of Controller. In the very basic concept if you create a project using an internet template, then in this case you will see 2 base classes in the MVC Pattern.
These classes are:
Home Controller
This Home Controller is nothing but the main/root/reference page of your website. In a normal web dev language we generally know these pages as:
- Home page
- About Page
- Contact Page
- Default/Index Page
- Root/Main Page
But the common thing in all those cases is that if it’s saying "HOME Controller’ then it should be clear in your mind that its talking about a Home/Root/Main page.
Account Controller
As the name suggests, the Account Controller is responsible for handling account related requests. These account related requests are normally generated using the following types of pages of your website or web portal:
- Login/SignIn Page
- Logout/SignOut Page
- Registration Page
Creating a Sample Controller
I tried to keep this as simple as I can. It’s just a brief overviews or I can say a teaser of creating a sample MVC Controller. Here we go.
You can start by creating a new "Store Controller Class". So for this use the following procedure.
Click on Solution Explorer (usually in the right hand side, upper-most corner).
Right-Click the Controllers folder.
-Click on ADD
(You will get a list of items)
-Click on Controller
-Name the Controller
(As MyController)
-Select a TEMPLATE
(Depending on you, for a quick reference about templates visit the article:
http://www.c-sharpcorner.com/UploadFile/2072a9/Asp-Net-mvc4-a-walk-through/)
-Finally, click Add
(After adding a new Controller: MyController, you will be something like that)
Interview Questions | Controller
So, if you guys are into MVC then try to mesmerize these questions based on MVC Controller, because in a parallel world you can encounter these questions during an interview or discussion with another Geek and colleagues:
- Is Unit Testing of a MVC application possible without running the Controller?
- How does a Controller accommodate the M and V in MVC?
- What does Controller represents in a MVC application?
- Is the sharing of a view possible across multiple Controllers?
- Write down some return type of action Controllers’ method in MVC?
- What is the real application of Controllers in MVC?
- How do you return a partial view from a Controller?
(These are very few questions. If anyone has more questions then let me know and I’ll update the list here. There is one more thing I didn’t provide an answer for to keep this article as brief as possible. If any of you have query regarding any question, then his/her queries are warmly welcomed.)
Pin Points
Actually these pin points are the image of this article. Here they are:
- A Controller is nothing but a bridge
- What we need depends on our business logic
- Contains two base classes
- Necessary (depending on the Separation of concerns)
- You can work with a Front Controller or I can say ControllerLess MVC
Summary
A few tips from my side is that, if you guys really want to become familiar with MVC then:
- Take your time
- Do as much practise as you can (Implementation)
- Try to "Learn’ things.. Don’t just "Study’ (I mean it)
- Try to discuss things with friends, colleagues or in forums (C# Corner)
- If you experience any problem, then your queries are welcome.
I hope you guys enjoyed this.
#Happy Coding