title: Domain model to ViewModel and back again using repository pattern and Entity Framework in MVC3
body: <p>I have read here <a href="http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/" rel="nofollow noreferrer">http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/</a> about how you probably shouldn't be trying to un-flatten a flattened object, but considering how I am using a repository with Entity Framework, the Entity models are expected, not the ViewModels. </p>
  
  <p>I started wondering whether I should be taking a different approach, does anyone have any best practices for this sort of thing? or is it just time to start using ValueInjector <a href="https://stackoverflow.com/questions/3145062/using-automapper-to-unflatten-a-dto">Using AutoMapper to unflatten a DTO</a> ? and not being too concerned with mapping RecipeCreateViewModel back to Recipe?</p>
  
  <p>Below is my code to give you an idea of what I have at the moment.</p>
  
  <pre><code>// Entities
  public class Recipe {
      public int Id { get; set; }
  
      public string Name { get; set; }
      public Course Course { get; set; }
  }
  
  public class Course {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  // View Model
  public class RecipeCreateViewModel {
      // Recipe properties
      public int Id { get; set; }
      public string Name { get; set; }
  
      // Course properties, as primitives via AutoMapper
      [Required]
      public int CourseId { get; set; }
      // Don't need CourseName in the viewmodel but it should probably be set in Recipe.Course.Name
      //public string CourseName { get; set; }
  
      // For a drop down list of courses
      public SelectList CourseList { get; set; }
  }
  
  
  // Part of my View
  @model EatRateShare.WebUI.ViewModels.RecipeCreateViewModel
  ...
  &lt;div class="editor-label"&gt;
      Course
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @* The first param for DropDownListFor will make sure the relevant property is selected *@
      @Html.DropDownListFor(model =&gt; model.CourseId, Model.CourseList, "Choose...")
      @Html.ValidationMessageFor(model =&gt; model.CourseId)
  &lt;/div&gt;
  ...
  
  
  // Controller actions
  
  public ActionResult Create() {
      // map the Recipe to its View Model
      var recipeCreateViewModel = Mapper.Map&lt;Recipe, RecipeCreateViewModel&gt;(new Recipe());
      recipeCreateViewModel.CourseList = new SelectList(courseRepository.All, "Id", "Name");
      return View(recipeCreateViewModel);
  }
  
  [HttpPost]
  public ActionResult Create(RecipeCreateViewModel recipe) {
      if (ModelState.IsValid) {
          // set the course name based on the id that was posted
              // not currently checking if the repository doesn't find anything.
      recipe.CourseName = courseRepository.Find(recipe.CourseId).Name;
              var recipeEntity = Mapper.Map&lt;RecipeCreateViewModel, Recipe&gt;(recipe);
          recipeRepository.InsertOrUpdate(recipeEntity);
          recipeRepository.Save();
          return RedirectToAction("Index");
      } else {
          recipe.CourseList = new SelectList(courseRepository.All, "Id", "Name");
          return View(recipe);
      }
  }
  </code></pre>