Good?
Good?
Good?
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.
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
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 a Model?
What is a View?
What is a Controller?