我在這裏http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/如何你可能不應該試圖讀取非扁平化扁平物體,但考慮到我是如何使用存儲庫實體框架,實體模型是預期的,而不是ViewModels。域模型視圖模型,然後再返回使用存儲庫模式和Entity Framework在MVC3
我開始想我是否應該採取不同的方法,沒有任何人有這樣的事情的最佳做法?還是剛開始使用ValueInjector Using AutoMapper to unflatten a DTO?而不是太在意將RecipeCreateViewModel映射回配方?
下面是我的代碼給你什麼,我目前所面對的一個想法。
// 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
...
<div class="editor-label">
Course
</div>
<div class="editor-field">
@* The first param for DropDownListFor will make sure the relevant property is selected *@
@Html.DropDownListFor(model => model.CourseId, Model.CourseList, "Choose...")
@Html.ValidationMessageFor(model => model.CourseId)
</div>
...
// Controller actions
public ActionResult Create() {
// map the Recipe to its View Model
var recipeCreateViewModel = Mapper.Map<Recipe, RecipeCreateViewModel>(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<RecipeCreateViewModel, Recipe>(recipe);
recipeRepository.InsertOrUpdate(recipeEntity);
recipeRepository.Save();
return RedirectToAction("Index");
} else {
recipe.CourseList = new SelectList(courseRepository.All, "Id", "Name");
return View(recipe);
}
}
感謝Mike的回答,我的問題不是很清楚,因爲我不確定幾件事情,並且一次詢問幾件事情。即時消息關閉後,您已經清楚地瞭解了創建和顯示圖層。我一直在努力的問題是從視圖模型返回映射。 – Pricey 2012-03-29 15:53:45
@MikeSW爲什麼在從PM轉換回VM時跳過域模型?從DM映射你的VM不是更好嗎?這樣你在上層只做一次。 – 2013-04-19 14:59:00
@JoaoLeme不是。顯示更新視圖模型只需要從持久性實體已經可用的模型中保存數據。所以不是填充整個對象,而是直接查詢存儲以獲取相關數據。這幾乎是CQRS – MikeSW 2013-04-19 15:15:10