<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>ProgramGood.Net - MVP</title>
    <link>http://www.programgood.net/</link>
    <description>The journey to becoming a great programmer</description>
    <language>en-us</language>
    <copyright>Dave Mateer</copyright>
    <lastBuildDate>Thu, 16 Jul 2009 02:38:59 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>davemateer@gmail.com</managingEditor>
    <webMaster>davemateer@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.programgood.net/Trackback.aspx?guid=a4d78d60-874e-44ed-9fab-22a5620db068</trackback:ping>
      <pingback:server>http://www.programgood.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.programgood.net/PermaLink,guid,a4d78d60-874e-44ed-9fab-22a5620db068.aspx</pingback:target>
      <dc:creator>Dave Mateer</dc:creator>
      <wfw:comment>http://www.programgood.net/CommentView,guid,a4d78d60-874e-44ed-9fab-22a5620db068.aspx</wfw:comment>
      <wfw:commentRss>http://www.programgood.net/SyndicationService.asmx/GetEntryCommentsRss?guid=a4d78d60-874e-44ed-9fab-22a5620db068</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Easy to understand, working example WinForms
app using MVP (Model View Presenter) pattern.<br /><br />
Why use this pattern?  Testability and maintainability (ie loosely coupled so
less likely to break!)<br /><br />
Download <a href="http://www.programgood.net/content/binary/noddy.zip">noddy.zip (106.62
KB)</a> (VS2008)<br /><br />
Notes:<br />
View is dumb and does render logic<br />
View never talks to the model (or any business entity eg customer) directly<br />
Presenter retries data and manipulates<br />
Model is the data and business rules<br /><br />
usually MVP sits on top of an ORM or Database Abstraction / Business Logic layer:<br /><img src="http://www.programgood.net/content/binary/Drawing1.jpg" border="0" /><br /><br />
How it works:<br /><br />
1) Application comes to Program.cs which calls Form1.cs.. no MVP code here.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Program
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
The main entry point for the application.</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
Standard code.. nothing MVP</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span> [STAThread] <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Main()
{ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>);
Application.Run(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Form1());
} }</span></pre><br />
2) Form1 (which is the view in MVP) which implements IView interface (**why**).. instantiates
a new MainPresenter calling it presenter.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> partial <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Form1
: Form, IView { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> MainPresenter
presenter; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> Form1()
{ InitializeComponent(); presenter <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> MainPresenter(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>);
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> btnPostCustomer_Click(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> sender,
EventArgs e) { presenter.PostCustomer(); } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
IView members</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> FirstName
{ get { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> txtFirstName.Text;
} set { txtFirstName.Text <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> value;
} } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> LastName
{ get { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> txtLastName.Text;
} set { txtLastName.Text <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> value;
} } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> DisplayResult(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> result)
{ MessageBox.Show(result); } }</span></pre>3) MainPresenter constructor runs, and
is passed in the view object of type IView. (**could we have just passed it in as
a Form object?**)<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> MainPresenter
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> IView
view; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> MainModel
model <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> MainModel(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> MainPresenter(IView
view) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.view <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> view;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> PostCustomer()
{ Debug.Assert(view !<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">null</span>); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">try</span> {
Customer customer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Customer(view.FirstName,
view.LastName); model.PostCustomer(customer); view.DisplayResult(customer.ToString() <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"
posted"</span>); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">catch</span>(Exception
ex) { view.DisplayResult(ex.Message); } } }</span></pre>4) App is run, names are filled
in, and Post button is submitted.<br /><p></p><img src="http://www.programgood.net/content/binary/screendump.gif" border="0" /><br /><br />
5) View code (form1.cs) runs presenter.PostCustomer<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> btnPostCustomer_Click(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> sender,
EventArgs e) { presenter.PostCustomer(); }</span></pre>6) presenter creates a new
customer (which could be seen as being outside of MVP, and more in an entity later,
as the model layer is usually light, and only relating the modelviews) and passes
in the names.  Nothing special on the constructor of customer.  
<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> PostCustomer()
{ Debug.Assert(view !<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">null</span>); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">try</span> {
Customer customer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Customer(view.FirstName,
view.LastName); model.PostCustomer(customer); view.DisplayResult(customer.FullName() <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"
posted"</span>); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">catch</span>(Exception
ex) { view.DisplayResult(ex.Message); } }<br /></span></pre>7) Presenter calls the main model to PostCustomer which writes out to
disk the Customer which has just been entered.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> PostCustomer(Customer
customer) { XmlSerializer serializer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> XmlSerializer(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">typeof</span>(Customer)); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> (File.Exists(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Customer.xml"</span>))
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (FileStream
stream <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> FileStream(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Customer.xml"</span>,
FileMode.Append, FileAccess.Write)) { serializer.Serialize(stream, customer); } } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">else</span> { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (FileStream
stream <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> FileStream(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Customer.xml"</span>,
FileMode.Create, FileAccess.Write)) { serializer.Serialize(stream, customer); } }
}</span></pre><br /><br /><br />
8) Presenter calls the view to DisplayResult, passing in the customer data as a string.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> DisplayResult(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> result)
{ MessageBox.Show(result); }</span></pre><br /><br />
Included in the download are simple tests on the ViewModel and Domain model. 
Also Form2 which has a Form2Presenter.  Uses same ViewModel (hmm probably should
have a seperate one), and the same domain model (customer.cs).  Next step for
me is to get tests working on the Presenters.<br /><br /><br /><br /><br /><a href="http://www.programgood.net/content/binary/noddy.zip"></a><img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=a4d78d60-874e-44ed-9fab-22a5620db068" /></body>
      <title>Simple MVP</title>
      <guid isPermaLink="false">http://www.programgood.net/PermaLink,guid,a4d78d60-874e-44ed-9fab-22a5620db068.aspx</guid>
      <link>http://www.programgood.net/2009/07/16/SimpleMVP.aspx</link>
      <pubDate>Thu, 16 Jul 2009 02:38:59 GMT</pubDate>
      <description>Easy to understand, working example WinForms app using MVP (Model View Presenter) pattern.&lt;br&gt;
