如何查看型号映射回域模型在POST操作? [英] How to map View Model back to Domain Model in a POST action?

查看:128
本文介绍了如何查看型号映射回域模型在POST操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网上查到使用的ViewModels和利用Automapper每篇文章都给出了指引控制器 - >视图方向映射。你把一个域模型的所有选择列表一起到一个专门的视图模型,并把它传递给视图。这清晰细腻。结果
视图有一个形式,最终我们是在POST操作。在这里,所有的模型绑定莅临现场与一起的 [显然]是的 [显然]至少在命名约定的部分相关的原来的视图模型另一个的视图模型的绑定和验证着想。

Every article found in the Internet on using ViewModels and utilizing Automapper gives the guidelines of the "Controller -> View" direction mapping. You take a domain model along with all Select Lists into one specialized ViewModel and pass it to the view. That's clear and fine.
The view has a form, and eventually we are in the POST action. Here all the Model Binders come to the scene along with [obviously] another View Model which is [obviously] related to the original ViewModel at least in the part of naming conventions for the sake of binding and validation.

如何将其映射到你的领域模型?

让它成为一个插入的动作,我们可以使用相同的Automapper。但是,如果它是一个更新动作?我们必须从系统信息库中检索我们的域实体,更新按照视图模型中的值的属性并保存到存储库。

Let it be an insert action, we could use the same Automapper. But what if it was an update action? We have to retrieve our Domain Entity from the Repository, update it's properties according to the values in the ViewModel and save to the Repository.

附录1(2010年2月9日):有时,指定型号的性能是不够的。应该根据视图模型的价值对其采取的域模型有所行动。即,有些方法应在域模型调用。也许,应该有一种它以处理视图模型矗立控制器和域之间的应用服务层的...

ADDENDUM 1 (9th of February 2010): Sometimes, assigning Model's properties is not enough. There should be taken some action against Domain Model according to the values of View Model. I.e., some methods should be called on Domain Model. Probably, there should be a kind of an Application Service layer which stands between Controller and Domain in order to process View Models...

如何组织这个code和在何处放置它来实现以下目标?


  • 保持控制器瘦

  • 荣誉SoC的做法

  • 按照领域驱动设计原则

  • 干燥

  • 要继续...

推荐答案

我使用的 IBuilder 的界面和使用的 ValueInjecter

I use an IBuilder interface and implement it using the ValueInjecter

public interface IBuilder<TEntity, TViewModel>
{
      TEntity BuildEntity(TViewModel viewModel);
      TViewModel BuildViewModel(TEntity entity);
      TViewModel RebuildViewModel(TViewModel viewModel); 
}

...(实现)的 RebuildViewModel 的只是调用 BuildViewModel(BuilEntity(视图模型))

[HttpPost]
public ActionResult Update(ViewModel model)
{
   if(!ModelState.IsValid)
    {
       return View(builder.RebuildViewModel(model);
    }

   service.SaveOrUpdate(builder.BuildEntity(model));
   return RedirectToAction("Index");
}

顺便说一句,我不写的ViewModel我写输入的Cuz它的要短得多,但只是没有真正重要

希望它能帮助

btw I don't write ViewModel I write Input cuz it's much shorter, but that just not really important
hope it helps

更新:
我现在使用这种方法在 ProDinner ASP.net MVC演示应用
这就是所谓的IMapper现在,也有提供了一个PDF,其中这种做法详细解释

Update: I'm using this approach now in the ProDinner ASP.net MVC Demo App, it's called IMapper now, there's also a pdf provided where this approach is explained in detail

这篇关于如何查看型号映射回域模型在POST操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