Good?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

MVC

So why MVC when ASP.NET code-behind is so


good?
I am sure all ASP.NET developers love the code-behind concept. Accepting something new like MVC
will not be easy for them. So let’s analyze the problems with the current code-behind concept.

When we generally talk about ASP.NET applications built on a tiered architecture they are divided in
four parts: UI (ASPX pages), code-behind (ASPX.CS pages), middle tier (.NET classes), and finally the
Data layer.

If you look from the aspect of code distribution, the major code which has logic is in the middle tier
or in the code-behind (ASPX.CS files). The UI or ASPX files are HTML files which are more about UI
design, and data access logic is pretty much standard components like enterprise data blocks, entity
data contexts, etc.

Let’s try to analyze the problems.


Problem number 1: Unit Testing
From the aspect of unit testing we can exclude the data logic and the UI HTML. The data logic
classes are already time tested components like enterprise data block, entity data context, or LINQ
data context. So we really do not have to put a lot of effort on testing the DAL separately. In case you
have a custom data access layer it will still be easy to test them as they are simple .NET classes.

There is no logic in testing an ASPX HTML as it’s more about look and feel.

The middle tier is again a simple .NET class like data logic so you can easily do unit testing using
VSTS or NUNIT.

Now comes the most important one: the code-behind. The code-behind has a lot of action, and
testing it is one of the most important things. The only way to invoke these codes is by doing a
manual test. From a time perspective this would not be a great choice.

Even though Microsoft has always boasted about how the ASP.NET code-behind is separate from the
UI, in practical sense, it’s very difficult to decouple ASP.NET code-behind and do unit testing on it.

The ASP.NET code-behind is completely tied up with the ASP.NET HttpContext object which makes
unit testing very difficult. Just think os how to unit test the below ASP.NET code-behind. How do I
create an HttpCcontext object, how do I simulate the sender and EventArgs objects of the
button clicks, etc.

FYI: Many developers talk about mock tests, rhino mocks, etc., but still it is cryptic and the
complication increases with session variables, view data objects, and ASP.NET UI controls creating
further confusion.
Problem 2: The reality of separation of code
and UI
As said previously, the ASPX and the ASPX.CS cannot be decoupled in reality, thus reducing
reusability. Yes, Microsoft did say first that the code-behind is different from the UI, but then they are
probably separate physical files only and one cannot just exist without the other.

For instance let’s say the same button click code when called via HTTP POST should display
using displayinvoice.aspxand when called via HTTP GET should display in a tree view. In other words
we would like to reuse the code-behind. Just think of how can we do this using the current code-
behind.
Our HERO MVC (Model, View, and Controller)
That’s where MVC comes to rescue. The code-behind is moved to a simple .NET class called
Controller. Any user request first comes to the Controller class, the Controller class then invokes the
model, and attaches the model to the view for display to the end user.

As this controller class is a simple .NET class we can reuse and also do unit testing easily. So let’s see
how we can create MVC application using MVC template provided by visual studio.
What is MVC Pattern?

MVC stands for Model - View – Controller. It is an architecture for developing interactive applications
where there would be a user interaction involved and event handling would occur

What is the role of Model, View and Conroller?

 Model : It manages data basically state of application in memory. There is no fixed size or shape
of model objects since what we inherit to hold in memory in one application may not be the
same that in other application. It includes all of an application’s validation logic, business logic
and data access logic. For example, an Employee Object(Model) might retrieve information from
a database, operate on it, validate it and then write updated information back to a Products
table in database.

 View : It contains logic for rendering Graphical Representation/ HTML Output. Typically it
creates UI with data from Model. An example would be edit view of a products table that
displays text boxes, drop down lists, and check boxes based on the current state of a products
object.

 Controller : It contains control flow logic. It is the one which interacts with both models and
views to control the flow of application execution. The controller handles and responds to user
input and interaction by creating and passing model to view.
What is a Framework?
A Framework is a collection of classes which provide abstraction of a particular concept.
Framework classes are re-usable since a framework is like a semi-complete application which
has to be extended and made complete for developing a specific application.
Our code fits into the Framework through extension points.

What is ASP.NET MVC Framework?


 It’s an alternative framework to ASP.NET WebForm framework for developing web based
applictions using asp.net framework.
 Asp.net MVC 1.0 was launched in first part of 2009.
 Every web application is interactive by nature and hence is a potential candidate in applying
MVC architecture

What is a Model?

1. MVC model is basically a C# or vb.net class


2. A model is accessible by both controller and view
3. A model can be used to pass data from controller to view
4. A view can use model to display data in page

What is a View?

1. View is an ASPX page without having a code behind file


2. All page specific HTML generations and formatting can be done inside view
3. One can use inline code(server tags) to develop dynamic pages
4. A request to view(aspx page) can be made only from a controller’s action method

What is a Controller?

1. Controller is basically a C# or vb.net class which inherits System.Mvc.Controller


2. Controller is heart of the entire MVC architecture
3. Inside controller’s class action methods can be implemented which are responsible for
responding to browser or calling views
4. Controller can access and use model class to pass data to views
5. Controller uses viewdata to pass any data to view.

You might also like