in

Platinum Bay

Peace, Love, and Visual Studio ALM

This Blog

Syndication


.NETicated

DevEvents – LINQ to SQL Model Binding in .NET MVC

In a previous post, I outlined the generics-based Repository model I built for DevEvents. There are two additional pieces of extensibility to the model that I left out for the sake of clarity. The first one, and the one I’ll cover in this post, is LINQ to SQL model binding.

During the initial development of the DevEvents framework, I was using MVC model binding for forms and entities:

public ActionResult Create(FormCollection form)
{
var org = new Organization();
try
{
UpdateModel(org, form);

Unfortunately, from time to time I would receive a stack overflow exception when calling UpdateModel. This is because LINQ to SQL entities contain references as properties to related entities, and the model binding would iterate recursively through them, forever.

Fortunately, UpdateModel contains a generic overload that helps to solve this problem:

protected internal void UpdateModel<TModel>(TModel model) where TModel : class;

For TModel, I can specify an interface which contains properties for the fields to bind. I have 60 tables though, and I didn’t want to manually create interfaces for each of them, so I set about with a T4 template to auto-create the interfaces based on my LINQ to SQL model. The generated code lives in the same namespace as my model, and contains a base, empty interface:

public interface IFormBindable { }

Then, I iterate through the DBML XML and create an interface that implements IFormBindable for each table. Finally, I iterate through the columns, and create properties for each column that is a primitive type.

public interface IPresentationFormBindable : IFormBindable
{
    System.String Title { get; set; }
    System.String Abstract { get; set; }
    ...
}

In order to make my model entities bindable, I apply these custom interfaces to their respective types in model partial classes, such as:

public partial class Presentation : IPresentationFormBindable

Finally, I can call UpdateModel for a presentation as follows, and only the properties specified on the IFormBindable interface will be bound.

public ActionResult AddPresentation(string id, FormCollection form)
{
... Presentation presentation = _presentationRepository.GetPresentationByPresentationID(id); UpdateModel<IPresentationFormBindable>(presentation, form);
...

The second item I left out of the generic repository post for clarity was validation, which I plan to discuss in an upcoming post.

Hope this helps!

Comments

January 18, 2009 7:14 PM

Pingback from  Dew Drop - January 18, 2009 | Alvin Ashcraft's Morning Dew

 

January 21, 2009 3:39 AM

ASP.NET Routing and Rewriting [Via: scott ] Periodically Updating the Screen and Web Page Title with...

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.