c# - Mapping domain model to view model via AutoMapper or not -


i want use view model display insted of domain model. , want customise property display, how should this? , practice use automapper display?

below code sample:

public class bookcontroller : basecontroller     {         private ibookservice bookservice;          public bookcontroller(ibookservice bookservice)         {             this.bookservice = bookservice;         }          public actionresult details(int id)         {             var book = bookservice.getbookbyid(id);              return view(mapper.map<bookview>(book));         } }      public class book      {                 public virtual int id { get; set; }         public virtual string name { get; set; }     }      public class bookview     {         public int id { get; set; }         public string name { get; set; }     } 

if use way, can customise property, below:

  public actionresult details(int id)         {             var book = bookservice.getbookbyid(id);              return view(new bookview(book));         }      public class bookview     {        public bookview(book book){          name = book.name +" decorated";        }         public int id { get; set; }         public string name { get; set; }     } 

how should this? , practice use automapper display?

update

it seems using automapper in scenario below more appropriate. example, mapping view model domain model below. opinions?

  [httppost]     public actionresult create(bookview bookview)     {         try         {             var book = mapper.map<book>(bookview);  //this wrong              bookservice.saveorupdate(book);              return redirecttoaction("index");         }         catch         {             return view();         }     } 

update 2

for complex custom display via view model, don't want use automapper map display logic, assuming automapper can map it. because mixes different purposes. example:

mapper.createmap<book, bookview>()     .formember(dest => dest.name, opt => opt.mapfrom(src => src.name + " display purpose")); 

however, using manual mapping below seems intuitive.

 public bookview(book book){     //mapping here } 

update 3

quote jimmy bogard:

i think using automapper because don’t want use “=” operator bit lazy. instead, use flatten , reshape, optimizing destination type’s environment. remember, original motivation automapper was:

enable protecting domain layer other layers mapping dtos

thanks @andrewwhitaker link

this use case automapper (i've used way extensively on many projects success). not want expose domain entities view (in mvc, exposing model directly view, incorrect).

you not need 1-1 mapping between domain entity , viewmodel. can make them different , customize mapping in createmap<> call. use example:

mapper.createmap<book, bookview>()     .formember(dest => dest.name, opt => opt.mapfrom(src => src.name + " decorated")); 

worst case, can ditch automapper complex cases or use custom type resolver automapper job done.

in fact, this how jimmy bogard (the author) recommends using automapper. mentions mapping domain entities asp.net mvc viewmodels use typed views.

another advantage can unit test mapping profiles. way if end mismatch between viewmodel , model you'll failing unit test.

updates:

i think quote added question further supports using automapper mapping domain models viewmodels:

instead, use flatten , reshape, optimizing destination type’s environment.

so in example you'd optimizing destination type's environment (in case view).

also per link reference above should not using automapper map to domain, from. in mind, you'll have write logic create/update domain entities receive view no matter what. remember controller actions should not take domain entities directly (you should not trust data comes directly view--let model determine if domain entity valid or not).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -