<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Michael Carman</title>
	<atom:link href="http://mscarman.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscarman.wordpress.com</link>
	<description>Tales from the Heap</description>
	<lastBuildDate>Thu, 09 Jul 2009 21:47:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mscarman.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Michael Carman</title>
		<link>http://mscarman.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mscarman.wordpress.com/osd.xml" title="Michael Carman" />
	<atom:link rel='hub' href='http://mscarman.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Controller Actions Revisited</title>
		<link>http://mscarman.wordpress.com/2009/07/09/controller-actions-revisited/</link>
		<comments>http://mscarman.wordpress.com/2009/07/09/controller-actions-revisited/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 21:47:28 +0000</pubDate>
		<dc:creator>mscarman</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Controller Actions]]></category>

		<guid isPermaLink="false">http://mscarman.wordpress.com/?p=10</guid>
		<description><![CDATA[Last month I posted about separating actions into their own classes.  Recently, I got a chance to try this concept out on a small project.  The results, so far, have been promising.  Here is a sample action controller: The conventions that we are using for the action controllers are: Create one ActionController class for each unique URL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mscarman.wordpress.com&amp;blog=8248566&amp;post=10&amp;subd=mscarman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last month I posted about <a href="http://mscarman.wordpress.com/2009/06/22/my-ever-changing-controllers/">separating actions into their own classes</a>.  Recently, I got a chance to try this concept out on a small project.  The results, so far, have been promising.  Here is a sample action controller:</p>
<p><pre class="brush: csharp;">
public class EmployeeIndexAction() : ActionController
{
    private readonly IRepository&lt;Employee&gt; m_employeeRepository;
 
    public AlbumIndex(IRepository&lt;Employee&gt; employeeRepository)
    {
        m_employeeRepository= employeeRepository;
    }
 
    public ActionResult Get()
    {
        var employees = m_employeeRepository.FindAll();
        return View(employees);
    }
}
</pre></p>
<p>The conventions that we are using for the action controllers are:</p>
<ul>
<li>Create one ActionController class for each unique URL (controller and action).</li>
<li>Name the class using a combination of the controller name and action name with a suffix of &#8220;Action&#8221; or more succinctly {controller}{action}Action.</li>
<li>Create a method for each Http Verb (Get, Post, Put &#8230;) that is supported for the URL.</li>
</ul>
<p>The next code example illustrates a typical edit scenario.  It also shows that the action controllers still have support for binding parameters.  The URL for this example is ~/employee/edit.</p>
<p><pre class="brush: csharp;">
public class EmployeeEditAction : ActionController
{   
    public ActionResult Get(int id)
    {
        EmployeeForm form = new EmployeeForm();
        // code to load employee and map to a EmployeeForm instance
        return View(form);
    }
   
    public ActionResult Post(EmployeeForm form)
    {
        if (!ModelState.IsValid)
        {
            return View(form);
        }
       
        // code to map and save employee
       
        return RedirectToAction(&quot;Index&quot;);
    }
}
</pre></p>
<p>I created a sample application that uses action controllers.  You can download the application at <a href="http://mscarman.googlecode.com/files/SampleSite.zip">http://mscarman.googlecode.com/files/SampleSite.zip</a>.</p>
<p>Jeffrey Palermo also posted about this subject last month.  If you haven&#8217;t seen his post, <a href="http://jeffreypalermo.com/blog/the-asp-net-mvc-actioncontroller-ndash-the-controllerless-action-or-actionless-controller/">check it out</a>.  His approach is a little different. But the general idea is the same and the comments are very informative as well.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mscarman.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mscarman.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mscarman.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mscarman.wordpress.com&amp;blog=8248566&amp;post=10&amp;subd=mscarman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mscarman.wordpress.com/2009/07/09/controller-actions-revisited/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/92fbdc16d43a0377cb0e65f44b0bc16b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mscarman</media:title>
		</media:content>
	</item>
		<item>
		<title>My Ever Changing Controllers</title>
		<link>http://mscarman.wordpress.com/2009/06/22/my-ever-changing-controllers/</link>
		<comments>http://mscarman.wordpress.com/2009/06/22/my-ever-changing-controllers/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 02:11:11 +0000</pubDate>
		<dc:creator>mscarman</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://mscarman.wordpress.com/2009/06/22/my-ever-changing-controllers/</guid>
		<description><![CDATA[On my current project, we are applying the Open Close Principle in our architecture.  Some of inspiration for this came from Ayende’s post Composite Architecture &#8211; The Open Close Principle as applied to system architecture.  In part of his post, he writes about how each new feature was implemented with new code and didn’t require [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mscarman.wordpress.com&amp;blog=8248566&amp;post=4&amp;subd=mscarman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On my current project, we are applying the <a href="http://en.wikipedia.org/wiki/Open/closed_principle" target="_blank">Open Close Principle</a> in our architecture.  Some of inspiration for this came from Ayende’s post <a href="http://ayende.com/Blog/archive/2008/12/23/composite-architecture-the-open-close-principle-as-applied-to.aspx">Composite Architecture &#8211; The Open Close Principle as applied to system architecture</a>.  In part of his post, he writes about how each new feature was implemented with new code and didn’t require touching existing code.</p>
<p>Applying this principal to our project has been working out very well.  For example, we are using the specification pattern with our repositories.  As a result we no longer need to add new methods to our repositories every time a new requirement comes along where we need to apply a different criteria.  We are having similar successes in other parts of our project.</p>
<p>One area where we haven’t been as successful is with our controllers.  When adding new features, there were many instances where we needed to add methods to existing controllers.  At times, these new methods would also add new dependencies to the controller.  Basically, the source for our controllers is not closed for changes.</p>
<p>As an alternative, we started looking at mapping each URL or action to a <code>ControllerAction</code> class.  For example, the URL /register/form would map to the <code>RegisterFormAction</code> class.  By default, action classes execute the method that matches the request type (GET, POST, PUT, etc …).  So if the request is a <code>GET</code> request, then the <code>Get</code> method is executed.  If there are any parameters for the <code>Get</code> method, they are mapped to the values passed in the query string.  The following is code for a sample action.</p>
<div class="csharpcode">
<pre class="alt"><span class="rem">// url: ~/register/form</span>
<span class="kwrd">public</span> <span class="kwrd">class</span> RegisterFormAction : ControllerAction</pre>
<pre class="alt">{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Class&gt; m_classRepository;
    <span class="kwrd">public</span> RegisterFormAction(IRepository&lt;Class&gt; classRepository)</pre>
<pre class="alt">    {
        m_classRepository = classRepository;</pre>
<pre class="alt">    }</pre>
<pre class="alt">    <span class="kwrd">public</span> ActionResult Get()
    {</pre>
<pre class="alt">        var model = <span class="kwrd">new</span> RegistrationForm();
        <span class="kwrd">return</span> ShowForm(model);</pre>
<pre class="alt">    }</pre>
<pre class="alt">    <span class="kwrd">public</span> ActionResult Post(RegistrationForm model)
    {</pre>
<pre class="alt">        <span class="kwrd">if</span> (!ModelState.IsValid)
        {</pre>
<pre class="alt">            <span class="kwrd">return</span> ShowForm(model);
        }
        <span class="rem">// TODO: map and save registration </span>
        <span class="rem">// redirect to ~/registration/confirmation</span></pre>
<pre class="alt">        <span class="kwrd">return</span> RedirectToAction(<span class="str">"Confirmation"</span>);
    }
    <span class="kwrd">private</span> ActionResult ShowForm(RegistrationModel model)</pre>
<pre class="alt">    {
        var filter = <span class="kwrd">new</span> ActiveClassFilter();</pre>
<pre class="alt">        model.Classes = m_classRepository.FetchFor(                                      filter,                                       OrderBy&lt;Class&gt;(c =&gt; c.ClassName));
        <span class="kwrd">return</span> View(model);</pre>
<pre class="alt">    }
}</pre>
</div>
<p>We are still debating whether we want to go this route in our project. The debate typically centers around these two questions: Are we going to far in applying the Open Closed principal? Or, are we just afraid to break from the ASP.NET MVC convention of having controller classes?  I’m starting to lean towards the latter.</p>
<p>I also noticed the other people are looking in the same direction.  Recently, Chad Myers blogged about <a href="http://www.lostechies.com/blogs/chad_myers/archive/2009/06/18/going-controller-less-in-mvc-the-way-fowler-meant-it.aspx">Going Controller-less in MVC: The Way Fowler Meant It To Be</a>.  It’s always nice to know that other people are thinking the same way you are <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If we decide to go this route, I add more posts detailing what we implement.  Until then, I love to know what other people think.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mscarman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mscarman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mscarman.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mscarman.wordpress.com&amp;blog=8248566&amp;post=4&amp;subd=mscarman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mscarman.wordpress.com/2009/06/22/my-ever-changing-controllers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/92fbdc16d43a0377cb0e65f44b0bc16b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mscarman</media:title>
		</media:content>
	</item>
	</channel>
</rss>
