Basic Introduction of ASP.NET MVC

MVC is a fresh experience for the Microsoft ASP.NET developer. Last one year I am working with this framework. The most important thing is that ASP.NET MVC build on top of .NET Framework and can take advantage of the exiting .NET features like CLR, BCL, JIT etc. For one who is using ASP.NET Web Form it will be a good fresh experience like me.

What is ASP.NET MVC?

It is always good practice to build application which have different layers and they have less strong dependencies between them. By doing so we can gain some advantages like parallel development, maintainable code, testable code etc. When we are using ASP.NET Web Form then it can not be fully possible, because Web Form having tight relation between presentation layer and application logic by code behind files. We can not test the presentation layer easily because it has tight relation with ASP.NET runtime. The code behind files always hold the application logic and we can not easily decouple it with the aspx page.

On the other hand MVC has three layer with the name Model, View and Controller. Model means the domain objects of our application. View means the pure presentation layer with out any application logic (we should not write any logic here) and the Controller means the application logic of our application. So here the Controller is responsible to populate the model with data and call the view to create the presentation. By using this three layer you can divide the responsibilities. MVC has the option to heavy use of interfaces and dependency injection, so that we can test every layers of the application (also we should create a wrapper for the static classes for testability). We can test the Controller with out any dependencies with the ASP.NET runtime.

ASP.NET MVC is light weight. Almost every component is pluggable here. Like you can use a different view engine or a different validation framework in ASP.NET MVC. Microsoft also implement it as a convention over configuration in mind. That means many things are done simply following the convention, you do not have to be configure it explicitly. For example views are kept on particular folders with the same name of the controller and the controller can find them with out any more coding from the developer’s end. I think the most exiting feature of ASP.NET MVC is model binding. You can create a view specific model and can create a strongly typed view with that model. It will actually render and populate the model object for you. Another fine feature is attribute based model validation in ASP.NET MVC. We can declare attribute for our validation on model properties. By which our validation logic will be DRY (don’t repeat yourself) enough so that where we are using that model our validation will be in one place, easy to maintain. You can also extends validation logic in ASP.NET MVC.

MVC
MVC

Basic demo

Here a small and very basic sample of ASP.NET MVC:

Model:

using System.Collections.Generic;

namespace MvcApplication4.Models
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
    }

    public class EmployeeDB
    {
        public List<Employee> GetAll()
        {
            // In real application data may be coming from database.
            return new List<Employee>()
            {
                new Employee(){ Id = 1, Name = "Rahul", Department = "Software" },
                new Employee(){ Id = 2, Name = "Mita", Department = "HR" },
                new Employee(){ Id = 3, Name = "Bob", Department = "Sales" },
            };
        }
    }
}

Controller:

using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            EmployeeDB empDB = new EmployeeDB();
            IList<Employee> emps = empDB.GetAll(); // Get the data.
            return View(emps); // Return view with model data.
        }
    }
}

View:

<%@ Page 
    Title="" 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication4.Models.Employee>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <table>
        <tr>
            <th></th>
            <th>Id</th>
            <th>Name</th>
            <th>Department</th>
        </tr>
    <% foreach (var item in Model) 
       { %>
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id = item.Id }) %> |
                <%: Html.ActionLink("Details", "Details", new { id = item.Id })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id = item.Id })%>
            </td>
            <td><%: item.Id %></td>
            <td><%: item.Name %></td>
            <td><%: item.Department %></td>
        </tr>
    <% } %>
    </table>

    <p><%: Html.ActionLink("Create New", "Create") %></p>

</asp:Content>

For more information on ASP.NET MVC please take a look here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s