Getting started with MVP Pattern in .NET

MVP is one of the latest patterns that is being used to develop applications in .NET. Not to be confused with MVC, MVP stands for Model-Viewer-Presenter. MVC is basically a framework, which has got its own syntax/concept of the coding and designing. On the other hand, MVP is a kind of pattern which is used to separate out the layers of the code, with the code being written in the same way as in normal .NET applications.

For this article, I will create a simple application, that displays the sum of 2 numbers, using the MVP pattern. To create the sample application, we could have also used the WCSF (Web Client Software Factory) tool, which can generate a sample template of the application, with the separation of the layers. But we will be creating the application with the layers in our own way. So let’s start with creating a new project application.

Our project will consist of three layers :

View : This is the UI part which will be getting displayed to the user. User will be providing his input here and getting the output.

Presenter :  This will act as the intermediate layer between the Model and the View. It will receive the input from the view and pass the same to the model. Then it will receive the output/results from the model and send it back to the UI.

Model : The model will perform the database or any logical operations for which we received the input from the UI via Presenter.

So let’s start by creating an interface for the View named IDefaultView. This interface will be implemented by our Default.cs page and will use the properties defined in the interface to send the data from the View or UI to the Presenter and get the data back from there. Add two textboxes, a label and a button to the Default.aspx page.

So our interface and its implementation will be like the screenshot below :


As you can see above, we have the interface defined on the left side and its implementation on the right side in the View or the UI. Here the properties are being implemented to set the initial calculation values.

Next, add DefaultPresenter.cs where we will be passing the data from the UI to the model(Model.cs). So our Presenter and the model will look like the following :


So on the left we have the Presenter with constructor  where the View or the UI input is getting passed to. It also has a method CalculateSum() which is further calling the Sum() method in the Model on the right. So the presenter method is called from the view, which in turn, calls the Model method, calculates and returns the result to the presenter, which sets it to the View. So here, there is NO direct interaction between the View and the Model. The presenter acts as an intermediate between the two.

Finally, in the UI part, on the click of the button, we will be passing the View to the Presenter and calling its method. So our code will look like the following :


Finally run the application and enter the two numbers and check the results.


So here we created a sample application using the MVP pattern. Like i mentioned above, we could have created the sample template using the WCSF tool, but that would not have served our purpose of understanding the flow of the data from one layer to another, so easily. So hope you now get the basic idea of using the MVP pattern. Happy Coding…!!!


About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in MVC & MVP. Bookmark the permalink.

I have a suggestion..

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

You are commenting using your 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