ASP.NET MVC

I wasn't entirely happy with my previous implementation of a generic LINQ-to-SQL ModelBinder. There's a bit of tier-jumping going on, it would be better if the ModelBinder interacted with some loosely-coupled Service Layer. Also, registering my custom ModelBinder in Global.asax messes up the functionality of UpdateModel and TryUpdateModel, both very important tools for handling form posting situations.

With this in mind, I've adapted the concept to apply more generally.

In my previous post, Assigning responsibility in ASP.NET MVC I discussed using ASP.NET MVC's ModelBinder framework to perform SELECT operations against the database. Using this technique, your Action methods (e.g. Products/Details/1) recieve a domain object (Product p) instead of an id (int id). This makes your Action methods more database agnostic, easier to unit test, and saves you repeating your database select inside every Action method that needs to convert an id into a domain object.

In this post, I describe an implementation of a generic ModelBinder to select domain objects from any Linq-to-SQL datacontext.

I've been working on a more generic implementation of Maarten Balliauw's Linq-to-SQL Model Binder. Maarten's implementation bothers me because it builds up a query as a string and uses MyDataContext.ExecuteQuery to get the result as a non-generic IEnumerable. I don't find this implementation particularly elegant, and in particular Maarten notes that as it stands his code is vulnerable to SQL Injection.

A similar post by Timothy Khouri (using ToString() to build up a base-64 representation of an object to pass between requests, and a matching ModelBuilder to re-build the object on the other side) was criticised on the grounds that "you wouldn't ever want any database interaction going on during the binding process".

This got me thinking a little bit about how responsibility is divided up in ASP.NET MVC.

Late last year, I finally got over my preconceptions and gave ASP.NET MVC a try. The result has been a massive u-turn in my opinion of the framework. Now that I've used it in a couple of projects (including porting Worlds Apart to the framework), let me share a few of my thoughts with you.