views:

624

answers:

1

So let's say we have a domain object such as the following

public class Person
{
   public string Name { get; set; }
   public IList<PhoneNumber> PhoneNumbers {get; set; }
   public IList<Address> Addresses { get; set; }
}

The Person is not valid until a name, phone numbers, and addresses have been entered. How do you guys handle this using ASP.NET MVC and forms...

I was thinking you could serialze the Person to session and have multiple views for editing Name, adding phone numbers, adding addresses - the controller actions would modify the person in the session and a final Save action would push to database.

I don't really like having multiple views and using the session. Another option would be to have a single very complex form that could have "dynamic" sections of elements for adding/removing phone numbers, addresses within the browser prior to posting to the save action.

What is everyone doing with complex objects and editing via forms?

Thanks!

+1  A: 

I would usually use the "dynamic section" route.

However, I would not make your validation so strict that the user is unable to save work in progress. A list of phone numbers, addresses, etc., can take quite a while to enter. It is beneficial to the end-user to be able to save their work from time to time in case they lose Internet connectivity or something. It's probably a good idea to save the records automatically via AJAX from time to time if your data entry form is quite large. (Like Gmail.) Therefore, your model should allow them to save incomplete work from time to time, and run the whole validation only when they say they are "done."

Craig Stuntz
Thanks Craig, I like that idea as well. I think dynamic forms are a lot easier from an end user perspective. My model binder will have to get a bit smarter to handle dynamic fields, but I think we can make it work.
cain
Jimmy Nilsson advocates saving in an invalid state in Applying Domain-Driven Design and Patterns. He advocates that you should be able to save your work at any time, but that it must pass validation before entering an accepted state.
Daniel Auger