ASP.NET MVC View Model Patterns

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 eme...


Setting Selected Item in FluentHtml Select List

I ran into a small issue today when using the FluentHtml [http://mvccontrib.codeplex.com/wikipage?title=FluentHtml] Select() HtmlHelper.  My original code was not properly setting the value of the selected item in the drop down: <%=this.Select(m => m.Motorcycle.Make).Selected(Model.Motorcycle.Make).Label("Vehicle Make").Options(Model.MakeList)%> 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...


What IoC Container do you use?

I often get asked what IoC Container I prefer.  Short answer: StructureMap [http://structuremap.sourceforge.net/Default.htm]. I love the fluent syntax for configuration. Overall, it's easy to use, has many advanced features, and is very lightweight.  I view the learning curve with StructureMap as relatively small.  It is one of the longest-lived IoC containers (if not the longest) and has a huge adoption rate which means it's quite mature and not difficult to find code examples online. For exam...


StructureMap with Named Instance and With Method

I ran into an interesting IoC issue today that was ultimately resolved by some extremely helpful email assistance by Chad Myers [http://www.lostechies.com/blogs/chad_myers/]. 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: public interface IFoo { IBar Bar { get; set; } }   public class Foo : IFoo { public IBar Bar { get; set; }   public Foo(IBar bar) { this.Bar = bar; } }   public interface IBa...


NoVa Code Camp – Code Samples

Thanks to everyone who attended my session today at NoVa Code Camp [http://www.novacodecamp.org].  Both the code and PowerPoint slides are available for download. Download samples for: MVC in the Real World [https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&ReleaseId=3288] . Check out the readme.txt file in Solution Items and all SQL scripts for creating the databases....