Posts Tagged ‘MVP’

A Quick Look at Model-View-Presenter (MVP) Architecture

Monday, July 12th, 2010

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 remove lost of logic from UI layer.

Some noteworthy benefits of MVP pattern:

  • Platform independent: 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 later on required to any of the application portion convert in web to window or any other platform we can handle it by minimum efforts.
  • Loose couples form UI: So application stays loose couples from code behind page (UI) to business layer or any other next latter.
  • Useful to create automated unit test: MVP is purely loose couples from any plate form so that it is very use to prepare automated unit test cases.
  • Increase code maintainability: Keeps code more maintainable throughout the lifetime of the project, especially during the maintenance phase.

Overall Application Architecture:

mvp-diagram

  1. Model: Model is UI interface for how and what the data are needed 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 it’s 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.

I.e.

Public Interface Icustomer

ReadOnly Property CustomerName()

WriteOnly Property DisplayMessage()

End Interface

b. Code Behind (Model) – In this class we need to implement View.(No need to specify any logic). We are just implement properties which is defined in view.

#Region “Member”

Dim Presenter As CustomerPresenter

#End Region

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Presnter = New CustomerPresenter(Me)

Presnter.LoadData()

End Sub

ReadOnly Property CustomerName() Implements ICustomer.CustomerName

Get

Return textCustomerName.Text

End Get

End Property

WriteOnly Property DisplayMessage() Implements ICustomer.DisplayMessage

Set(ByVal value)

Response.Write(value)

End Set

End Property


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

End Sub

‘Public Sub New(ByVal view As ICustomer, ByVal SessionProvider As ISessionProvider)

‘ Me.iView = view

‘ Me.SessionProvider = SessionProvider

‘End Sub

Public Sub LoadData()

Me.iView.DisplayMessage = Me.iView.CustomerName

End Sub

End Class

Summary:

  • MVP is little prolonged process to implement and understanding in the beginning, but later on it will be very useful as it provides application maintainability on application maintenance phase, increase code re-usability. A good thing about it is a clear distinction of different layers.  For example, all the code is separate from the user interface.
  • MVP is a strong consideration candidate for large scale projects especially where project maintainability re-usability are the key success criteria. It suits well with automated test cases methods also.