MVC .NET forms with a domain-driven design

The pattern I see in the majority of .NET MVC examples is to have your model be the object passed between the controller and lower layers and to form-bind directly to those. I opted for a domain-driven approach where I have domain objects passed between the layers. The idea was that these objects would be passed into the views.

The problem I'm running into is when it comes to pages with forms. I was intending to have separate objects for the forms to bind to on the way back (which would also contain the validation annotations). I was keeping that separate from the domain object because a single object could possibly be updated by different pages, with each page having it's own validation requirements (for example a person's address might not even be shown on one page, but required on another, so the validation wouldn't always work on the universal domain object). This complicates things once you postback the form and need to display errors.

Take for example an update page. I'd type the view to my domain Person object and the page would have it's field values populated from that. The postback action would take the form object for that page and validate that. If it passed I use automapper to copy the values to the domain object off the form and save. This all works. Where it breaks down is re-displaying that page when there are errors. If it's typed to the domain object I'd end up just repopulating the fields based on the old values instead of the values the user entered. If it's typed to the form object then I need to translate all my domain objects to these form objects for every page (and still possibly have to pass in the domain object if I need some values I'd be using read-only for that page).

I'm sure I'm overlooking / over-complicating something here.

Update An interesting find after playing around because of what @Charlino said. If I used the strongly typed html helpers to make the inputs (Html.TextBoxFor()) it will remember the values even if the view is typed to the domain object. If I use the generic ones (Html.TextBox()) or raw HTML it doesn't seem to.

Answers


Having a ViewModel object for each and every view is what I end up doing.

It is a bit more up-front work, as I have to define the ViewModel and mapping, but that code is so simple and straightforward it takes only seconds to write.

Often, when I need a form, I make a separate Form object, and have the ViewModel contain it.

public class MyViewModel 
{
    public string SomeNonFormDisplayValue { get; set; }
    public bool AnotherDisplayOnlyValue { get; set; }
    public IEnumerable<Tuple<int, string>> SelectionListItems { get; set; }

    public MyViewsForm Form { get; set; }
}


public class MyViewsForm
{
    public string EditableProperty { get; set; }
    public int SelectionListItemId { get; set; }
}

I type my View to the ViewModel, but I make the Post Action Method take just the form.

public class MyController
{
    [HttpGet]
    public ActionResult Edit() { ... }

    [HttpPost]
    public ActionResult Edit(MyViewsForm form) { ... }
}

I also make query methods to retrieve the ViewModel, one that populates the form and another that does not - for when I return the POST'd form with errors.


Need Your Help

writing keys of a dictionary to a text file

python python-3.x python-2.7

How can i write keys of a dictionary to a text file line by line, i.e. one key per line? If there is any link pointing to the same, please direct me to that link. I can find loading text file conte...

Java applet sound issues, sound continues on close, and I can't sign the sound files

java audio applet code-signing

So I was working on a mini game, and for the first time I added sound to my game, but I ran into two issues, the first is that when I load the applet up in my browser, and I close the tab when I'm ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.