Background
ASP.NET MVP pattern evolved during the late 90s. It is the descendent of MVC pattern with a slight change in the design pattern, i.e., it implements the observer design approach of MVC. The rest of the components remain the same which means the model and view do their task as in MVC.
MVP (Model-View-Presenter)
ASP.NET MVP consists of three parts. They are:
- Model
- View
- Presenter
Model
Model is the domain type or the business level object. It contains the application data and provides logics and behaviours to access the data. Model needs to be independent and shouldn’t have any problems and issues regarding the user interface (UI). Since the model should be independent, the same model can be used by different type of UI level applications.
View
A view is a place holder which holds the model data and shows them to the application users. Simply, view is a web form for presenting the model data. Model doesn’t have direct link with views while view should have knowledge about their models.
Presenter
Presenter addresses the user input and uses it to manipulate the model data. A view passes the user input actions to the presenter for interpretation. Presenter will act upon the received data from the view and communicate with model and returns result to the view.
The following figure describes the basic flow of MVP pattern.
Fig. MVP architecture
MVP Vs. Web Forms Vs. MVC
Web forms have high productivity and less control over markup, URL and code. MVC (Model View Controller) has high control on markup, URL and code and less productivity. Here comes the MVP which balances between the productivity and control.
Choosing Between MVC and MVP
In MVC, the model stores the data, view is the representation of that data and the controller allows the user to change the data. When the data is changed, all the views are notified of the change and they put themselves as necessary. MVP is a derivative of MVC.
MVP gives less control to the view and puts most of the responsibility in the presenter. It is an attempt to make all logic part of the presenter, thus the view is “passive”. The basic idea of the MVC and MVP is the same, the model stores the data, the view is representation of that data and the presenter coordinates the data.
Entity Framework Introduction
In the real world programming, the database developers prefer to use relational model to represent data while the application developers prefer to use object oriented models. This difference in two models is addressed by ADO.NET Entity Framework. The ADO.NET Entity Framework is a descendent of ADO.NET to provide object relational mapping between conceptual entities which are .NET classes and the data store. Entity framework uses entity data model which consists of storage schema, the conceptual schema, the mapping schema and the entity classes.
Process Flow
- At first, create a new ASP.NET empty web application in a VWD (Visual Web Developer). In my case, I have used VS 2010.
- After that, add a new interface for view names
IView
in file IView.cs. The code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestMVPApp.Model;
namespace TestMVPApp.View
{
public interface IView
{
string FirstName { get; set; }
string MiddleName { get; set; }
string LastName { get; set; }
string Address { get; set; }
string ContactNo { get; set; }
string Message { get; set; }
}
}
- Add a presenter class and name it as Presenter.cs which consists of the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestMVPApp.Model;
using TestMVPApp.View;
using System.Web;
using System.Configuration;
namespace TestMVPApp.Presenter
{
public class TestPresenter
{
IView detailsView;
public TestPresenter(IView view)
{
detailsView = view;
}
public string SaveDetails()
{
TestModel test = new TestModel();
if (test.SaveDetails(detailsView.FirstName,
detailsView.MiddleName, detailsView.LastName,
detailsView.Address, detailsView.ContactNo))
{
detailsView.Message = "Success";
}
else
{
detailsView.Message = "Failure";
}
detailsView.FirstName = string.Empty;
detailsView.MiddleName = string.Empty;
detailsView.LastName = string.Empty;
detailsView.Address = string.Empty;
detailsView.ContactNo = string.Empty;
return detailsView.Message.ToString();
}
}
}
- After adding presenter class, add a view page and name it as InputViewPage.aspx. In this UI page, create a table and fields for entering first name, middle name, last name, address and contact number.
And in the code behind, write the code to implement the interface IView
. The code for that is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestMVPApp.Presenter;
namespace TestMVPApp.View
{
public partial class InputViewPage : System.Web.UI.Page, IView
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnShowData_Click(object sender, EventArgs e)
{
TestPresenter detailsPresenter = new TestPresenter(this);
if (detailsPresenter.SaveDetails() != null)
{
Response.Redirect("DisplayViewPage.aspx");
}
}
public string FirstName
{
get { return txtFname.Text; }
set { txtFname.Text = value; }
}
public string MiddleName
{
get { return txtMName.Text; }
set { txtMName.Text = value; }
}
public string LastName
{
get { return txtLName.Text; }
set { txtLName.Text = value; }
}
public string Address
{
get { return txtAddress.Text; }
set { txtAddress.Text = value; }
}
public string ContactNo
{
get { return txtContact.Text; }
set { txtContact.Text = value; }
}
public string Message
{
get { return ltrlMessage.Text; }
set { ltrlMessage.Text = value; }
}
}
}
- Now it is time to write the model class. Before that, add a new Entity data model and name it and create a database connection accordingly. In the model class, write the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestMVPApp.Model
{
public interface IModel
{
List<tbl_userdetails> ReturnDetails();
}
public class TestModel : IModel
{
private Entities textContext = new Entities();
private Tbl_UserDetails testDetails;
private IEnumerable<tbl_userdetails> detailsList;
public TestModel() { }
public bool SaveDetails(string firstName,
string middleName, string lastName, string address,
string contactNo)
{
testDetails = new Tbl_UserDetails()
{
FirstName = firstName,
MiddleName = middleName,
LastName = lastName,
Address = address,
ContactNo = contactNo
};
textContext.AddToTbl_UserDetails(testDetails);
int testValue = textContext.SaveChanges();
if (testValue > 0)
{
return true;
}
else
{
return false;
}
}
public List<tbl_userdetails> ReturnDetails()
{
return detailsList.ToList<tbl_userdetails>();
}
}
}
- Now it is time to run the application, the following screen appears:
Fig. main page(View)
- Fill in the details and submit the data. If the data is successfully saved in the database, then the following screen appears:
Fig. Success screen
Conclusion
The article describes the ASP.NET MVP using Entity framework and guides you in a step by step manner. Happy coding.