<?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 - MVC</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>Fri, 17 Jul 2009 20:16:06 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=55ec44ed-b372-4d8b-a792-36192bb7fb9e</trackback:ping>
      <pingback:server>http://www.programgood.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.programgood.net/PermaLink,guid,55ec44ed-b372-4d8b-a792-36192bb7fb9e.aspx</pingback:target>
      <dc:creator>Dave Mateer</dc:creator>
      <wfw:comment>http://www.programgood.net/CommentView,guid,55ec44ed-b372-4d8b-a792-36192bb7fb9e.aspx</wfw:comment>
      <wfw:commentRss>http://www.programgood.net/SyndicationService.asmx/GetEntryCommentsRss?guid=55ec44ed-b372-4d8b-a792-36192bb7fb9e</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">A simple MVC (Model View Controller) implementation..code
by Jimmy Chandra<br />
http://stackoverflow.com/questions/1107720/mvc-c-simplest-possible-implementation<br /><br />
Why use MVC?  Similar reasons to MVP - testability and seperation of concerns<br /><br /><a href="http://www.programgood.net/content/binary/Simple.MVC.zip">Simple.MVC.zip
(167.25 KB)</a><br /><br /><img src="http://www.programgood.net/content/binary/Drawing4.gif" border="0" /><br /><br />
1. Program.cs runs, running Main.. as is standard in a Win Forms app.  Difference
from MVP is that main here is instantiating and running a controller class first of
all, instead of newing up the view, which calls the controller/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;">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>);
TopEmployeeController controller <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> TopEmployeeController();
Application.Run(controller.View <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> Form);
}</span></pre>2. Controller is instantiated and constructor called:<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> ITopEmployeeView
_view; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> Employee
_employee; <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;">bool</span> _monitoring; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> TopEmployeeController()
{ _employee <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> Employee();
_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;">new</span> TopEmployeeForm(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>,
_employee); }<br /></span></pre>3. Controller news up an employee (dumb).. then a view of type TopEmployeeForm,
passing its self (the controller) and employee to it.<br /><br />
4. View (the observer) subscribes to the model (the subject) so that when model.OnPropertyChange
is called, view.UpdateView is called.  This is the observer pattern, made easier
in C# using events.. which is using a delegate. <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;"></span><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
view</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> TopEmployeeForm(ITopEmployeeController
controller, Employee model) { _controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> controller;
_model <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> model; <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Let
the model know that this view is interested if the model change</span> _model.OnPropertyChange
+= <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Action(UpdateView);
InitializeComponent(); </span></pre>
}</span></pre>6. When Button is pressed on the view.  The controller.GetTopEmplyee
method is called.  This calls model.MontorChanges:<br /><pre><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"></span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
controller</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;">void</span> GetTopEmployee()<br />
{<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> (!_monitoring)<br />
{<br />
_monitoring <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;">true</span>;<br />
_employee.MonitorChanges();<br />
}<br />
}</span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /><br /><br /></span>7. MonitorChanges does some waiting code, then every second calls FirePropertyChange().
Which calls model.OnPropertyChange. 
<br />
Which is really a delegate to View.UpdateView<br /></pre><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
model</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;">event</span> Action
OnPropertyChange; <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> FirePropertyChange()
{ var propChange <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> OnPropertyChange; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> (propChange
!<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>)
{ OnPropertyChange(); } }<br /><br /></span></pre>8. The view method which runs every second and displays the name on the
form.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
view</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;">void</span> UpdateView()
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
to do with threading</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.InvokeRequired)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.Invoke(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Action(UpdateView));
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">else</span> {
TopEmployeeName <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> _model.FullName;
} }</span></pre><br /><br /><p></p><br /><a href="http://www.programgood.net/content/binary/Simple.MVC.zip"><br /></a><img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=55ec44ed-b372-4d8b-a792-36192bb7fb9e" /></body>
      <title>Simple MVC in WinForms</title>
      <guid isPermaLink="false">http://www.programgood.net/PermaLink,guid,55ec44ed-b372-4d8b-a792-36192bb7fb9e.aspx</guid>
      <link>http://www.programgood.net/2009/07/17/SimpleMVCInWinForms.aspx</link>
      <pubDate>Fri, 17 Jul 2009 20:16:06 GMT</pubDate>
      <description>A simple MVC (Model View Controller) implementation..code by Jimmy Chandra&lt;br&gt;
