As UI-creation technologies such as ASP.NET and Windows® Forms become more and more powerful, it’s common practice to let the UI layer do more than it should. Without a clear separation of responsibilities, the UI layer can often become a catch-all for logic that really belongs in other layers of the application. One design pattern, the Model View Presenter (MVP) pattern, is especially well suited to solving this problem
Benefits of MVP:
- In this Pattern application make independent from its platform so when ever requirement comes to move to window to web or any other version its only need to change on code behind page for view Implementation.
- So application stays loose couples from code behind page with business layer or any other next latter.
- MVP is purely loose couples from any plate form so that it is very use to prepare automated unit test cases.
- Keeps code more maintainable throughout the lifetime of the project, especially during the maintenance phase.
Overall application architecture:
MVP Class Diagram:
1. Model: Model is UI interface for how and what are the data need to displayed.
2. View : Show the representation of model, that can be build in presenter
3. Presenter : Build all data for model and pass all data to model in form of view by communicating with services class( or business logic layer)
Below is the MVP patterns example with service class
1. User Interface Layer
In this section contains actual UI, In .net terms its aspx or ascx pages.
2. Presentation Layer( MVP :- Code behind + view (interfaces) + Presenter)
In this layer we have designed as MVP patters so its divided in below three parts.
a. View – It is interfaces. This interface contains signature of all properties and events that needs to implement in code behind page that will be use in presenter class.
Public Interface Icustomer
ReadOnly Property CustomerName()
WriteOnly Property DisplayMessage()
b. Code Behind – In this class we need to implement View.(No need to specify any logic). We are just implement properties which is defined in view.
Dim Presnter As CustomerPresenter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Presnter = New CustomerPresenter(Me)
ReadOnly Property CustomerName() Implements ICustomer.CustomerName
WriteOnly Property DisplayMessage() Implements ICustomer.DisplayMessage
c. Presenter – In this class we are using all those members which we have specified in view and implemented in code behind. Presenter class is responsible for execute methods of business layer.
Public Class CustomerPresenter
Private iView As ICustomer
Private SessionProvider As ISessionProvider
Public Sub New(ByVal view As ICustomer)
Me.iView = view
‘Public Sub New(ByVal view As ICustomer, ByVal SessionProvider As ISessionProvider)
‘ Me.iView = view
‘ Me.SessionProvider = SessionProvider
Public Sub LoadData()
Me.iView.DisplayMessage = Me.iView.CustomerName
ü MVP is some how lengthy process to implement and understanding on initial phase, but later on that will be very useful to application maintainability on application maintenance phase, increase code reusability because all code is separate from UI.
ü So all projects MVP is not a best approaches but yes on some large scale project and we need to think on project maintainability, reusability and also required automated test cases methods that case this is best approaches for this scenario.