Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET

Entity Framework Implementation Using ASP.NET MVP

Rate me:
Please Sign up or sign in to vote.
4.07/5 (10 votes)
13 May 2011GPL33 min read 56.4K   389   17   11
Entity Framework Implementation using ASP.NET MVP

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:

  1. Model
  2. View
  3. 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.

Image 1

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

  1. At first, create a new ASP.NET empty web application in a VWD (Visual Web Developer). In my case, I have used VS 2010.
  2. After that, add a new interface for view names IView in file IView.cs. The code looks like this:
    C#
    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; }
         }
    }
  3. Add a presenter class and name it as Presenter.cs which consists of the following code:
    C#
    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();
    		}
    	}
    }
  4. 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:

    C#
    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; }
            }       
        }
    }
  5. 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:
    C#
    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()
    		{
    			//detailsList = from v in 
    			//textContext.GetDetails() select v;
    			return detailsList.ToList<tbl_userdetails>();
    		}
    	}
    }
  6. Now it is time to run the application, the following screen appears:

    mainpage.JPG

    Fig. main page(View)
  7. Fill in the details and submit the data. If the data is successfully saved in the database, then the following screen appears:

    success.JPG

    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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Nepal Nepal
I am a software developer with knowledge of C, C++, PHP, MySQL, C#.Net, ADO.Net, MSSQL Server, Crystal Reports, Asp.Net, Oracle, Asp.Net MVP, MVC, jQuery, javascript, WebService, WCF, Entity Framework and so on. I love to play on New Technologies.

Comments and Discussions

 
GeneralMy vote of 2 Pin
After205020-May-11 3:07
After205020-May-11 3:07 
GeneralRe: My vote of 2 Pin
Neerajan Lamsal10-Jun-11 2:33
Neerajan Lamsal10-Jun-11 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.