<?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/"
	>

<channel>
	<title>Joshua Arnold &#187; tutorial</title>
	<atom:link href="http://www.joshua-arnold.com/tags/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joshua-arnold.com</link>
	<description>Random thoughts on software development, life, and the art of growing old.</description>
	<lastBuildDate>Wed, 03 Feb 2010 00:07:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>FubuToDo &#8211; Part 2: Forms, Controllers, View, and jQuery</title>
		<link>http://www.joshua-arnold.com/2010/01/fubutodo-part-2-forms-controllers-view-and-jquery/</link>
		<comments>http://www.joshua-arnold.com/2010/01/fubutodo-part-2-forms-controllers-view-and-jquery/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 23:10:32 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[fubumvc]]></category>
		<category><![CDATA[fubutodo]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.joshua-arnold.com/?p=262</guid>
		<description><![CDATA[I wanted to create a simple “To Do” application using FubuMVC. In my previous post, I showed the basics for getting your controllers created, tested, and wired up. I also explained how to setup your conventions and how to bootstrap your application. This time, I’m going to talk about creating forms, actions on controllers to respond to those forms, and how to work in some jQuery.]]></description>
			<content:encoded><![CDATA[<p>This is the second post in a <a href="http://www.joshua-arnold.com/tags/fubutodo/" target="_blank">series of posts</a> I am writing on creating a simple “To-Do” application using <a href="http://fubumvc.com/" target="_blank">FubuMVC</a>. I’ve broken down this series into the following:</p>
<ol>
<li><a href="http://www.joshua-arnold.com/2010/01/fubutodo-part-1-conventions-opinions-and-bootstrapping/" target="_blank">Part 1: Conventions, Opinions and Bootstrapping</a></li>
<li><strong>Part 2: Forms, Controllers, Views, and jQuery </strong></li>
<li><a title="FubuMVC Example - Creating a To-Do application using FubuMVC (Part 3)" href="http://www.proace.com/2010/01/24/fubutodo-part-3-persistence/">Part 3: Persistence</a></li>
</ol>
<h3>Overview</h3>
<p>I wanted to create a simple “To Do” application using FubuMVC. In my <a href="http://www.joshua-arnold.com/2010/01/fubutodo-part-1-conventions-opinions-and-bootstrapping/" target="_blank">previous post</a>, I showed the basics for getting your controllers created, tested, and wired up. I also explained how to setup your conventions and how to bootstrap your application. This time, I’m going to talk about creating forms, actions on controllers to respond to those forms, and how to work in some jQuery.</p>
<p>In order to have a functioning “To-Do” application, we’re missing the following:</p>
<ol>
<li>Add/Edit/Remove Functionality</li>
<li>Validation</li>
<li>Persistence</li>
</ol>
<p>We’re going to talk about #1 in this part.</p>
<h3>Controllers</h3>
<p>As always, let’s start with some tests. I like to group entity-related actions into more semantic urls (e.g., “Items/Add”, “Items/Edit”). I’m just going to group those actions in a single controller so I came up with the following tests for my ItemsController:</p>
<ol>
<li>When adding an item, the controller calls Insert on the IUnitOfWork</li>
<li>When adding an item, the appropriate ViewModel is returned</li>
<li>When editing an item, the controller calls Update on the IUnitOfWork</li>
<li>When editing an item, the appropriate ViewModel is returned</li>
<li>When removing an item, the controller calls Delete on the IUnitOfWork</li>
<li>When removing an item, the appropriate ViewModel is returned</li>
</ol>
<p>Now, I know that I want to implement the add/edit/remove functionality via a modal dialogue. I’ve found I can write reusable javascript handlers for responses like this when the result is all the same for CRUD-based operations. To make it easier, I’ve decided to make the Add/Edit/Remove methods return an instance of the JsonUnitOfWorkResult class:</p>
<p><a name="JsonUnitOfWorkResult"></a></p>
<p><strong>JsonUnitOfWorkResult</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Represents the result of a unit of work.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">JsonUnitOfWorkResult
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets a message describing the result.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public string </span>Message { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets a flag indicating whether the unit of work was successful.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public bool </span>Success { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And now a sample of our tests:</p>
<p><strong>Test 1</strong></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">When adding an item, the controller calls Insert on the IUnitOfWork.
</span><span style="color: gray">/// &lt;/summary&gt;
</span>[<span style="color: #2b91af">Test</span>]
<span style="color: blue">public void </span>Add_Item_Calls_Insert()
{
    <span style="color: #2b91af">IUnitOfWork </span>unitOfWork = _mockRepository.StrictMock&lt;<span style="color: #2b91af">IUnitOfWork</span>&gt;();
    <span style="color: #2b91af">ItemsController </span>controller = <span style="color: blue">new </span><span style="color: #2b91af">ItemsController</span>(unitOfWork,
        _mockRepository.DynamicMock&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;());

    <span style="color: blue">using</span>(_mockRepository.Ordered())
    {
        unitOfWork.Expect(uow =&gt; uow.Insert(<span style="color: blue">null</span>)).IgnoreArguments();
        unitOfWork.Expect(uow =&gt; uow.Commit());
    }

    _mockRepository.ReplayAll();

    <span style="color: #2b91af">AddItemInputModel </span>inputModel = <span style="color: blue">new </span><span style="color: #2b91af">AddItemInputModel
    </span>{
        Description = <span style="color: #a31515">"Hello, World!"
    </span>};

    controller.Add(inputModel);

    _mockRepository.VerifyAll();
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong> </strong></p>
<p><strong>Test 2</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">When removing an item, the appropriate ViewModel is returned.
</span><span style="color: gray">/// &lt;/summary&gt;
</span>[<span style="color: #2b91af">Test</span>]
<span style="color: blue">public void </span>Remove_Item_Sets_Result()
{
    <span style="color: #2b91af">IUnitOfWork </span>unitOfWork = _mockRepository.StrictMock&lt;<span style="color: #2b91af">IUnitOfWork</span>&gt;();
    <span style="color: #2b91af">ItemsController </span>controller = <span style="color: blue">new </span><span style="color: #2b91af">ItemsController</span>(unitOfWork,
        _mockRepository.DynamicMock&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;());

    <span style="color: #2b91af">RemoveItemInputModel </span>inputModel = <span style="color: blue">new </span><span style="color: #2b91af">RemoveItemInputModel
    </span>{
        ItemId = 1
    };

    <span style="color: #2b91af">JsonUnitOfWorkResult </span>result = controller.Remove(inputModel);
    <span style="color: #2b91af">Assert</span>.IsNotNull(result);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>As I’ve mentioned before, Fubu has the notion of “one model in, one model out” – following this gives you a lot of control over your behaviors which I’ll explain later in this post. To make things a little easier, I created an abstract class for all of my CRUD action input models:</p>
<p><strong>ItemInputModel</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides a base class for all </span><span style="color: gray">&lt;see cref="ToDoItem"/&gt; input </span><span style="color: green">models.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public abstract class </span><span style="color: #2b91af">ItemInputModel
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets the unique identifier of the item.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public int </span>ItemId { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets the description of the item.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public string </span>Description { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The actual controller implementation is incredibly easy (admittedly I could’ve done it better). Here’s the Add method:</p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Adds a new item.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;param name="itemInputModel"&gt;</span><span style="color: green">The item to add.</span><span style="color: gray">&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color: blue">public </span><span style="color: #2b91af">JsonUnitOfWorkResult </span>Add(<span style="color: #2b91af">AddItemInputModel </span>itemInputModel)
{
    <span style="color: blue">try
    </span>{
        _unitOfWork.Insert(<span style="color: blue">new </span><span style="color: #2b91af">ToDoItem
                               </span>{
                                   Description = itemInputModel.Description
                               });

        _unitOfWork.Commit();

        <span style="color: blue">return new </span><span style="color: #2b91af">JsonUnitOfWorkResult
                   </span>{
                       Success = <span style="color: blue">true</span>,
                       Message = <span style="color: #a31515">"Item added successfully"
                   </span>};
    }
    <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>exc)
    {
        <span style="color: blue">return new </span><span style="color: #2b91af">JsonUnitOfWorkResult
                   </span>{
                       Success = <span style="color: blue">false</span>,
                       Message = exc.Message
                   };
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h3>Forms</h3>
<p>Ok, we’ve got a working controller. Now let’s setup our forms. I’m going to use jQuery and jQuery UI to handle my dialogs. Let’s take a look at what I wanted the application to do:</p>
<ol>
<li>Display a list of to-do items</li>
<li>Allow me to add a new item</li>
<li>Allow me to specify that an item is completed or not</li>
<li>Allow me to edit an existing item</li>
<li>Allow me to remove an existing item</li>
</ol>
<p>We can already do the first requirement. However, since I’m planning on doing the rest through modals, we know we’re going to have to respond to some commands and refresh that list. Let’s revisit the Home/Index.aspx view and refactor our the displaying of those items:</p>
<p><strong>Step 1: Move to a partial view</strong></p>
<p><em>ItemListViewModel</em></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides a model containing a list of </span><span style="color: gray">&lt;see cref="ToDoItem"/&gt; </span><span style="color: green">entities.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">ItemListViewModel
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets the associated collection of items.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">ToDoItem</span>&gt; Items { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><em>List.aspx</em></p>
<pre class="code"><span style="background: #ffee62">&lt;%</span><span style="color: blue">@ </span><span style="color: #a31515">Page </span><span style="color: red">Language</span><span style="color: blue">="C#" </span><span style="color: red">AutoEventWireup</span><span style="color: blue">="true" </span><span style="color: red">CodeBehind</span><span style="color: blue">="List.aspx.cs" </span><span style="color: red">Inherits</span><span style="color: blue">="FubuToDo.Web.Controllers.Item.List" </span><span style="background: #ffee62">%&gt;
&lt;%</span><span style="color: blue">@ </span><span style="color: #a31515">Import </span><span style="color: red">Namespace</span><span style="color: blue">="System.Linq"</span><span style="background: #ffee62">%&gt;
&lt;%</span> <span style="color: blue">if</span>(Model.Items.Any()) { <span style="background: #ffee62">%&gt;
</span><span style="color: blue">&lt;</span><span style="color: #a31515">ul </span><span style="color: red">class</span><span style="color: blue">="item-list"&gt;
</span><span style="background: #ffee62">&lt;%</span> <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>Model.Items) { <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;</span><span style="color: #a31515">li </span><span style="color: red">id</span><span style="color: blue">="</span><span style="background: #ffee62">&lt;%</span>= item.ItemId <span style="background: #ffee62">%&gt;</span><span style="color: blue">" </span><span style="color: red">class</span><span style="color: blue">="item"&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="row no-label"&gt;
            &lt;</span><span style="color: #a31515">input </span><span style="color: red">type</span><span style="color: blue">="checkbox" </span><span style="color: red">name</span><span style="color: blue">="Item-</span><span style="background: #ffee62">&lt;%</span>= item.ItemId <span style="background: #ffee62">%&gt;</span><span style="color: blue">" </span><span style="color: red">&lt;%= item.IsComplete ? "checked=\"checked\"" : string.Empty %&gt; </span><span style="color: blue">/&gt;
            &lt;</span><span style="color: #a31515">label </span><span style="color: red">class</span><span style="color: blue">="checkbox"&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="javascript:return false;"&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>item.Description <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">label</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">br </span><span style="color: red">class</span><span style="color: blue">="cboth" /&gt;
    &lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;
</span><span style="color: blue">&lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%</span> } <span style="color: blue">else </span>{ <span style="background: #ffee62">%&gt;
</span><span style="color: blue">&lt;</span><span style="color: #a31515">span</span><span style="color: blue">&gt;</span>No items.<span style="color: blue">&lt;/</span><span style="color: #a31515">span</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Step 2: Defer list rendering to an ajax call</strong></p>
<p>In order to do this, I removed the Items property off of the IndexViewModel and removed the list rendering from the Home/Index.aspx view. Let’s take a look at the following snippets:</p>
<p><a name="Home-Index-Aspx"></a></p>
<p><em>Index.aspx</em></p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="blog-entry-container"&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="success" </span><span style="color: red">style</span><span style="color: blue">="</span><span style="color: red">display</span>:<span style="color: blue">none</span>;<span style="color: blue">"&gt;&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="error" </span><span style="color: red">style</span><span style="color: blue">="</span><span style="color: red">display</span>:<span style="color: blue">none</span>;<span style="color: blue">"&gt;&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>To-Do List<span style="color: blue">&lt;/</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="Item-List"&gt;
    &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">button </span><span style="color: red">class</span><span style="color: blue">="button-prim-med" </span><span style="color: red">type</span><span style="color: blue">="button" </span><span style="color: red">value</span><span style="color: blue">="Add" </span><span style="color: red">id</span><span style="color: blue">="Add-New"&gt;
        &lt;</span><span style="color: #a31515">span</span><span style="color: blue">&gt;</span>Add New<span style="color: blue">&lt;/</span><span style="color: #a31515">span</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">button</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="ItemDialog" </span><span style="color: red">style</span><span style="color: blue">="</span><span style="color: red">display</span>: <span style="color: blue">none</span>;<span style="color: blue">"&gt;
    &lt;</span><span style="color: #a31515">form </span><span style="color: red">id</span><span style="color: blue">="frmItem" </span><span style="color: red">method</span><span style="color: blue">="post"&gt;
        &lt;</span><span style="color: #a31515">input </span><span style="color: red">type</span><span style="color: blue">="hidden" </span><span style="color: red">id</span><span style="color: blue">="hdnItemId" </span><span style="color: red">name</span><span style="color: blue">="ItemId" /&gt;
        &lt;</span><span style="color: #a31515">fieldset</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="row"&gt;
                &lt;</span><span style="color: #a31515">label</span><span style="color: blue">&gt;
                    </span>Description:<span style="color: blue">&lt;/</span><span style="color: #a31515">label</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">input </span><span style="color: red">type</span><span style="color: blue">="text" </span><span style="color: red">id</span><span style="color: blue">="Item-Description" </span><span style="color: red">name</span><span style="color: blue">="Description" </span><span style="color: red">class</span><span style="color: blue">="text required" /&gt;
            &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">fieldset</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">form</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>Now the Home/Index.aspx also references the Scripts/home/index.js file which will handle loading this information. Let’s take a look:</p>
<p><em>home.js</em></p>
<pre class="code"><span style="color: blue">function </span>reloadItems() {
    <span style="color: blue">var </span>$list = $(<span style="color: #a31515">'#Item-List'</span>);
    $list.html(<span style="color: #a31515">'&lt;span&gt;Loading...&lt;/span&gt;'</span>);

    $.ajax({
        url: <span style="color: #a31515">'/Items/List'</span>,
        type: <span style="color: #a31515">'GET'</span>,
        contentType: <span style="color: #a31515">"application/json; charset=utf-8"</span>,
        data: { random: <span style="color: blue">new </span>Date().getUTCMilliseconds() },
        success: <span style="color: blue">function</span>(data) {
            $list.html(data);
            bindEditLinks();            bindItemCheckboxes();
        },
        timeout: 5000,
        error: <span style="color: blue">function</span>(xmlObj, textStatus, errorThrown) {
            $list.html(<span style="color: #a31515">'&lt;span&gt;Error&lt;/span&gt;'</span>);
        }
    });
};</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Our Items/List.aspx view returns a simple html snippet. Since we haven’t told Fubu any differently, it treats it as HTML and the appropriate content type is set. Our simple GET request will receive this content and set the contents of that empty div we created in our Home/Index.aspx view. If you take a look at the file a little deeper, you’ll see I call reloadItems on document ready.</p>
<blockquote><p><em>You might have noticed the additional “random” parameter that we’re sending in. This this is a GET request, most browsers will cache the result. I threw this in there to avoid bang-head-into-desk syndrome.</em></p></blockquote>
<p>Ok, now when I run it I get my items rendering through my ajax call. Now to wire up my dialogs and get it all working.</p>
<p><strong>Adding/Editing an Item</strong></p>
<p>I’ve setup the form in a way that could be used to add or edit an item. Since our input models are virtually identical, all we have to do is change the action of the form. We already have our dialog worked out (see <a href="#Home-Index-Aspx">Home/Index.aspx</a>) so let’s wire up our buttons and links:</p>
<p><em>Wiring up Add Item</em></p>
<pre class="code"><span style="color: blue">function </span>addItem() {
    <span style="color: blue">var </span>itemForm = $(<span style="color: #a31515">'#frmItem'</span>);
    <span style="color: blue">var </span>itemDialog = $(<span style="color: #a31515">'#ItemDialog'</span>);
    itemForm.clearForm();

    <span style="color: green">// set up buttons
    </span>itemDialog.dialog(<span style="color: #a31515">'option'</span>, <span style="color: #a31515">'buttons'</span>, {
        <span style="color: #a31515">'Create'</span>: <span style="color: blue">function</span>() {
            itemForm.attr(<span style="color: #a31515">'action'</span>, <span style="color: #a31515">'/Items/Add'</span>);
            itemForm.submit();
        },
        Cancel: <span style="color: blue">function</span>() {
            itemForm.clearForm();
            $(<span style="color: blue">this</span>).dialog(<span style="color: #a31515">'close'</span>);
        }
    });
    itemDialog.dialog(<span style="color: #a31515">'option'</span>, <span style="color: #a31515">'title'</span>, <span style="color: #a31515">'Add Item'</span>);

    <span style="color: green">// show the dialog
    </span>itemDialog.dialog(<span style="color: #a31515">'open'</span>);
};</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><em>Wiring up Edit Item</em></p>
<pre class="code"><span style="color: blue">function </span>bindEditLinks() {
    $(<span style="color: #a31515">'.item-list &gt; .item &gt; div.row &gt; label &gt; a'</span>).click(<span style="color: blue">function</span>() {
        <span style="color: blue">var </span>itemForm = $(<span style="color: #a31515">'#frmItem'</span>);
        <span style="color: blue">var </span>itemDialog = $(<span style="color: #a31515">'#ItemDialog'</span>);

        <span style="color: blue">var </span>listItemId = $(<span style="color: blue">this</span>).parent().parent().parent().attr(<span style="color: #a31515">'id'</span>);
        itemForm.find(<span style="color: #a31515">'#hdnItemId'</span>).val(listItemId);
        itemForm.find(<span style="color: #a31515">'#Item-Description'</span>).val($(<span style="color: blue">this</span>).html());

        <span style="color: green">// set up buttons
        </span>itemDialog.dialog(<span style="color: #a31515">'option'</span>, <span style="color: #a31515">'buttons'</span>, {
            <span style="color: #a31515">'Save'</span>: <span style="color: blue">function</span>() {
                itemForm.attr(<span style="color: #a31515">'action'</span>, <span style="color: #a31515">'/Items/Edit'</span>);
                itemForm.submit();
            },
            <span style="color: #a31515">'Remove'</span>: <span style="color: blue">function</span>() {
                itemForm.attr(<span style="color: #a31515">'action'</span>, <span style="color: #a31515">'/Items/Remove'</span>);
                itemForm.submit();
            },
            Cancel: <span style="color: blue">function</span>() {
                itemForm.clearForm();
                $(<span style="color: blue">this</span>).dialog(<span style="color: #a31515">'close'</span>);
            }
        });
        itemDialog.dialog(<span style="color: #a31515">'option'</span>, <span style="color: #a31515">'title'</span>, <span style="color: #a31515">'Edit Item'</span>);

        <span style="color: green">// show the dialog
        </span>itemDialog.dialog(<span style="color: #a31515">'open'</span>);
    });
};</pre>
<p><em>Wiring up Item Complete:</em></p>
<pre class="code"><span style="color: blue">function </span>bindItemCheckboxes() {
    $(<span style="color: #a31515">".item-list &gt; .item &gt; div.row &gt; input[type='checkbox']"</span>).click(<span style="color: blue">function</span>() {
        <span style="color: blue">var </span>listItemId = $(<span style="color: blue">this</span>).parent().parent().attr(<span style="color: #a31515">'id'</span>);
        <span style="color: blue">var </span>description = $(<span style="color: blue">this</span>).parent().find(<span style="color: #a31515">'label &gt; a'</span>).html();

        $.ajax({
            url: <span style="color: #a31515">'/Items/Edit'</span>,
            type: <span style="color: #a31515">'POST'</span>,
            dataType: <span style="color: #a31515">'json'</span>,
            data: {
                IsComplete: document.getElementById($(<span style="color: blue">this</span>).attr(<span style="color: #a31515">'id'</span>)).checked,
                ItemId: listItemId,
                Description: description
            },
            success: <span style="color: blue">function</span>(response) {
                jsonTransactionHandler(response);
            },
            error: <span style="color: blue">function</span>(responseObj, textError, errorCode) {
                $(<span style="color: #a31515">'.error'</span>).html(textError).fadeIn(<span style="color: #a31515">'slow'</span>, <span style="color: blue">function</span>() {
                    setTimeout(<span style="color: blue">function</span>() { $(<span style="color: #a31515">'.error'</span>).fadeOut(); }, 4000);
                });
            }
        });
    });
};</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Three things here to note here:</p>
<ol>
<li>bindEditLinks is called in the reloadItems function.</li>
<li>bindItemCheckboxes is also called in the reloadItems function. This simply submits a POST message to the Edit action with the appropriate fields.</li>
<li>The ‘Remove’ button sets the action of the form to ‘Items/Remove’ and submits it which will successfully remove the item.</li>
</ol>
<p>Let’s take a look at how we have our form being submitted and handled:</p>
<p><em>Form Initialization</em></p>
<pre class="code"><span style="color: green">// initialize the form
</span>$(<span style="color: #a31515">'#frmItem'</span>).ajaxSubmit({
    dataType: <span style="color: #a31515">'json'</span>,
    success: <span style="color: blue">function</span>(response) {
        jsonTransactionHandler(response);
        $(<span style="color: #a31515">'#ItemDialog'</span>).dialog(<span style="color: #a31515">'close'</span>);
        reloadItems();
    },
    error: <span style="color: blue">function</span>(responseObj, textError, errorCode) {
        $(<span style="color: #a31515">'.error'</span>).html(textError).fadeIn(<span style="color: #a31515">'slow'</span>, <span style="color: blue">function</span>() {
            setTimeout(<span style="color: blue">function</span>() { $(<span style="color: #a31515">'.error'</span>).fadeOut(); }, 4000);
        });
    }
});</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><em>Result Handler</em></p>
<pre class="code"><span style="color: blue">function </span>jsonTransactionHandler(response) {
    <span style="color: blue">if </span>(response.Success) {
        $(<span style="color: #a31515">'.success'</span>).html(response.Message);
        $(<span style="color: #a31515">'.success'</span>).fadeIn(<span style="color: #a31515">'slow'</span>, <span style="color: blue">function</span>() {
            setTimeout(<span style="color: blue">function</span>() { $(<span style="color: #a31515">'.success'</span>).fadeOut(); }, 4000);
        });
    } <span style="color: blue">else </span>{
        $(<span style="color: #a31515">'.error'</span>).html(response.Message);
        $(<span style="color: #a31515">'.error'</span>).fadeIn(<span style="color: #a31515">'slow'</span>, <span style="color: blue">function</span>() {
            setTimeout(<span style="color: blue">function</span>() { $(<span style="color: #a31515">'.error'</span>).fadeOut(); }, 4000);
        });
    }
};</pre>
<p>Any successful HTTP response from the controller will trigger the jsonTransactionHandler (ff you remember, we set up our <a href="#JsonUnitOfWorkResult">JsonUnitOfWorkResult</a> to let us handle all responses the same way). The dialog then gets closed and the items get reloaded to reflect the changes.</p>
<h3>Getting it to run</h3>
<p>There are only two changes we’ll need to make to get these changes up and running w/ Fubu:</p>
<p><strong>FubuStructureMapBoostrapper (line 32):</strong></p>
<pre class="code"><span style="color: #2b91af">ObjectFactory</span>.Initialize(x =&gt;
                             {
                                 x.For&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;().Use&lt;<span style="color: #2b91af">FakeToDoItemRepository</span>&gt;();
                                 x.For&lt;<span style="color: #2b91af">IUnitOfWork</span>&gt;().Use&lt;<span style="color: #2b91af">FakeUnitOfWork</span>&gt;();
                             });</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>We’re registering a fake unit of work for our ItemsController.</p>
<p><strong>FubuToDoRegistry (last line of the constructor):</strong></p>
<pre class="code">JsonOutputIf.WhenTheOutputModelIs&lt;<span style="color: #2b91af">JsonUnitOfWorkResult</span>&gt;();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This tells Fubu that we want a JSON response whenever it encounters our <a href="#JsonUnitOfWorkResult">JsonUnitOfWorkResult</a> class.</p>
<p>That’s it! We’re all done. In my next part I’ll talk implementing validation and switching over our repository and unit of works classes to enable some real persistence.</p>
<h3>Source code</h3>
<p>You can download the source for this part at: <a title="http://svn.joshua-arnold.com/fubutodo/tags/part2" href="http://svn.joshua-arnold.com/fubutodo/tags/part2">http://svn.joshua-arnold.com/fubutodo/tags/part2</a></p>
<span class="akst_link"><a href="http://www.joshua-arnold.com/?p=262&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_262"  class="akst_share_link">Share</a>
</span>
<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/fubumvc' rel='tag' target='_self'>fubumvc</a>, <a class='technorati-link' href='http://technorati.com/tag/fubutodo' rel='tag' target='_self'>fubutodo</a>, <a class='technorati-link' href='http://technorati.com/tag/tutorial' rel='tag' target='_self'>tutorial</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.joshua-arnold.com/2010/01/fubutodo-part-2-forms-controllers-view-and-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>FubuToDo &#8211; Part 1: Conventions, Opinions, and Bootstrapping</title>
		<link>http://www.joshua-arnold.com/2010/01/fubutodo-part-1-conventions-opinions-and-bootstrapping/</link>
		<comments>http://www.joshua-arnold.com/2010/01/fubutodo-part-1-conventions-opinions-and-bootstrapping/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 21:05:09 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[fubumvc]]></category>
		<category><![CDATA[fubutodo]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.joshua-arnold.com/?p=251</guid>
		<description><![CDATA[Building off of Tim’s directions, I created a simple but more involved application to show some jQuery magic and really just get started with the framework. I’ve broken down this series into the following:

   1. Part 1: Conventions, Opinions and Bootstrapping
   2. Part 2: Forms, Controllers, Views, and jQuery
   3. Part 3: Validation and Persistence]]></description>
			<content:encoded><![CDATA[<p>With the recent activity from the <a title="FubuMVC" href="http://fubumvc.com/" target="_blank">FubuMVC</a> team, I thought that I would help others get started by providing some samples. <a title="Tim Tyrrell" href="http://twitter.com/timtyrrell/" target="_blank">Tim Tyrrell</a> started us off with his two posts:</p>
<ol>
<li><a href="http://blog.timtyrrell.net/2010/01/hello-world-with-fubumvc-super-quick.html" target="_blank">“Hello World” with FubuMVC (Super Quick Start)</a></li>
<li><a href="http://blog.timtyrrell.net/2010/01/hello-world-with-fubumvc-without.html" target="_blank">“Hello World” with FubuMVC (without the training wheels)</a></li>
</ol>
<p>Building off of Tim’s directions, I created a simple but more involved application to show some jQuery magic and really just get started with the framework. I’ve broken down this series into the following:</p>
<ol>
<li><strong>Part 1: Conventions, Opinions and Bootstrapping </strong></li>
<li><a title="FubuToDo - Part 2: Forms, Controllers, Views, and jQuery" href="http://www.joshua-arnold.com/2010/01/fubutodo-part-2-forms-controllers-view-and-jquery/" target="_blank">Part 2: Forms, Controllers, Views, and jQuery</a></li>
<li><a title="FubuMVC Example - Creating a To-Do application using FubuMVC (Part 3)" href="http://www.proace.com/2010/01/24/fubutodo-part-3-persistence/">Part 3: Persistence</a></li>
</ol>
<h3>Overview</h3>
<p>I wanted to create a simple “To Do” application using FubuMVC. I threw together some quick css and figured I’d make something similar to figure 1.1.:</p>
<p><a href="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure1.1" src="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.1_thumb.png" border="0" alt="Figure1.1" width="244" height="176" /></a></p>
<p><em>Figure 1.1 &#8211; Wireframe</em></p>
<p>One important thing to note is that I built this application using a very specific approach: don’t think too much. A lot of the refactoring that you’ll see me do came to mind before I wrote the original pieces (and I’m sure you’ll see it too) but I decided to just stay the course and see what Fubu can do.</p>
<p>Each “Part” in this series has a corresponding tag in the repository I used which I’ll link to at the end of each article.</p>
<h3>Getting Started</h3>
<p>I started with the tests for my home controller. I wasn’t too sure how I wanted everything to work just yet so I came up with the following cases to test against:</p>
<ol>
<li>Given some input model (“IndexInputModel”), the Home controller should return a instance of “IndexViewModel” and it should not be null.</li>
<li>When the home controller creates the “IndexViewModel”, it should set the “Items” property.</li>
</ol>
<p>Let’s take a look at those tests:</p>
<p><strong>Test 1</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Given some input model ("IndexInputModel"), the Home controller should return a instance of "IndexViewModel" and it should not be null.
</span><span style="color: gray">/// &lt;/summary&gt;
</span>[<span style="color: #2b91af">Test</span>]
<span style="color: blue">public void </span>Index_Sets_ViewModel()
{
    <span style="color: #2b91af">IToDoItemRepository </span>itemRepository = _mockRepository.DynamicMock&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;();
    <span style="color: #2b91af">HomeController </span>controller = <span style="color: blue">new </span><span style="color: #2b91af">HomeController</span>(itemRepository);

    <span style="color: #2b91af">IndexViewModel </span>viewModel = controller.Index(<span style="color: blue">new </span><span style="color: #2b91af">IndexInputModel</span>());
    <span style="color: #2b91af">Assert</span>.IsNotNull(viewModel);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Test 2</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">When the home controller creates the "IndexViewModel", it should set the "Items" property.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public void </span>Index_Sets_ViewModel_Items()
{
    <span style="color: #2b91af">IToDoItemRepository </span>itemRepository = _mockRepository.DynamicMock&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;();
    <span style="color: #2b91af">HomeController </span>controller = <span style="color: blue">new </span><span style="color: #2b91af">HomeController</span>(itemRepository);

    <span style="color: blue">using</span>(_mockRepository.Ordered())
    {
        itemRepository.Expect(r =&gt; r.GetAll()).Return(<span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">ToDoItem</span>&gt;());
    }

    _mockRepository.ReplayAll();

    <span style="color: #2b91af">IndexViewModel </span>viewModel = controller.Index(<span style="color: blue">new </span><span style="color: #2b91af">IndexInputModel</span>());
    <span style="color: #2b91af">Assert</span>.IsNotNull(viewModel.Items);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Simple, <em>right</em>?</p>
<p>Now, let’s take a look at the other classes:</p>
<p><strong>IndexInputModel</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides an input model for the home index view.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">IndexInputModel
</span>{
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>IndexViewModel</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides a model for the home index view.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">IndexViewModel
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets the associated collection of </span><span style="color: gray">&lt;see cref="ToDoItem"/&gt; </span><span style="color: green">entities.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">ToDoItem</span>&gt; Items { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>ToDoItem</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Represents a to-do item.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">ToDoItem
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets the unique identifier of the item.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public virtual int </span>ItemId { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets a description of the item.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public virtual string </span>Description { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Gets or sets a flag indicating whether the item has been completed.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public virtual bool </span>IsComplete { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>HomeController</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides a controlling mechanism for the home area.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">HomeController
</span>{
    <span style="color: blue">private readonly </span><span style="color: #2b91af">IToDoItemRepository </span>_itemRepository;
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Initializes a new instance of the </span><span style="color: gray">&lt;see cref="HomeController"/&gt; </span><span style="color: green">class with the specified dependency.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="itemRepository"&gt;</span><span style="color: green">The item repository.</span><span style="color: gray">&lt;/param&gt;
    </span><span style="color: blue">public </span>HomeController(<span style="color: #2b91af">IToDoItemRepository </span>itemRepository)
    {
        _itemRepository = itemRepository;
    }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Prepares the index view model.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="model"&gt;</span><span style="color: green">The input model.</span><span style="color: gray">&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color: blue">public </span><span style="color: #2b91af">IndexViewModel </span>Index(<span style="color: #2b91af">IndexInputModel </span>model)
    {
        <span style="color: blue">return new </span><span style="color: #2b91af">IndexViewModel
                   </span>{
                       Items = _itemRepository.GetAll()
                   };
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Piecing it together</strong></p>
<p>Ok, so now I had a working controller. What I needed to do next was move my classes around, create a view, and then wire everything up. I went with three projects:</p>
<ol>
<li><span>FubuToDo</span>
<ol>
<li>Domain</li>
<li>Repositories</li>
<li><em>Infrastructure code will go here too</em></li>
</ol>
</li>
<li>FubuToDo.Tests</li>
<li><span>FubuToDo.Web</span>
<ol>
<li>Controllers &#8211; Folder for each</li>
<li>Common &#8211; Master pages</li>
<li>Images</li>
<li>Styles</li>
<li>Infrastructure &#8211; Bootstrapping, registries</li>
</ol>
</li>
</ol>
<p>I took up some habits from the <a title="Dovetail Software" href="http://www.dovetailsoftware.com/" target="_blank">Dovetail</a> guys and organized my views, models, and controllers in the same folder. Fubu uses the notion of “one model in, one model out” and the controllers are intended to be very light. Given those conventions, it’s pretty safe to assume that your folders won’t blow up with a ton of files and it’s just easier to find everything. I ended up with this:</p>
<p><a href="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure1.2" src="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.2_thumb.png" border="0" alt="Figure1.2" width="210" height="484" /></a></p>
<p><em>Figure 1.2 – Solution structure</em></p>
<p>The Index view doesn’t have anything fancy. The code-behind class inherits from FubuPage&lt;IndexViewModel&gt; and here’s the code for rendering the items:</p>
<pre class="code">    <span style="color: blue">&lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="blog-entry-container"&gt;</span><span style="color: blue">
        &lt;</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>To-Do List<span style="color: blue">&lt;/</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="Item-List"&gt;
            </span><span style="background: #ffee62">&lt;%</span> <span style="color: blue">if</span>(Model.Items.Any()) { <span style="background: #ffee62">%&gt;
</span>            <span style="color: blue">&lt;</span><span style="color: #a31515">ul </span><span style="color: red">class</span><span style="color: blue">="item-list"&gt;
            </span><span style="background: #ffee62">&lt;%</span> <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>Model.Items) { <span style="background: #ffee62">%&gt;
</span>                <span style="color: blue">&lt;</span><span style="color: #a31515">li </span><span style="color: red">id</span><span style="color: blue">="</span><span style="background: #ffee62">&lt;%</span>= item.ItemId <span style="background: #ffee62">%&gt;</span><span style="color: blue">" </span><span style="color: red">class</span><span style="color: blue">="item"&gt;
                    &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="row no-label"&gt;
                        &lt;</span><span style="color: #a31515">input </span><span style="color: red">type</span><span style="color: blue">="checkbox" </span><span style="color: red">name</span><span style="color: blue">="Item-</span><span style="background: #ffee62">&lt;%</span>= item.ItemId <span style="background: #ffee62">%&gt;</span><span style="color: blue">" </span><span style="color: red">&lt;%= item.IsComplete ? "checked=\"checked\"" : string.Empty %&gt; </span><span style="color: blue">/&gt;
                        &lt;</span><span style="color: #a31515">label </span><span style="color: red">class</span><span style="color: blue">="checkbox"&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>item.Description <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">label</span><span style="color: blue">&gt;
                    &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">br </span><span style="color: red">class</span><span style="color: blue">="cboth" /&gt;
                &lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
            </span><span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;
</span>            <span style="color: blue">&lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
            </span><span style="background: #ffee62">&lt;%</span> } <span style="color: blue">else </span>{ <span style="background: #ffee62">%&gt;
</span>            <span style="color: blue">&lt;</span><span style="color: #a31515">span</span><span style="color: blue">&gt;</span>No items.<span style="color: blue">&lt;/</span><span style="color: #a31515">span</span><span style="color: blue">&gt;
            </span><span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;
</span>        <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<h3>Getting it to run</h3>
<p>We’re almost there, I promise. We’ve got everything working. Now we just need to tell Fubu what to do with it all. Let’s take a look at the Global.asax.cs file:</p>
<p><strong>Global.asax.cs</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Provides a global application class for the to-do list application.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">Global </span>: <span style="color: #2b91af">HttpApplication
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Called by the ASP.NET Framework on application start.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="sender"&gt;&lt;/param&gt;
    /// &lt;param name="e"&gt;&lt;/param&gt;
    </span><span style="color: blue">protected void </span>Application_Start(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)
    {
        <span style="color: blue">var </span>routes = <span style="color: #2b91af">RouteTable</span>.Routes;
        <span style="color: #2b91af">FubuStructureMapBootstrapper</span>.Bootstrap(routes);
    }
}</pre>
<p>We’re deferring all of the initialization and registry calls to our FubuStructureMapBootstrapper class. It’s got a method called “Bootstrap” that just takes in our route collection. Let’s take a look under the hood (if you’ve read Tim’s post, this should look extremely familiar):</p>
<p><strong>FubuStructureMapBoostrapper</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Simple StructureMap bootstrapper.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">FubuStructureMapBootstrapper </span>: <span style="color: #2b91af">IBootstrapper
</span>{
    <span style="color: blue">private readonly </span><span style="color: #2b91af">RouteCollection </span>_routes;
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Initializes a new instance of the </span><span style="color: gray">&lt;see cref="FubuStructureMapBootstrapper"/&gt; </span><span style="color: green">class.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="routes"&gt;&lt;/param&gt;
    </span><span style="color: blue">public </span>FubuStructureMapBootstrapper(<span style="color: #2b91af">RouteCollection </span>routes)
    {
        _routes = routes;
    }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Initializes the bootstrapping process.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public void </span>BootstrapStructureMap()
    {
        <span style="color: #2b91af">UrlContext</span>.Reset();

        <span style="color: #2b91af">ObjectFactory</span>.Initialize(x =&gt; x.For&lt;<span style="color: #2b91af">IToDoItemRepository</span>&gt;().Use&lt;<span style="color: #2b91af">FakeToDoItemRepository</span>&gt;());

        Bootstrap(<span style="color: #2b91af">ObjectFactory</span>.Container, _routes);
    }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Bootstraps the application with the specified container.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="container"&gt;&lt;/param&gt;
    /// &lt;param name="routes"&gt;&lt;/param&gt;
    </span><span style="color: blue">public static void </span>Bootstrap(<span style="color: #2b91af">IContainer </span>container, <span style="color: #2b91af">RouteCollection </span>routes)
    {
        <span style="color: blue">var </span>bootstrapper = <span style="color: blue">new </span><span style="color: #2b91af">StructureMapBootstrapper</span>(container, <span style="color: blue">new </span><span style="color: #2b91af">FubuToDoRegistry</span>());
        bootstrapper.Bootstrap(routes);
    }
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Bootstraps the application for the specified routes.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name="routes"&gt;&lt;/param&gt;
    </span><span style="color: blue">public static void </span>Bootstrap(<span style="color: #2b91af">RouteCollection </span>routes)
    {
        <span style="color: blue">new </span><span style="color: #2b91af">FubuStructureMapBootstrapper</span>(routes).BootstrapStructureMap();
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>There are two things to note here: 1) The FakeToDoItemRepository and 2) the FubuToDoStructureMapRegistry classes.</p>
<p>The FakeToDoItemRepository class simply returns a hard-coded List&lt;ToDoItem&gt; full of 3 to-do items. The FubuToDoRegistry class has all of my registry calls which tell Fubu what conventions I would like for my app:</p>
<p><strong>FubuToDoRegistry</strong></p>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Simple registry.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public class </span><span style="color: #2b91af">FubuToDoRegistry </span>: <span style="color: #2b91af">FubuRegistry
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Initializes a new instance of the </span><span style="color: gray">&lt;see cref="FubuToDoRegistry"/&gt; </span><span style="color: green">class.
    </span><span style="color: gray">/// &lt;/summary&gt;
    </span><span style="color: blue">public </span>FubuToDoRegistry()
    {
        IncludeDiagnostics(<span style="color: blue">true</span>);

        Applies.ToThisAssembly();

        Actions
            .IncludeTypesNamed(x =&gt; x.EndsWith(<span style="color: #a31515">"Controller"</span>));

        Routes
            .IgnoreControllerNamespaceEntirely();

        Views.TryToAttach(x =&gt;
        {
            x.by_ViewModel_and_Namespace_and_MethodName();
            x.by_ViewModel_and_Namespace();
            x.by_ViewModel();
        });
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I have used the same conventions that Tim did in his example. He’s got a great explanation on what these do so I strongly recommend<a title="“Hello World” with FubuMVC (without the training wheels)" href="http://blog.timtyrrell.net/2010/01/hello-world-with-fubumvc-without.html" target="_blank"> reading his post</a>.</p>
<h3>Running the application</h3>
<p>At the time of this writing, there is no way to specify your default route in your registry. To get around this for now, I created a Default.aspx in the root and did a Response.Redirect(“~/Home/Index”) to force it to my default action. Once that’s in place, hit F5 and you should see this:</p>
<p><a href="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure1.3" src="http://www.joshua-arnold.com/wp-content/uploads/2010/01/Figure1.3_thumb.png" border="0" alt="Figure1.3" width="441" height="484" /></a></p>
<p><em>Figure 1.3 – End result</em></p>
<p>That’s it! We’re all done. In my next part I’ll talk about wiring up forms to your controllers and leverage the UnitOfWork pattern to get the items saving.</p>
<h3>Source code</h3>
<p>You can download the source for this part at: <a title="http://svn.joshua-arnold.com/fubutodo/tags/part1" href="http://svn.joshua-arnold.com/fubutodo/tags/part1">http://svn.joshua-arnold.com/fubutodo/tags/part1</a></p>
<span class="akst_link"><a href="http://www.joshua-arnold.com/?p=251&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_251"  class="akst_share_link">Share</a>
</span>
<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/fubumvc' rel='tag' target='_self'>fubumvc</a>, <a class='technorati-link' href='http://technorati.com/tag/fubutodo' rel='tag' target='_self'>fubutodo</a>, <a class='technorati-link' href='http://technorati.com/tag/tutorial' rel='tag' target='_self'>tutorial</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.joshua-arnold.com/2010/01/fubutodo-part-1-conventions-opinions-and-bootstrapping/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
