开发者

MVC .NET forms with a domain-driven design

开发者 https://www.devze.com 2023-01-28 08:55 出处:网络
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-drive

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 vali开发者_StackOverflow社区date 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号