Using Interfaces for Model in ASP.NET MVC (Impl)…

I mentioned in my first post that we were using interfaces to represent our models and WHY we did it. So here’s the HOW of doing it.


As I mentioned, we used the model attribute to decorate our models when they are in the method signature. This attribute was based on the AbstractParameterBinderAttribute from MvcContrib (if you’re using ASP.NET MVC and NOT using MvcContrib, you’re probably re-inventing more than a few wheels) which is just a CustomModelBinderAttribute with a little bit of happy sprinkled in. Here’s what our binding looked like:

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
	ViewDataDictionary viewData = GetViewData(controllerContext.Controller);
	object result = null;
	if (viewData != null)
		result = viewData.For(controllerContext, bindingContext);
		SetViewData(result, controllerContext, bindingContext, viewData);
	return result;

The GetViewData and SetViewData are pretty self-explanatory but the real heavy lifting is in that extension method. Let’s take a look at it.


We know that we have a giant dictionary holding on to our data and tedious mapping using strings make me itchy, not to mention it breaking my refactoring tool, so how do we push the values without them? DictionaryAdapter from Castle. This little known, under utilized piece of work was shown to me by Craig Neuwirt, it’s creator, and I’m constantly amazed at it’s usefulness. Observe the following extension method for ViewData:

private static readonly IDictionaryAdapterFactory
	ViewDataFactory = new DictionaryAdapterFactory();

public static object For(this ViewDataDictionary viewData, ControllerContext controllerContext, ModelBindingContext bindingContext)
	ViewDataAdapter data = new ViewDataAdapter(viewData);
	object adapter = ViewDataFactory.GetAdapter(bindingContext.ModelType, data);

	IDictionaryAdapter meta = adapter as IDictionaryAdapter;
	return adapter;

It’s that simple. No, really. The DictionaryAdapter will match up the properties on the interface you defined with the corresponding elements in the ViewData and push their values into an instance of the model. All of that, without you even having to buy it dinner.

There’s more??

This approach worked great for us and gave us most of what we needed, however, this code doesn’t handle rehydrating objects from ActiveRecord – yet. But that’s a post for another night…

CORRECTION: Craig informed me that he did not create it originally but re-wrote it’s initial implementation to what it is now. Sorry for the misunderstanding.

David O’Hara is a Principal with Improving Enterprises in Dallas, Texas.


2 Responses to Using Interfaces for Model in ASP.NET MVC (Impl)…

  1. […] to VoteUsing Interfaces for Model in ASP.NET MVC (Impl)… (8/18/2009)Tuesday, August 18, 2009 from blog.davidohara.netI mentioned in my first post that we were using […]

  2. Mikhail says:

    Thanks for your post. It is exactly my problem.
    But can you still explain algorithm of SetViewData method? Is it a way to set up controllerContext.Controller.ViewData value?

%d bloggers like this: