<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C# 3.0</title>
        <link>http://geekswithblogs.net/michelotti/category/6978.aspx</link>
        <description>C# 3.0</description>
        <language>en-US</language>
        <copyright>Steve Michelotti</copyright>
        <managingEditor>steve.michelotti@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>ASP.NET MVC in Action Review</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/11/07/asp.net-mvc-in-action-review.aspx</link>
            <description>&lt;p&gt;In an effort to continually improve as a developer, one of the things I do is &lt;a href="http://www.shelfari.com/michelotti/shelf" target="_blank"&gt;read lots of books&lt;/a&gt;. Recently I read &lt;a href="http://www.amazon.com/ASP-NET-MVC-Action-Jeffrey-Palermo/dp/1933988622/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1257257918&amp;amp;sr=8-1" target="_blank"&gt;ASP.NET MVC in Action&lt;/a&gt; by Jeffrey Palermo, Ben Scheirman, and Jimmy Bogard. In short, I consider this a “must read” for anyone who is serious about developing with the ASP.NET MVC framework.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.amazon.com/ASP-NET-MVC-Action-Jeffrey-Palermo/dp/1933988622/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1257257918&amp;amp;sr=8-1" target="_blank"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/51EJtErAQ6L._SL500_AA240_.jpg" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I’ve heard some people say that this should not be your first MVC book because it is more advanced than other MVC books available.  While I can understand that logic to a degree, I think it would be more accurate to say that if you’re literally brand new to MVC, this might not be the best “introductory” book.  However, if I could only own one book on MVC, this book would be it. There are two common themes from this book that I really enjoyed. First, the authors base their content on “real world” concepts rather than just explaining every topic in a mechanical way. This gives a great real world context throughout the book.  Secondly, the authors are not afraid to be “opinionated” in their recommended best practices for the MVC framework. These are aspects that are lacking in many technical books.&lt;/p&gt;  &lt;p&gt;The book starts out with a nice introduction to MVC (and it’s not too advanced to follow in the least). One of the things I really liked about this first chapter is that it spent some time discussing the history of web development which gave great context to where and how ASP.NET MVC fits into the discussion. This included many of the reasons as to *why* we have the MVC framework to begin with. The very next chapter dives into the Model of MVC and does a great job explaining the differences between domain models and presentation or view models.  Philosophically, I really agree with their concepts around view models and I have &lt;a href="http://api.postrank.com/log?url=http%3A%2F%2Fgeekswithblogs.net%2Fmichelotti%2Farchive%2F2009%2F10%2F25%2Fasp.net-mvc-view-model-patterns.aspx" target="_blank"&gt;referenced some of them before on this blog&lt;/a&gt;. Understanding these view model concepts is essential to building mature MVC applications.&lt;/p&gt;  &lt;p&gt;The books then dives into Controllers. The aspect I enjoyed most about this was the emphasis on unit testing. This included mocking and best practices for dependencies. The Controllers chapter ended with a discussion of action filters which was decent but I would have liked if the discussion of action filters had been expanded a little. The Views chapter provided great content covering validation, custom html helpers, partial views, and more. I didn’t find the Views chapter to be very advanced at all – rather, it provided a solid foundation for the type of information developers should know when building views.&lt;/p&gt;  &lt;p&gt;I really appreciated the chapter on Routing. It discussed designing the routing topology of your application up front which is often overlooked. It also discussed REST best practices. The examples of Routing constraints were one of the strongest parts of the chapter and they are an area that I’ve observed is often overlooked in MVC applications. My favorite part of the Routing chapter was unit testing routes with the very cool route testing extensions available in the MvcContrib project which makes unit testing routes trivial. For example, it showed how you can test a route in one simple line of code like this: "~/boiseCodeCamp/edit".ShouldMapTo&amp;lt;ConferenceController&amp;gt;(x =&amp;gt; x.Edit("boiseCodeCamp"));&lt;/p&gt;  &lt;p&gt;Towards the end of the book there was a chapter dedicated to exploring (for the sake of contrasting and understanding historical context) MonoRail and Ruby of Rails.  I always find the comparisons between Ruby on Rails and MVC very interesting and that was true in this book as well.  However, I didn’t find myself as engaged in the MonoRail discussion and I didn’t think it added as much to the book as the Ruby on Rails section.&lt;/p&gt;  &lt;p&gt;Probably the best chapter in the book was the one dedicated to Best Practices. Once again the authors were not afraid to be opinionated in their recommendations and this was based on “real world” experience with the framework (for which there is no substitute). One example I really liked from this section was the advocating of the RenderAction() method. This method has not been without its share of controversy because there are some “purists” that believe the existence of this method violates the tenets of the MVC framework because this means the view has some “knowledge” of the controllers. However, the authors point out (correctly, in my opinion) that the method allows for a much more elegant implementation in many cases that is a very pragmatic choice that actually leads to *better* separation of concerns in many instances since the partial gets its own controller.&lt;/p&gt;  &lt;p&gt;Learning the mechanics of MVC is important. But even more important is learning best practices and how you can extend the MVC framework to suit your needs when the situation requires it. In my opinion, this is the best book I’ve read that can help get you to that goal.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136130"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136130" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/136130.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/11/07/asp.net-mvc-in-action-review.aspx</guid>
            <pubDate>Sun, 08 Nov 2009 02:37:24 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/136130.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/11/07/asp.net-mvc-in-action-review.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/136130.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/136130.aspx</trackback:ping>
        </item>
        <item>
            <title>CMAP Code Camp &amp;ndash; MVC in the Real World Code Samples and Extra Resources</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/11/07/cmap-code-camp-ndash-mvc-in-the-real-world-code.aspx</link>
            <description>&lt;p&gt;Thanks to everyone who attended my “MVC in the Real World” presentation at CMAP Code Camp today.  The code as well as the PowerPoint can be downloaded here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&amp;amp;ReleaseId=3288" target="_blank"&gt;MVC in the Real World Download&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I also had a few other requests during the talk.  First, I had a request to post the code as it looked at the very beginning of my talk before I modified anything.  You’ll find that link on the download page above as well (file name: PersonalInfoManager-ReadlWorldMVC-Begin.zip).  &lt;/p&gt;  &lt;p&gt;Second, I was asked about a couple of the tools I was using.  You can see the links for all the tools I use (almost all free) on my &lt;a href="http://api.postrank.com/log?url=http%3A%2F%2Fgeekswithblogs.net%2Fmichelotti%2Farchive%2F2008%2F11%2F23%2Fdeveloper-tools-and-utilities.aspx" target="_blank"&gt;Developer Tools and Utilities&lt;/a&gt; page.&lt;/p&gt;  &lt;p&gt;Third, I had a request to post some links that discussed some of the Unit Testing best practices. Brad Wilson (one of the primary developers of &lt;a href="http://www.codeplex.com/xunit" target="_blank"&gt;xUnit&lt;/a&gt;) co-authored this excellent article: &lt;a href="http://bradwilson.typepad.com/presentations/effective-unit-testing.pdf" target="_blank"&gt;Effective Unit Testing&lt;/a&gt;. I also really like this slide deck which he also published: &lt;a href="http://bradwilson.typepad.com/presentations/lessons-learned-in-unit-testing.pdf" target="_blank"&gt;Lessons Learned in Programmer Testing&lt;/a&gt;. Roy Osherove also has a great post on &lt;a href="http://weblogs.asp.net/rosherove/archive/2005/04/03/TestNamingStandards.aspx" target="_blank"&gt;Naming Standards for Unit Tests&lt;/a&gt;. Finally, I love this article by Jeremy Miller in MSDN magazine entitled &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd263069.aspx" target="_blank"&gt;Designing for Testability&lt;/a&gt;. One of his article’s major premises is that “testable” code typically will naturally result in “well designed” code – a concept that I’m a huge fan of.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136129"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136129" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/136129.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/11/07/cmap-code-camp-ndash-mvc-in-the-real-world-code.aspx</guid>
            <pubDate>Sun, 08 Nov 2009 01:10:02 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/136129.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/11/07/cmap-code-camp-ndash-mvc-in-the-real-world-code.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/136129.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/136129.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET MVC View Model Patterns</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx</link>
            <description>&lt;p&gt;Since MVC has been released I have observed much confusion about how best to construct view models. Sometimes this confusion is not without good reason since there does not seem to be a ton of information out there on best practice recommendations.  Additionally, there is not a “one size fits all” solution that acts as the silver bullet. In this post, I’ll describe a few of the main patterns that have emerged and the pros/cons of each. It is important to note that many of these patterns have emerged from people solving real-world issues.&lt;/p&gt;  &lt;p&gt;Another key point to recognize is that the question of how best to construct view models is *not* unique to the MVC framework.  The fact is that even in traditional ASP.NET web forms you have the same issues.  The difference is that historically developers haven’t always dealt with it directly in web forms – instead what often happens is that the code-behind files end up as monolithic dumping grounds for code that has no separation of concerns whatsoever and is wiring up view models, performing presentation logic, performing business logic, data access code, and who knows what else.  MVC at least facilitates the developer taking a closer look at how to more elegantly implement Separation of Concerns.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Pattern 1 – Domain model object used directly as the view model&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;Consider a domain model that looks like this:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Motorcycle&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Make { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Model { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Year { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; VIN { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When we pass this into the view, it of course allows us to write simple HTML helpers in the style of our choosing:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextBox(&lt;span class="str"&gt;"Make"&lt;/span&gt;) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextBoxFor(m =&amp;gt; m.Make) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And of course with default model binding we are able to pass that back to the controller when the form is posted:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Save(Motorcycle motorcycle)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;While this first pattern is simple and clean and elegant, it breaks down fairly quickly for anything but the most trivial views. We are binding directly to our domain model in this instance – this often is not sufficient for fully displaying a view.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Pattern 2 – Dedicated view model that *contains* the domain model object&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Staying with the Motorcycle example above, a much more real-world example is that our view needs more than just a Motorcycle object to display properly.  For example, the Make and Model will probably be populated from drop down lists. Therefore, a common pattern is to introduce a view model that acts as a container for all objects that our view requires in order to render properly:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MotorcycleViewModel&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Motorcycle Motorcycle { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; SelectList MakeList { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; SelectList ModelList { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this instance, the controller is typically responsible for making sure MotorcycleViewModel is correctly populated from the appropriate data in the repositories (e.g., getting the Motorcycle from the database, getting the collections of Makes/Models from the database).  Our Html Helpers change slightly because they refer to Motorcycle.Make rather than Make directly:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.DropDownListFor(m =&amp;gt; m.Motorcycle.Make, Model.MakeList) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When the form is posted, we are still able to have a strongly-typed Save() method:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Save([Bind(Prefix = &lt;span class="str"&gt;"Motorcycle"&lt;/span&gt;)]Motorcycle motorcycle)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note that in this instance we had to use the Bind attribute designating “Motorcycle” as the prefix to the HTML elements we were interested in (i.e., the ones that made up the Motorcycle object).&lt;/p&gt;

&lt;p&gt;This pattern is simple and elegant and appropriate in many situations. However, as views become more complicated, it also starts to break down since there is often an impedance mismatch between domain model objects and view model objects.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Pattern 3 – Dedicated view model that contains a custom view model entity&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As views get more complicated it is often difficult to keep the domain model object in sync with concerns of the views.  In keeping with the example above, suppose we had requirements where we need to present the user a checkbox at the end of the screen if they want to add another motorcycle.  When the form is posted, the controller needs to make a determination based on this value to determine which view to show next. The last thing we want to do is to add this property to our domain model since this is strictly a presentation concern. Instead we can create a custom “view model entity” instead of passing the actual Motorcycle domain model object into the view. We’ll call it MotorcycleData:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MotorcycleData&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Make { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Model { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Year { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; VIN { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; AddAdditionalCycle { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This pattern requires more work and it also requires a “mapping” translation layer to map back and forth between the Motorcycle and MotorcycleData objects but it is often well worth the effort as views get more complex.  This pattern is strongly advocated by the authors of &lt;a href="http://www.amazon.com/ASP-NET-MVC-Action-Jeffrey-Palermo/dp/1933988622/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1256416048&amp;amp;sr=8-1" target="_blank"&gt;MVC in Action&lt;/a&gt; (a book a highly recommend).  These ideas are further expanded in a post by &lt;a href="http://www.lostechies.com/blogs/jimmy_bogard/default.aspx" target="_blank"&gt;Jimmy Bogard&lt;/a&gt; (one of the co-authors) in his post &lt;a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx" target="_blank"&gt;How we do MVC – View Models&lt;/a&gt;. I strongly recommended reading Bogard’s post (there are many interesting comments on that post as well). In it he discusses approaches to handling this pattern including using MVC Action filters and &lt;a href="http://www.codeplex.com/AutoMapper" target="_blank"&gt;AutoMapper&lt;/a&gt; (I also recommend checking out AutoMapper).&lt;/p&gt;

&lt;p&gt;Let’s continue to build out this pattern without the use of Action filters as an alternative. In real-world scenarios, these view models can get complex fast.  Not only do we need to map the data from Motorcycle to MotorcycleData, but we also might have numerous collections that need to be populated for dropdown lists, etc.  If we put all of this code in the controller, then the controller will quickly end up with a lot of code dedicated just to building the view model which is not desirable as we want to keep our controllers thin. Therefore, we can introduce a “builder” class that is concerned with building the view model.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MotorcycleViewModelBuilder&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; IMotorcycleRepository motorcycleRepository;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; MotorcycleViewModelBuilder(IMotorcycleRepository repository)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.motorcycleRepository = repository;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; MotorcycleViewModel Build()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="rem"&gt;// code here to fully build the view model &lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        &lt;span class="rem"&gt;// with methods in the repository&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This allows our controller code to look something like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Edit(&lt;span class="kwrd"&gt;int&lt;/span&gt; id)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    var viewModelBuilder = &lt;span class="kwrd"&gt;new&lt;/span&gt; MotorcycleViewModelBuilder(&lt;span class="kwrd"&gt;this&lt;/span&gt;.motorcycleRepository);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    var motorcycleViewModel = viewModelBuilder.Build();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.View();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Our views can look pretty much the same as pattern #2 but now we have the comfort of knowing that we’re only passing in the data to the view that we need – no more, no less.  When the form is posted back, our controller’s Save() method can now look something like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Save([Bind(Prefix = &lt;span class="str"&gt;"Motorcycle"&lt;/span&gt;)]MotorcycleData motorcycleData)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    var mapper = &lt;span class="kwrd"&gt;new&lt;/span&gt; MotorcycleMapper(motorcycleData);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    Motorcycle motorcycle = mapper.Map();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.motorcycleRepository.Save(motorcycle);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.RedirectToAction(&lt;span class="str"&gt;"Index"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Conceptually, this implementation is very similar to Bogard’s post but without the AutoMap attribute.  The AutoMap attribute allows us to keep some of this code out of the controller which can be quite nice.  One advantage to not using it is that the code inside the controller class is more obvious and explicit.  Additionally, our builder and mapper classes might need to build the objects from multiple sources and repositories. Internally in our mapper classes, you can still make great use of tools like AutoMapper.&lt;/p&gt;

&lt;p&gt;In many complex real-world cases, some variation of pattern #3 is the best choice as it affords the most flexibility to the developer.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Considerations&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How do you determine the best approach to take?  Here are some considerations to keep in mind:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Code Re-use&lt;/u&gt; – Certainly patterns #1 and #2 lend themselves best to code re-use as you are binding your views directly to your domain model objects. This leads to increased code brevity as mapping layers are not required. However, if your view concerns differ from your domain model (which they often will) options #1 and #2 begin to break down.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Impedance mismatch&lt;/u&gt; – Often there is an impedance mismatch between your domain model and the concerns of your view.  In these cases, option #3 gives the most flexibility.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Mapping Layer&lt;/u&gt; – If custom view entities are used as in option #3, you must ensure you establish a pattern for a mapping layer. Although this means more code that must be written, it gives the most flexibility and there are libraries available such as AutoMapper that make this easier to implement.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Validation&lt;/u&gt; – Although there are many ways to perform validation, one of the most common is to use libraries like Data Annotations. Although typical validations (e.g., required fields, etc.) will probably be the same between your domain models and your views, not all validation will always match.  Additionally, you may not always be in control of your domain models (e.g., in some enterprises the domain models are exposed via services that UI developers simply consume).  So there is a limit to how you can associate validations with those classes.  Yes, you can use a separate “meta data” class to designate validations but this duplicates some code similar to how a view model entity from option #3 would anyway. Therefore, option #3 gives you the absolute most control over UI validation.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Conclusion&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following has been a summary of several of the patterns that have emerged in dealing with view models. Although these have all been in the context of ASP.NET MVC, the problem with how best to deal with view models is also an issue with other frameworks like web forms as well. If you are able to bind directly to domain model in simple cases, that is the simplest and easiest solution. However, as your complexity grows, having distinct view models gives you the most overall flexibility.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135703"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135703" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/135703.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx</guid>
            <pubDate>Sun, 25 Oct 2009 13:03:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/135703.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx#feedback</comments>
            <slash:comments>19</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/135703.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/135703.aspx</trackback:ping>
        </item>
        <item>
            <title>Setting Selected Item in FluentHtml Select List</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/10/22/setting-selected-item-in-fluenthtml-select-list.aspx</link>
            <description>&lt;p&gt;I ran into a small issue today when using the &lt;a href="http://mvccontrib.codeplex.com/wikipage?title=FluentHtml" target="_blank"&gt;FluentHtml&lt;/a&gt; Select() HtmlHelper.  My original code was not properly setting the value of the selected item in the drop down:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=&lt;span class="kwrd"&gt;this&lt;/span&gt;.Select(m =&amp;gt; m.Motorcycle.Make).Selected(Model.Motorcycle.Make).Label(&lt;span class="str"&gt;"Vehicle Make"&lt;/span&gt;).Options(Model.MakeList)&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;I kept ending up with no item being selected from the drop down even though I definitely had a vehicle Make populated in my model and my drop down was correctly populated with the collection from the “MakeList” property of the view model.  It turns out that the solution was a simple matter of corrected ordering:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=&lt;span class="kwrd"&gt;this&lt;/span&gt;.Select(m =&amp;gt; m.Motorcycle.Make).Options(Model.MakeList).Selected(Model.Motorcycle.Make).Label(&lt;span class="str"&gt;"Vehicle Make"&lt;/span&gt;)&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In hindsight I guess this order makes a little more sense intuitively anyway.  However, the fluent API did not constrain me from running into this issue.  So, just a heads up, when using the Select() helper, set the Options() first, and then set the selected value by invoking the Selected() helper method.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135657"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135657" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/135657.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/10/22/setting-selected-item-in-fluenthtml-select-list.aspx</guid>
            <pubDate>Thu, 22 Oct 2009 18:30:28 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/135657.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/10/22/setting-selected-item-in-fluenthtml-select-list.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/135657.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/135657.aspx</trackback:ping>
        </item>
        <item>
            <title>StructureMap with Named Instance and With Method</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/10/14/structuremap-with-named-instance-and-with-method.aspx</link>
            <description>&lt;p&gt;I ran into an interesting IoC issue today that was ultimately resolved by some extremely helpful email assistance by &lt;a href="http://www.lostechies.com/blogs/chad_myers/" target="_blank"&gt;Chad Myers&lt;/a&gt;. I’ll post the solution here in the hopes that someone else will find it helpful. Here is the code to set up the example:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IFoo&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    IBar Bar { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Foo : IFoo&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; IBar Bar { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Foo(IBar bar)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.Bar = bar;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IBar&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="kwrd"&gt;bool&lt;/span&gt; Flag { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Bar : IBar&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Flag { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Bar(&lt;span class="kwrd"&gt;bool&lt;/span&gt; flag)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.Flag = flag;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The key here is that Bar has a constructor that takes a Boolean parameter and there are some circumstances where I want Bar to have a true parameters and some instances where I want it to be false.  Because of this variability, I can’t just initialize like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;x.ForRequestedType&amp;lt;IBar&amp;gt;().TheDefault.Is.OfConcreteType&amp;lt;Bar&amp;gt;().WithCtorArg(&lt;span class="str"&gt;"flag"&lt;/span&gt;).EqualTo(&lt;span class="kwrd"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That won’t work because that only supports the “true” parameter for IBar. In order to support both, I need to utilize named instances.  Therefore, my complete StructureMapBootstrapper looks like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; StructureMapBootstrapper&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        ObjectFactory.Initialize(x =&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                x.ForRequestedType&amp;lt;IFoo&amp;gt;().TheDefaultIsConcreteType&amp;lt;Foo&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;                x.ForRequestedType&amp;lt;IBar&amp;gt;().TheDefault.Is.OfConcreteType&amp;lt;Bar&amp;gt;().WithCtorArg(&lt;span class="str"&gt;"flag"&lt;/span&gt;).EqualTo(&lt;span class="kwrd"&gt;true&lt;/span&gt;).WithName(&lt;span class="str"&gt;"TrueBar"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                x.ForRequestedType&amp;lt;IBar&amp;gt;().TheDefault.Is.OfConcreteType&amp;lt;Bar&amp;gt;().WithCtorArg(&lt;span class="str"&gt;"flag"&lt;/span&gt;).EqualTo(&lt;span class="kwrd"&gt;false&lt;/span&gt;).WithName(&lt;span class="str"&gt;"FalseBar"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This now enables me to create instances of IBar by using the GetNamedInstance() method.  However, the primary issue is that I need to create an instance of IFoo and Foo has a nested dependency on IBar (where the parameter can vary).  It turns out the missing piece to the puzzle is to simply leverage the ObjectFactory’s With() method (in conjunction with the GetNamedInstance() method) which takes an object instance and returns an ExplicitArgsExpression to enable the fluent syntax. Therefore, these instances can easily be instantiated either way like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var fooWithTrueBar = ObjectFactory.With(ObjectFactory.GetNamedInstance&amp;lt;IBar&amp;gt;(&lt;span class="str"&gt;"TrueBar"&lt;/span&gt;)).GetInstance&amp;lt;IFoo&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;var fooWithFalseBar = ObjectFactory.With(ObjectFactory.GetNamedInstance&amp;lt;IBar&amp;gt;(&lt;span class="str"&gt;"FalseBar"&lt;/span&gt;)).GetInstance&amp;lt;IFoo&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Just another example of how IoC containers can enable us to keep clean separation of concerns in our classes and enable us to avoid cluttering our code with the wiring up of dependencies.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135475"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135475" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/135475.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/10/14/structuremap-with-named-instance-and-with-method.aspx</guid>
            <pubDate>Thu, 15 Oct 2009 03:07:04 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/135475.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/10/14/structuremap-with-named-instance-and-with-method.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/135475.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/135475.aspx</trackback:ping>
        </item>
        <item>
            <title>NoVa Code Camp &amp;ndash; Code Samples</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/10/10/nova-code-camp-ndash-code-samples.aspx</link>
            <description>&lt;p&gt;Thanks to everyone who attended my session today at &lt;a href="http://www.novacodecamp.org" target="_blank"&gt;NoVa Code Camp&lt;/a&gt;.  Both the code and PowerPoint slides are available for download. &lt;/p&gt;  &lt;p&gt;Download samples for: &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&amp;amp;ReleaseId=3288" target="_blank"&gt;MVC in the Real World&lt;/a&gt;. Check out the readme.txt file in Solution Items and all SQL scripts for creating the databases.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135392"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135392" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/135392.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/10/10/nova-code-camp-ndash-code-samples.aspx</guid>
            <pubDate>Sat, 10 Oct 2009 19:30:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/135392.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/10/10/nova-code-camp-ndash-code-samples.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/135392.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/135392.aspx</trackback:ping>
        </item>
        <item>
            <title>MVC AJAX Form with jQuery Form Plugin and jQuery Thickbox</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/09/01/mvc-ajax-form-with-jquery-form-plugin-and-jquery-thickbox.aspx</link>
            <description>&lt;p&gt;In a previous post, I showed how to submit an AJAX form in MVC with the &lt;a href="http://geekswithblogs.net/michelotti/archive/2009/08/31/mvc-ajax-form-with-ajax.beginform-and-jquery-thickbox.aspx" target="_blank"&gt;jQuery Thickbox and the built-in MVC AJAX helpers&lt;/a&gt;.  If you read that post first, it will show the complete context for how to simply submit an AJAX form that is being rendered inside a jQuery Thickbox with built-in MVC AJAX helpers.  But what if you want to stick to a pure jQuery solution?  That is also simple enough to do.  Instead of using the Ajax.BeginForm() method, you can use the &lt;a href="http://plugins.jquery.com/project/form" target="_blank"&gt;jQuery Form Plugin&lt;/a&gt;.  The code is almost identical to the previous implementation.  The only differences are in the Index.ascx partial view:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&amp;lt;MvcAjaxForm.Models.ContactMessage&amp;gt;" %&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/javascript"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    $(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        $(&lt;span class="str"&gt;"#contactForm"&lt;/span&gt;).ajaxForm({ target: &lt;span class="str"&gt;'#contactArea'&lt;/span&gt; });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;form&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="contactForm"&lt;/span&gt; &lt;span class="attr"&gt;action&lt;/span&gt;&lt;span class="kwrd"&gt;="&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="attr"&gt;Index&lt;/span&gt;&lt;span class="kwrd"&gt;", "&lt;/span&gt;&lt;span class="attr"&gt;Contact&lt;/span&gt;&lt;span class="kwrd"&gt;") %&amp;gt;"&lt;/span&gt; &lt;span class="attr"&gt;method&lt;/span&gt;&lt;span class="kwrd"&gt;="post"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="contactArea"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        Email Address: &lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextBox(&lt;span class="str"&gt;"EmailAddress"&lt;/span&gt;) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        Message: &lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextArea(&lt;span class="str"&gt;"MessageText"&lt;/span&gt;)&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="submit"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;form&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On line 11, you will now specify a normal HTML form just like you might specify any other form in MVC (you could also use the Html.BeginForm() method as well if you have that preference). The only additional thing you have to implement is the one line of code shown on line 5 above.  This is making a call to the ajaxForm() method (that comes in the Form Plugin library) and passes in the options to indicate that the response from the server should be displayed in the “contactArea” div.  This is just one example of how to use the Form plugin – you can also find numerous example within the &lt;a href="http://malsup.com/jquery/form/#getting-started" target="_blank"&gt;Form Plugin’s documentation site&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134433"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134433" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/134433.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/09/01/mvc-ajax-form-with-jquery-form-plugin-and-jquery-thickbox.aspx</guid>
            <pubDate>Tue, 01 Sep 2009 11:28:11 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/134433.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/09/01/mvc-ajax-form-with-jquery-form-plugin-and-jquery-thickbox.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/134433.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/134433.aspx</trackback:ping>
        </item>
        <item>
            <title>MVC AJAX Form with Ajax.BeginForm() and jQuery Thickbox</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/08/31/mvc-ajax-form-with-ajax.beginform-and-jquery-thickbox.aspx</link>
            <description>&lt;p&gt;A relatively common scenario you might want in your application is the ability for a user to click a link that pops up a little dialog to submit some information.  For example, let’s say you have this form where the user could click the “Contact this person” link:&lt;/p&gt;  &lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_mvc-ajax1.PNG" /&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;After clicking this link, it pops up the following dialog where the user can type in their message:&lt;/p&gt;  &lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_mvc-ajax2.PNG" /&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Finally, once the user submits their message, it shows a little confirmation:&lt;/p&gt;  &lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_mvc-ajax3.PNG" /&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;This scenario can be implemented with MVC with very few lines of code.  First off, we’ll be using the &lt;a href="http://www.dofactory.com/Ajax/AjaxMessageHtml.aspx" target="_blank"&gt;AJAX HTML Message pattern&lt;/a&gt; so that the AJAX messages that are going across the wire are simple HTML snippets.  Also, we’ll use jQuery &lt;a href="http://jquery.com/demo/thickbox/" target="_blank"&gt;Thickbox&lt;/a&gt; for our dialog. The first thing we need to do it to implement our “Contact this person” link:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.ActionLink(&lt;span class="str"&gt;"Contact this person"&lt;/span&gt;, &lt;span class="str"&gt;"Index"&lt;/span&gt;, &lt;span class="str"&gt;"Contact"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; { height = 200, width = 300 }, &lt;span class="kwrd"&gt;new&lt;/span&gt; { @&lt;span class="kwrd"&gt;class&lt;/span&gt; = &lt;span class="str"&gt;"thickbox"&lt;/span&gt; }) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is a pretty typical implementation of the jQuery thickbox.  We’re specifying that we want an AJAX call to be made to our ContactController.Index() method. The HTML that this ActionLink renders will simply look like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;="thickbox"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="/Contact?height=200&amp;amp;amp;width=300"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contact this person&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The complete code for the ContractController looks like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ContactController : Controller&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Index()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; View(&lt;span class="kwrd"&gt;new&lt;/span&gt; ContactMessage());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    [ActionName(&lt;span class="str"&gt;"Index"&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    [AcceptVerbs(HttpVerbs.Post)]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult SubmitMessage(ContactMessage message)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.View(&lt;span class="str"&gt;"Confirmation"&lt;/span&gt;, message);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It is important to note that both views returned by the ContactController’s action methods are both partial views (i.e., Index.ascx and Confirmation.ascx) with our HTML snippets in there. We know that in the Index.ascx we want to do an AJAX form submission/post rather than a full page form submission. There are a couple of ways to do this including using the &lt;a href="http://plugins.jquery.com/project/form" target="_blank"&gt;jQuery Form plugin&lt;/a&gt;. However, in this case, the MVC framework already comes built-in with this functionality so we don’t have to rely on the jQuery Form plugin (unless we want to). Most of the time we find ourselves using the Html property of the View which is of type HtmlHelper.  However, there is also an Ajax property of the view of type &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.ajaxhelper.aspx" target="_blank"&gt;AjaxHelper&lt;/a&gt;. The AjaxHelper has a BeginForm method that will allow you to submit the form via AJAX. You’ll need to make sure you include the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts in your page to use the Ajax helpers.  The complete implementation for the Index.ascx can just look like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&amp;lt;MvcAjaxForm.Models.ContactMessage&amp;gt;" %&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="kwrd"&gt;using&lt;/span&gt; (Ajax.BeginForm(&lt;span class="str"&gt;"Index"&lt;/span&gt;, &lt;span class="str"&gt;"Contact"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; AjaxOptions { UpdateTargetId = &lt;span class="str"&gt;"contactArea"&lt;/span&gt; } )) { &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="contactArea"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Email Address: &lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextBox(&lt;span class="str"&gt;"EmailAddress"&lt;/span&gt;) &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        Message: &lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;=Html.TextArea(&lt;span class="str"&gt;"MessageText"&lt;/span&gt;)&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="submit"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt; } &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Notice the UpdateTargetId is specifying the “contactArea” div.  This causes all HTML that is sent back on the form post (i.e., the confirmation message) to be displayed inside this div only.  The complete code for this sample can be downloaded &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=michelotti&amp;amp;DownloadId=7086" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134407"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134407" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/134407.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/08/31/mvc-ajax-form-with-ajax.beginform-and-jquery-thickbox.aspx</guid>
            <pubDate>Mon, 31 Aug 2009 09:00:18 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/134407.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/08/31/mvc-ajax-form-with-ajax.beginform-and-jquery-thickbox.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/134407.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/134407.aspx</trackback:ping>
        </item>
        <item>
            <title>C# 3.0 Deep Dish with a slice of C# 4.0 &amp;ndash; Code Samples</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/08/26/c-3.0-deep-dish-with-a-slice-of-c-4.0-again.aspx</link>
            <description>&lt;p&gt;Thanks to everyone who attended my presentation last night at &lt;a href="http://caparea.net/" target="_blank"&gt;CapArea&lt;/a&gt; on C# 3.0 and C# 4.0.  You can download the C# 3.0 code samples, C# 4.0 code samples, and the PowerPoint presentation from &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&amp;amp;ReleaseId=3148" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Several people asked me about some of the tools I was using during the presentation.  A list of the tools that I use (and more) can be found &lt;a href="http://geekswithblogs.net/michelotti/archive/2008/11/23/developer-tools-and-utilities.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Feel free to email me with any questions.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134337"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134337" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/134337.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/08/26/c-3.0-deep-dish-with-a-slice-of-c-4.0-again.aspx</guid>
            <pubDate>Wed, 26 Aug 2009 14:04:51 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/134337.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/08/26/c-3.0-deep-dish-with-a-slice-of-c-4.0-again.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/134337.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/134337.aspx</trackback:ping>
        </item>
        <item>
            <title>C# 3.0 Deep Dish with a slice of C# 4.0</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/08/23/c-3.0-deep-dish-with-a-slice-of-c-4.0.aspx</link>
            <description>&lt;p&gt;On Tuesday night (August 25), I will be presenting “C# 3.0 Deep Dish with a slice of C# 4.0” at &lt;a href="http://caparea.net/" target="_blank"&gt;CapArea .NET User Group&lt;/a&gt; (but I have to give &lt;a href="http://www.softinsight.com/bnoyes/" target="_blank"&gt;Brian Noyes&lt;/a&gt; credit for the catchy title). The primary content of the presentation will be a deep dive into the C# 3.0 language features. The latter part of the presentation will be an overview of the features coming in C# 4.0. Hope to see you there.&lt;/p&gt;  &lt;p&gt;Additionally, the next night (Wednesday, August 26), I’ll be at &lt;a href="http://www.frednug.org/" target="_blank"&gt;FredNUG&lt;/a&gt; presenting Linq to SQL.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134266"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134266" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/134266.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/08/23/c-3.0-deep-dish-with-a-slice-of-c-4.0.aspx</guid>
            <pubDate>Sun, 23 Aug 2009 11:44:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/134266.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/08/23/c-3.0-deep-dish-with-a-slice-of-c-4.0.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/134266.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/134266.aspx</trackback:ping>
        </item>
    </channel>
</rss>