http://stackoverflow.com/questions/1107720/mvc-c-simplest-possible-implementation&lt;br&gt;
&lt;br&gt;
Why use MVC?&amp;nbsp; Similar reasons to MVP - testability and seperation of concerns&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.programgood.net/content/binary/Simple.MVC.zip"&gt;Simple.MVC.zip
(167.25 KB)&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;img src="http://www.programgood.net/content/binary/Drawing4.gif" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
1. Program.cs runs, running Main.. as is standard in a Win Forms app.&amp;nbsp; Difference
from MVP is that main here is instantiating and running a controller class first of
all, instead of newing up the view, which calls the controller/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;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;);
TopEmployeeController controller &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; TopEmployeeController();
Application.Run(controller.View &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; Form);
}&lt;/span&gt;&lt;/pre&gt;2. Controller is instantiated and constructor called:&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; ITopEmployeeView
_view; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; Employee
_employee; &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;bool&lt;/span&gt; _monitoring; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; TopEmployeeController()
{ _employee &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; Employee();
_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;new&lt;/span&gt; TopEmployeeForm(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;,
_employee); }&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;3. Controller news up an employee (dumb).. then a view of type TopEmployeeForm,
passing its self (the controller) and employee to it.&lt;br&gt;
&lt;br&gt;
4. View (the observer) subscribes to the model (the subject) so that when model.OnPropertyChange
is called, view.UpdateView is called.&amp;nbsp; This is the observer pattern, made easier
in C# using events.. which is using a delegate. &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;&lt;/span&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
view&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; TopEmployeeForm(ITopEmployeeController
controller, Employee model) { _controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; controller;
_model &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; model; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Let
the model know that this view is interested if the model change&lt;/span&gt; _model.OnPropertyChange
+= &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Action(UpdateView);
InitializeComponent(); &lt;/span&gt;&lt;/pre&gt;
}&lt;/span&gt;&lt;/pre&gt;6. When Button is pressed on the view.&amp;nbsp; The controller.GetTopEmplyee
method is called.&amp;nbsp; This calls model.MontorChanges:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
controller&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;void&lt;/span&gt; GetTopEmployee()&lt;br&gt;
{&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (!_monitoring)&lt;br&gt;
{&lt;br&gt;
_monitoring &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;true&lt;/span&gt;;&lt;br&gt;
_employee.MonitorChanges();&lt;br&gt;
}&lt;br&gt;
}&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;7. MonitorChanges does some waiting code, then every second calls FirePropertyChange().
Which calls model.OnPropertyChange. 
&lt;br&gt;
Which is really a delegate to View.UpdateView&lt;br&gt;
&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
model&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;event&lt;/span&gt; Action
OnPropertyChange; &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; FirePropertyChange()
{ var propChange &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; OnPropertyChange; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (propChange
!&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;)
{ OnPropertyChange(); } }&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;8. The view method which runs every second and displays the name on the
form.&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: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
view&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;void&lt;/span&gt; UpdateView()
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
to do with threading&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.InvokeRequired)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.Invoke(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Action(UpdateView));
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;else&lt;/span&gt; {
TopEmployeeName &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; _model.FullName;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;a href="http://www.programgood.net/content/binary/Simple.MVC.zip"&gt;
&lt;br&gt;
&lt;/a&gt;&lt;img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=55ec44ed-b372-4d8b-a792-36192bb7fb9e" /&gt;</description>
      <comments>http://www.programgood.net/CommentView,guid,55ec44ed-b372-4d8b-a792-36192bb7fb9e.aspx</comments>
      <category>MVC</category>
      <category>Object Oriented Programming</category>
      <category>Patterns</category>
      <category>Testing</category>
    </item>
  </channel>
</rss>