&lt;br&gt;
Why use this pattern?&amp;nbsp; Testability and maintainability (ie loosely coupled so
less likely to break!)&lt;br&gt;
&lt;br&gt;
Download &lt;a href="http://www.programgood.net/content/binary/noddy.zip"&gt;noddy.zip (106.62
KB)&lt;/a&gt; (VS2008)&lt;br&gt;
&lt;br&gt;
Notes:&lt;br&gt;
View is dumb and does render logic&lt;br&gt;
View never talks to the model (or any business entity eg customer) directly&lt;br&gt;
Presenter retries data and manipulates&lt;br&gt;
Model is the data and business rules&lt;br&gt;
&lt;br&gt;
usually MVP sits on top of an ORM or Database Abstraction / Business Logic layer:&lt;br&gt;
&lt;img src="http://www.programgood.net/content/binary/Drawing1.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
How it works:&lt;br&gt;
&lt;br&gt;
1) Application comes to Program.cs which calls Form1.cs.. no MVP code here.&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; Program
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;///
The main entry point for the application.&lt;/span&gt; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;///
Standard code.. nothing MVP&lt;/span&gt; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; [STAThread] &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Main()
{ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;);
Application.Run(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Form1());
} }&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
2) Form1 (which is the view in MVP) which implements IView interface (**why**).. instantiates
a new MainPresenter calling it presenter.&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; partial &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; Form1
: Form, IView { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; MainPresenter
presenter; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; Form1()
{ InitializeComponent(); presenter &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; MainPresenter(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;);
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; btnPostCustomer_Click(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; sender,
EventArgs e) { presenter.PostCustomer(); } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
IView members&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; FirstName
{ get { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; txtFirstName.Text;
} set { txtFirstName.Text &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; value;
} } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; LastName
{ get { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; txtLastName.Text;
} set { txtLastName.Text &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; value;
} } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; DisplayResult(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; result)
{ MessageBox.Show(result); } }&lt;/span&gt;&lt;/pre&gt;3) MainPresenter constructor runs, and
is passed in the view object of type IView. (**could we have just passed it in as
a Form object?**)&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; MainPresenter
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; IView
view; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; MainModel
model &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; MainModel(); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; MainPresenter(IView
view) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.view &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; view;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; PostCustomer()
{ Debug.Assert(view !&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;try&lt;/span&gt; {
Customer customer &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Customer(view.FirstName,
view.LastName); model.PostCustomer(customer); view.DisplayResult(customer.ToString() &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;+&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"
posted"&lt;/span&gt;); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;catch&lt;/span&gt;(Exception
ex) { view.DisplayResult(ex.Message); } } }&lt;/span&gt;&lt;/pre&gt;4) App is run, names are filled
in, and Post button is submitted.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.programgood.net/content/binary/screendump.gif" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
5) View code (form1.cs) runs presenter.PostCustomer&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; btnPostCustomer_Click(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; sender,
EventArgs e) { presenter.PostCustomer(); }&lt;/span&gt;&lt;/pre&gt;6) presenter creates a new
customer (which could be seen as being outside of MVP, and more in an entity later,
as the model layer is usually light, and only relating the modelviews) and passes
in the names.&amp;nbsp; Nothing special on the constructor of customer.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; PostCustomer()
{ Debug.Assert(view !&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;null&lt;/span&gt;); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;try&lt;/span&gt; {
Customer customer &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Customer(view.FirstName,
view.LastName); model.PostCustomer(customer); view.DisplayResult(customer.FullName() &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;+&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"
posted"&lt;/span&gt;); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;catch&lt;/span&gt;(Exception
ex) { view.DisplayResult(ex.Message); } }&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;7) Presenter calls the main model to PostCustomer which writes out to
disk the Customer which has just been entered.&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; PostCustomer(Customer
customer) { XmlSerializer serializer &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; XmlSerializer(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;typeof&lt;/span&gt;(Customer)); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (File.Exists(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Customer.xml"&lt;/span&gt;))
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; (FileStream
stream &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; FileStream(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Customer.xml"&lt;/span&gt;,
FileMode.Append, FileAccess.Write)) { serializer.Serialize(stream, customer); } } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;else&lt;/span&gt; { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; (FileStream
stream &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; FileStream(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Customer.xml"&lt;/span&gt;,
FileMode.Create, FileAccess.Write)) { serializer.Serialize(stream, customer); } }
}&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
8) Presenter calls the view to DisplayResult, passing in the customer data as a string.&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; DisplayResult(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; result)
{ MessageBox.Show(result); }&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
Included in the download are simple tests on the ViewModel and Domain model.&amp;nbsp;
Also Form2 which has a Form2Presenter.&amp;nbsp; Uses same ViewModel (hmm probably should
have a seperate one), and the same domain model (customer.cs).&amp;nbsp; Next step for
me is to get tests working on the Presenters.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.programgood.net/content/binary/noddy.zip"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=a4d78d60-874e-44ed-9fab-22a5620db068" /&gt;</description>
      <comments>http://www.programgood.net/CommentView,guid,a4d78d60-874e-44ed-9fab-22a5620db068.aspx</comments>
      <category>MVP</category>
      <category>Patterns</category>
      <category>Testing</category>
    </item>
  </channel>
</rss>