gpt4 book ai didi

c# - 使用 MVC3 中的存储库模式和 Entity Framework 将域模型转换为 ViewModel 并再次返回

转载 作者:行者123 更新时间:2023-11-30 12:14:17 24 4
gpt4 key购买 nike

我在这里读过http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/关于您可能不应该尝试取消扁平化对象的方式,但考虑到我如何使用带有 Entity Framework 的存储库,实体模型是预期的,而不是 ViewModels。

我开始怀疑我是否应该采取不同的方法,有人对这类事情有任何最佳实践吗?或者是时候开始使用 ValueInjector Using AutoMapper to unflatten a DTO ?并且不太关心将 RecipeCreateViewModel 映射回 Recipe?

下面是我的代码,让您了解我目前拥有的东西。

// 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);
}
}

最佳答案

如果你遵循“自然”(非常主观)的流程,它就像这样

创建模型

ViewModel -> Mapper -> Domain Entity -> Repository -> Mapper -> Persistence Entity

显示更新 View 模型

Persistence Entity -> Mapper -> ViewModel

在第一种情况下,您将 dto 处理成域实体(应用业务规则等),然后将其发送到以特定方式持久保存的存储库(EF 实体)

在第二种情况下,您想要加载一个 View 模型(DTO),它将用于更新领域模型。您不是重新加载整个域实体然后将其映射到 DTO,而是直接从存储库中执行此操作。

您可能会说这种情况不适用于您,因为您直接针对 EF 工作。好吧,这就是诀窍,域模型!=持久性模型!= View 模型。他们都是不同的,有不同的关注点。

因此,通过适当的分离,您将始终拥有: View 模型 -> 映射 -> 域实体 -> 映射 -> 持久性实体,并且在相反的方向:持久性实体 -> 映射 -> View 模型

关于c# - 使用 MVC3 中的存储库模式和 Entity Framework 将域模型转换为 ViewModel 并再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9899407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com