I am implementing a simple UserControl
that is actually a fancy TextBox
. One of its features is that you can set a formatting specification and this formatting get's applied to its contents automatically. For example, if you set the formatting specification to "000"
and the contents are "42"
, then "042"
will appear.
I am implementing this UserControl
following the MVP pattern. The implementation is similar to this: how Implement usercontrol in winforms mvp pattern?. Also, check this question: passive view and display logic
Approach #1
My Title
property in the View
looks like this:
private string title;
public string Title {
get { return title; }
set { title = value; titleTextBox.Text = presenter.Format(title); }
}
It is my feeling that this implementation adds unnecessary coupling. For example, if I change the Presenter
's Format
method then I will have to go through all View
s and change appropriately the call statement.
Approach #2
My Title
property in the View
looks like this:
public string Title {
get { return presenter.Title; }
set { presenter.Title = value; }
}
My Title
property in the Presenter
looks like this:
private string title;
public string Title {
get { return title; }
set { _view.SetTitle(this.Format(value); }
}
Now I have to add the SetTitle
method in the View
interface and in the View
implementation:
public void SetTitle(string title) {
titleTextBox.Text = title;
}
So, with this approach I get this ugly SetTitle
Java-like method.
Approach #3
Instead of calling SetTitle
create a new property RealTitle
in the View
and set that. It remains ugly with this Real
prefix.
Your approach
Can you think of a better way?
Use case
The UserControl
should be used like this:
var c = new FancyTextBox();
c.Format = "000";
c.Text = "42";
Controls.Add(c);
This snippet should display "042"
inside the UserControl
.
The big picture
Form FancyTextBoxView FancyTextBoxPresenter
| | |
| ftb.Text = "42" | |
|-------------------->| |
| | |
| | A |
| |----------------------------->|
| | |
| | B |
| |<-----------------------------|
What are the A and B actions? I want the formatted text to appear in the UI. The formatting code is in the Presenter
. The View
has a titleTextBox
that will hold the text in the UI.