c# - MVC .NET forms with a domain-driven design -


the pattern see in majority of .net mvc examples have model object passed between controller , lower layers , form-bind directly those. opted domain-driven approach have domain objects passed between layers. idea these objects passed views.

the problem i'm running when comes pages forms. intending have separate objects forms bind on way (which contain validation annotations). keeping separate domain object because single object possibly updated different pages, each page having it's own validation requirements (for example person's address might not shown on 1 page, required on another, validation wouldn't work on universal domain object). complicates things once postback form , need display errors.

take example update page. i'd type view domain person object , page have it's field values populated that. postback action take form object page , validate that. if passed use automapper copy values domain object off form , save. works. breaks down re-displaying page when there errors. if it's typed domain object i'd end repopulating fields based on old values instead of values user entered. if it's typed form object need translate domain objects these form objects every page (and still possibly have pass in domain object if need values i'd using read-only page).

i'm sure i'm overlooking / over-complicating here.

update interesting find after playing around because of @charlino said. if used typed html helpers make inputs (html.textboxfor()) remember values if view typed domain object. if use generic ones (html.textbox()) or raw html doesn't seem to.

having viewmodel object each , every view end doing.

it bit more up-front work, have define viewmodel , mapping, code simple , straightforward takes seconds write.

often, when need form, make separate form object, , have 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 view viewmodel, make post action method take form.

public class mycontroller {     [httpget]     public actionresult edit() { ... }      [httppost]     public actionresult edit(myviewsform form) { ... } } 

i make query methods retrieve viewmodel, 1 populates form , not - when return post'd form errors.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -