title: Mapping domain model to view model via AutoMapper or not
body: <p>I want to use view model for display insted of domain model. And I want to customise a property for display, how should I do this? And is it a good practice to use AutoMapper for display?</p>
  
  <p>Below is the code sample:</p>
  
  <pre><code>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&lt;BookView&gt;(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; }
      }
  </code></pre>
  
  <p>If I use another way, I can customise any property, like below:</p>
  
  <pre><code>  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; }
      }
  </code></pre>
  
  <p>How should I do this? And is it a good practice to use AutoMapper for display?</p>
  
  <p><strong>Update</strong></p>
  
  <p>It seems using automapper in the scenario below is more appropriate.  For example, mapping a view model to domain model like below. Any opinions?</p>
  
  <pre><code>  [HttpPost]
      public ActionResult Create(BookView bookView)
      {
          try
          {
              var book = Mapper.Map&lt;Book&gt;(bookView);  //this is wrong
  
              bookService.SaveOrUpdate(book);
  
              return RedirectToAction("Index");
          }
          catch
          {
              return View();
          }
      }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>For complex custom display via view model, I don't want to use automapper to map display logic, assuming automapper can map it. Because it mixes different purposes. For example:</p>
  
  <pre><code>Mapper.CreateMap&lt;Book, BookView&gt;()
      .ForMember(dest =&gt; dest.Name, opt =&gt; opt.MapFrom(src =&gt; src.Name + " this is for display purpose"));
  </code></pre>
  
  <p>However, using manual mapping like below seems intuitive.</p>
  
  <pre><code> public BookView(Book book){
      //mapping here
  }
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>Quote from <a href="http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/">Jimmy Bogard</a>:</p>
  
  <blockquote>
    <p>I think using AutoMapper because you don’t want to use the “=”
    operator is a bit lazy.  Instead, we use it to flatten and reshape,
    optimizing for the destination type’s environment.  Remember, my
    original motivation for AutoMapper was:</p>
    
    <p>Enable protecting the domain layer from other layers by mapping to
    DTOs</p>
  </blockquote>
  
  <p>Thanks @AndrewWhitaker for the link</p>