What rules do you use to delineate MVP methods and members
-
23-08-2019 - |
Question
When using the MVP pattern, I often come across methods and members which don't seem to fall nicely within the View or Presenter classes...My question is: What rules do you use to decide what functionality lies which classes? I am relatively new to MVP, so please humour me.
TIA.
Solution
I tend to favor the Passive View variant of MVP so this is a non issue for me. In passive view pattern the View pretty much delegates anything more complex than a simple assignment to the presenter.
You wind up with a pattern that looks like this:
public class MyView: IView
{
private MyPresenter Presenter;
private OnEvent()
{
Presenter.DoSomething();
}
public string MyProperty
{
get{ return UIControl.Property;}
set{ UIControl.Property = value}
}
}
public interface IView
{
public string MyProperty{ get; set;}
}
public class MyPresenter
{
private IView view;
public void DoSomething()
{
...
view.MyProperty = something;
}
}
The only trick part is if you have a datagrid on your form. These require a lot of work to fit into a Passive View pattern.
OTHER TIPS
It boils down to how much manipulation of the UI is going on. If the method consist a lot of direct access to individual controls then likely it belongs on the presenter. Otherwise it belongs on the view. The goal is to reduce the interaction between the view and the present to the minimum needed to fulfill the design of the software.
For example
Presenter.SetListTitle MyList.Name
For I = View.MyListStart to View.MyListEnd
Presenter.AddListItem MyList(I)
Next I
Presenter.ShowListAddBUtton
Presenter.ShowListDelButton
Should be placed in the presenter as below
Public Sub UpdateWithList(MyList as AList, View as AView)
Me.SetListTitle MyList.Name
For I = View.MyListStart to View.MyListEnd
Me.AddListItem MyList(I)
Next I
Me.ShowListAddBUtton
Me.ShowListDelButton
End Sub
Later if you decided to change your UI all you have to worry about is implementing UpdateWithList not SetListTitle,AddListItem, etc, etc.