Friday, February 15, 2008

How to correctly use MVP Design pattern

JHA has a presenter implementation in GMWBRiderControl.ascx, which uses GMWBPresenter. This presenter implementation has two problems:
(1) View are updated by function rather than properties, which makes Presenter stateless and unable to utilize overriding, retraction technique commonly seen in OOP/OOD.
(2) Missing commonly seem MVP elements such as IView and Processing() single entry point. This deviation from standard MVP result in a lot of business logic in ASP.Net Code-behind ( such as BindControls function in GMWBRiderControl.ascx.vb) --- completely defeated the purpuse of MVP to separately logic into Presenter.
To correctly this problem, the following changes are needed:

(1) Convert all function into property. e.g


Public Function ShowLifeTimeIncomeAmount() As Boolean
'
If IsGMWBIndicatorA _
OrElse IsGMWBIndicatorH _
Then
Return False
End If
'
Return True
End Function

would be changed to

Private _ShowLifeTimeIncomeAmount As Boolean
Public Property ShowLifeTimeIncomeAmount() As Boolean
Get
Return _ShowLifeTimeIncomeAmount
End Get
Set(ByVal value As Boolean)
_ShowLifeTimeIncomeAmount = value
End Set
End Property

all logic behind those function will be move into the function gettin gdata from Domain Model

Private Sub BindDataFromDomainModelToPresenter()
If IsGMWBIndicatorA _
OrElse IsGMWBIndicatorH _
Then
ShowLifeTimeIncomeAmount = False
End If
'
ShowLifeTimeIncomeAmount = True
' TODO: Change all the other function to property and call here to set it
End Sub


(2) Define "IView" and implement on User Control

Public Interface IGMWBView
Sub UpdateGMWBFields(ByVal IsVantageDown As Boolean)
Sub UpdateWithDrawAllowanceNewLabel(ByVal IsVantageDown As Boolean)
Sub UpdateWithdrawAmountInfo(ByVal IsVantageDown As Boolean)
Sub UpdateBenefitBase(ByVal IsVantageDown As Boolean)
Sub UpdatePaymentStepInfo(ByVal IsVantageDown As Boolean)
Sub MakeAllLIAFieldVisibleForBrokerClient(ByVal IsVantageDown As Boolean)
End Interface


Partial Class GmwbRiderControl
..
Implements IGMWBView


Public Sub UpdateGMWBFields(ByVal _IsVantageDown As Boolean) Implements IGMWBView.UpdateGMWBFields

' Move some code seqment from BindControls to here
End Sub

(3) Implementing business Execution login in Presenter


Public Overridable Sub UpdateGMWBView(ByVal view As IGMWBView)
' Pump Data from DO to P
BindDataFromDomainModelToPresenter()
' Invoke processing function implemented on ASP.Net code-Behind (VIEW)
view.UpdateGMWBFields(_IsVantageDown)
// other logic to change view
End Sub

(4) Call the single entry point on presenter "UpdateGMWBView" in user control in the function "BindControls"

The above steps will result in standard implementation of MVP.

No comments: