title: How to convert DTO to View Model and then back again?
body: <p>I'm using MVC 4 with the repository pattern and unit testing also. I have a typical controller that has simple CRUD functionality. I've separated my View Models from my DTOs and I would like to know the best way to convert between the 2:</p>
  
  <p><strong>Models:</strong></p>
  
  <p>I have <code>Admin.Models.Product</code> which is my view model and <code>AdminAssembly.Models.Product</code> which is my DTO.</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>    //repo that handles product operations
      AdminAssembly.Interfaces.IEntityRepository&lt;AdminAssembly.Models.Product&gt; db;
      //default constructor
      public ProductController() { db = new AdminAssembly.Repositories.EntityRepo&lt;AdminAssembly.Models.Product&gt;(new AdminAssembly.Models.EntitiesContext()); }
      //unit testing constructor
      public ProductController(AdminAssembly.Interfaces.IEntityRepository&lt;AdminAssembly.Models.Product&gt; context) { db = context; }
  
      //
      // POST: /Product/Create
  
      [HttpPost]
      public ActionResult Create(Admin.Models.Product product) {
  
          if (ModelState.IsValid) {
              //COMPILE-ERROR: how to convert to DTO?
              db.Add(product);
          }
          return View();
  
      }
  
      //
      // GET: /Product/Edit/5
  
      public ActionResult Edit(int id) {
          //COMPILE-ERROR: how to convert to view model?
          Admin.Models.Product product = db.GetAll().Where(p =&gt; p.ID == id);
  
          return View(product);
      }
  </code></pre>
  
  <p><strong>How do I convert between the 2?</strong></p>
  
  <p>Do I reference my DTO assembly in my view model and do something like: (won't this break my unit testing?)</p>
  
  <pre><code>//convert to AdminAssembly.Models.Product
  db.Add(product.ToDTO());
  
  //convert back to Admin.Models.Product via constructor
  Admin.Models.Product product = Admin.Models.new Product(db.GetAll().Where(p =&gt; p.ID == id));
  </code></pre>
  
  <p>Do I need some sort of object conversion black box?</p>
  
  <pre><code>Converter.ToViewProduct(product);
  </code></pre>
  
  <p>Some sort of interface?</p>
  
  <p>or something else?</p>
  
  <p><strong>Update 1:</strong></p>
  
  <pre><code>    public static class Product {
          public static Admin.Models.Product ToView(AdminAssembly.Models.Product dto) {
              Admin.Models.Product viewProduct = new Admin.Models.Product();
              //straight copy
              viewProduct.Property1 = dto.Property1;
              viewProduct.Property2 = dto.Property2;
  
              return viewProduct;
          }
          public static AdminAssembly.Models.Product ToDTO(Admin.Models.Product viewModel) {
              AdminAssembly.Models.Product dtoProduct = new AdminAssembly.Models.Product();
              //straight copy
              dtoProduct.Property1 = viewModel.Property1;
              dtoProduct.Property2 = viewModel.Property2;
  
              //perhaps a bit of wizza-majig
              dtoProduct.Property1 = viewModel.Property1 + viewModel.Property2;
  
              return dtoProduct;
          }
      }
  </code></pre>