Thursday, 27 September 2012

Introduction to Model View Presenter Architecture


Model View Presenter

Model-View-Presenter (MVP) is a variation of the Model-View-Controller (MVC) pattern but specifically geared towards a page event model such as ASP.NET. It is a design pattern used for web as well as windows application. It is a user interface design pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic.
Components of MVP
Model: The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.
View: The view is an interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
Presenter: The presenter acts upon the model and the view. It retrieves data from repositories (the model), persists it, and formats it for display in the view.

Benefits of Model-View-Presenter
The main purpose of the Model-View-Presenter (MVP) design pattern is to separate domain and application-specific logic (the Model) from the user interface (the View) of the application. The advantages of this separation of concerns are:

1. The user interface can be changed independently of the Model in many cases where the application requirements have not fundamentally changed. For example, to enhance usability it may be desirable to change a spin control to a slider control. Similarly, the actual implementation of the Model may be changed to use a different persistence model or to use a smarter algorithm

2. Last but not least, the separation of domain logic from the user interface greatly facilitates unit testing of the code.


Creating an application through MVP architecture with ADO.net
In order to create an application following MVP architecture, you have to follow the following steps:
Step-1. Start VS 2010 and click on File>New Project and choose ASP.Net web application option from dialog.

Step-2. Create a folder named as Model in the root directory and create an Interface IDataModel and a class DataModel that handles the database interaction.

Step-3. Create another folder named as View in root directory and create an interface IDataView, it contains all the properties and methods that display data on the page.

Step-4. Create another folder named as Presenter and create a class named as DataPresenter that contains the method that fetch data from model and pass it to the view.

Step-5. In web config file Create ConnectionString as follows:
<ConnectionStrings>
<add name=”cn” ConnectionString=”server=. ; Database=Sample; Integrated Security=true; />
</ConnectionStrings>

Step-5. Create a Web Form and take two textboxes and two labels named as FirstName and LastName and two button controls Save and Display. On click on the save button data is inserted into table and on click on Display button data is displayed in a grid.
Step-6.Implement IDataView interface on web form





No comments:

Post a Comment