Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expose properties of a user control in .NET for the MVP pattern
    text
    copied! <p>I am implementing a simple <code>UserControl</code> that is actually a fancy <code>TextBox</code>. 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 <code>"000"</code> and the contents are <code>"42"</code>, then <code>"042"</code> will appear.</p> <p>I am implementing this <code>UserControl</code> following the MVP pattern. The implementation is similar to this: <a href="https://stackoverflow.com/questions/7606955/how-implement-usercontrol-in-winforms-mvp-pattern/7607697#7607697">how Implement usercontrol in winforms mvp pattern?</a>. Also, check this question: <a href="https://stackoverflow.com/questions/2668673/passive-view-and-display-logic">passive view and display logic</a></p> <h2>Approach #1</h2> <p>My <code>Title</code> property in the <code>View</code> looks like this:</p> <pre class="lang-csh prettyprint-override"><code>private string title; public string Title { get { return title; } set { title = value; titleTextBox.Text = presenter.Format(title); } } </code></pre> <p>It is my feeling that this implementation adds unnecessary coupling. For example, if I change the <code>Presenter</code>'s <code>Format</code> method then I will have to go through all <code>View</code>s and change appropriately the call statement.</p> <h2>Approach #2</h2> <p>My <code>Title</code> property in the <code>View</code> looks like this:</p> <pre class="lang-csh prettyprint-override"><code>public string Title { get { return presenter.Title; } set { presenter.Title = value; } } </code></pre> <p>My <code>Title</code> property in the <code>Presenter</code> looks like this:</p> <pre class="lang-csh prettyprint-override"><code>private string title; public string Title { get { return title; } set { _view.SetTitle(this.Format(value); } } </code></pre> <p>Now I have to add the <code>SetTitle</code> method in the <code>View</code> interface and in the <code>View</code> implementation:</p> <pre class="lang-csh prettyprint-override"><code>public void SetTitle(string title) { titleTextBox.Text = title; } </code></pre> <p>So, with this approach I get this ugly <code>SetTitle</code> Java-like method.</p> <h2>Approach #3</h2> <p>Instead of calling <code>SetTitle</code> create a new property <code>RealTitle</code> in the <code>View</code> and set that. It remains ugly with this <code>Real</code> prefix.</p> <h2>Your approach</h2> <p>Can you think of a better way?</p> <h2>Use case</h2> <p>The <code>UserControl</code> should be used like this:</p> <pre class="lang-csh prettyprint-override"><code>var c = new FancyTextBox(); c.Format = "000"; c.Text = "42"; Controls.Add(c); </code></pre> <p>This snippet should display <code>"042"</code> inside the <code>UserControl</code>.</p> <h2>The big picture</h2> <pre class="lang-csh prettyprint-override"><code>Form FancyTextBoxView FancyTextBoxPresenter | | | | ftb.Text = "42" | | |--------------------&gt;| | | | | | | A | | |-----------------------------&gt;| | | | | | B | | |&lt;-----------------------------| </code></pre> <p>What are the A and B actions? I want the formatted text to appear in the UI. The formatting code is in the <code>Presenter</code>. The <code>View</code> has a <code>titleTextBox</code> that will hold the text in the UI.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload