Stack Overflow! MVC Model Binders Gone Awry

The recently released MVC Beta 1 has some pretty cool features. One of the features that I’ve been using for a while is Model Binders. These are now turned on by default which can cause some problems if you’re not expecting this behavior. For example, say you have an action as follows:

        public ActionResult Binder(Dog dog)
        {
            return View("Index");
        }

Here’s the model:

    public class Dog
    {
        public string Name { get; set; }
        public Dog cat { get; set; }
    }

Calling this action will cause a stack overflow error, as the default model binder will attempt to recreate every dog in the chain.

I would not have noticed this as I use my own implementation of the model binders to provide functionality for pulling an entity from NHibernate that is passed by ID via Ajax. The problem is that I recently switched over to the [Transaction]/ATM method of managing transactions with NHibernate. The problem with this method is that it creates a dynamic proxy of your controller so it can intercept the transaction. In doing so it looses the method argument attributes and then – boom – stack overflow. So now I’m off to hack YET ANOTHER open source project to see if I can get it to not remove the method parameter attributes. :(

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>