When to go for MVP (Model View Presenter) design pattern in .NET
Hello friends in this article we are going to give understanding of ASP.NET based design pattern - MVP i.e. - Model View Presenter pattern with in this article. Let me just quickly give an introduction
- Why there is a need of any design pattern in your software development project,
- What do you mean by design pattern
- How to select the best design pattern that suits your business and development needs
Need of Design Pattern:
You might have read different articles on different blogs regarding different design pattern and also seen different patterns. Patterns are primarily useful to overcome any bottlenecks that you may encounter in the future. Pattern is nothing but following some basic standard rules of coding in defined architecture.
The general design patterns are creation, structural and behavior patterns which includes sub patterns of each.
Design pattern are nothing but a structural and standard manner of coding which makes life of a developer very easy, enables reusability of code and improves maintenance / maintainability of project.
Design Pattern Selection:
The design pattern can be selected based on the business rules of application to be implemented.
In this article we are going to explain the MVP pattern. MVP is mainly used when you want your application to be loosely coupled with each other. Dependency of multiple tiers of application for input and output is loosely coupled or binded. This can be done with the help of using interface instead of abstract class.
Following are some case in which you can go for the interface instead of abstract class
- When the application being developed is of small or medium size
- When we are using wide range of different or distinct objects
You should use Abstract class for the following set of application
- When your are developing multi version application
- When you want to implement common functionality among multiple modules of application then you should go for abstract class architecture
MVP Pattern Description
We have created one show case application for listing of products, product details & news section by implementing the MVP pattern. This application is integrated in the starter kit of the Sitecore CMS as a presentation layer.
We will demonstrate all the operation for the news section. You can use the same to implement for any other module into your project.
Please refer the below image which has a different project structure of showcase application.
Create the project structure as given in the image above.
We are going to explain the implementation of the same from model-presenter-presentation. First of all we are going to explain SCShowcase.Repository project. This project mainly contains the entity class – create properties of all the required inputs for the news section. Generally it contains the properties same as that of news table column list. We have created the same as displayed in image below.
Now create your interface, provide proper naming like INewsRepository were “I” is prefix for the interface, news is for the section and repository for the project it belongs to.
The interface mainly includes the declaration of the methods required to perform Add, Update & Delete operation in general case. After that create the news repository which implement the interface INewsRepository in to it and also contains the routine for the methods which are declared in to the interface. You can write as many functions you want to call on presentation layer in to this. We have also integrated the Base interface to perform general utility functions.
Once you complete doing this, we need to go for the presenter layer. Normally the presenter layer declare the instance of the INewsRepository and the NewsRepository with the help of service locator. Please see the code given in the following image.
We are declaring the INewsView interface which we will implement in the presentation layer. This interface should includes the parameter that we are going to provide as a inputs by the end user from the presentation layer that is from the user control or aspx page. The second image contains the methods that we will make call from the presentation layer like DisplayNews & PopulateNewsListSettings. This methods make call to the repository project to perform data base operations which behave as model.
Finally, It comes to presentation layer which implement the presenter interface and make call to the methods that we declared into the presenter which make call to your repository which is model. In this manner the call are made from one tier to another and the response are sent back the it is requested. Here I have used some variable to take path of the SiteCoreCMS application which you can eliminate as per requirement. Also we have again declared the variables in my ascx control the one I have declared in my presenter view. This is required because we are implementing the presenter interface.
Please see below image which includes the presentation layer call using ascx control.