模型时,形式张贴在控制器绑定 - 为什么要使用从域模型视图模型,而不是阶级? [英] Model binding in controller when form is posted - why to use view model instead of class from domain model?

查看:171
本文介绍了模型时,形式张贴在控制器绑定 - 为什么要使用从域模型视图模型,而不是阶级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然相当新的ASP.NET MVC 3,我所遇到的视图模型及其对从控制器将数据传递到视图的使用。在我最近的<一个href=\"http://stackoverflow.com/questions/8883264/model-binding-in-the-controller-when-form-is-posted-navigation-properties-are\">question在模型绑定两位专家建议,我应该使用视图模型,模型结合为好。

I'm still reasonably new to ASP.NET MVC 3. I have come across view models and their use for passing data from a controller to the view. In my recent question on model binding two experts suggested that I should use view models for model binding as well.

这是我以前没有遇到过的。但是,这两个家伙都向我保证,这是最好的做法。可能有人也许棚上的原因一些轻为什么视图模型更适合模型绑定?

This is something I haven't come across before. But both guys have assured me that it is best practise. Could someone maybe shed some light on the reasons why view models are more suitable for model binding?

下面是一个例子情况:我有一个简单的类在我的域模型

Here is an example situation: I have a simple class in my domain model.

public class TestParent
{
    public int TestParentID { get; set; }
    public string Name { get; set; }
    public string Comment { get; set; }
}

这是我的控制器:

And this is my controller:

public class TestController : Controller
{
    private EFDbTestParentRepository testParentRepository = new EFDbTestParentRepository();
    private EFDbTestChildRepository testChildRepository = new EFDbTestChildRepository();

    public ActionResult ListParents()
    {
        return View(testParentRepository.TestParents);
    }

    public ViewResult EditParent(int testParentID)
    {
        return View(testParentRepository.TestParents.First(tp => tp.TestParentID == testParentID));
    }

    [HttpPost]
    public ActionResult EditParent(TestParent testParent)
    {
        if (ModelState.IsValid)
        {
            testParentRepository.SaveTestParent(testParent);
            TempData["message"] = string.Format("Changes to test parents have been saved: {0} (ID = {1})",
                                                        testParent.Name,
                                                        testParent.TestParentID);
            return RedirectToAction("ListParents");
        }
        // something wrong with the data values
        return View(testParent);
    }
}

所以,当一个HTTP POST到达我用TestParent的模型绑定它被调用第三操作方法。这感到很方便,因为其产生的HTTP POST请求的浏览器页面包含TestParent的所有属性输入字段。实际上我觉得就是这样的Visual Studio提供了CRUD操作模板工作以及方式。

So in the third action method which gets invoked when an HTTP POST arrives I used TestParent for model binding. This felt quite convenient because the browser page that generates the HTTP POST request contains input fields for all properties of TestParent. And I actually thought that's the way the templates that Visual Studio provides for CRUD operations work as well.

不过,我得到的建议是,第三个操作方法的签名应该读公众的ActionResult EditParent(TestParentViewModel视图模型)

However the recommendation that I got was that the signature of the third action method should read public ActionResult EditParent(TestParentViewModel viewModel).

推荐答案

这听起来起初吸引人,但由于模型和视图操作得到越来越复杂,你开始看到使用的ViewModels的(大部分)一切的价值,尤其是输入场景。

It sounds appealing at first, but as your models and view actions get increasingly complex, you start to see the value of using ViewModels for (most) everything, especially input scenarios.


  • 案例1 - 大多数Web框架很容易受到过度张贴。如果您是直接绑定到你的域模型,这是非常有可能过度后的数据和恶意​​改变的东西不属于用户。我觉得清洁绑定到一个输入视图模型不是让白名单或黑名单的一长串名单,虽然有一些其他有趣的方式与绑定的接口。

  • Case 1 - Most web frameworks are susceptible to over-posting. If you are binding straight to your domain model, it is very possible to over-post data and maliciously change something not belonging to the user. I find it cleaner to bind to an input view model than have long string lists of white lists or black lists, although there are some other interesting ways with binding to an interface.

案例2 - 当你投入增长的复杂性,你当你需要提交,而不是直接在域模型验证字段(我同意复选框,等)

Case 2 - As your input grows in complexity, you'll run into times when you need to submit and validate fields not directly in the domain model ('I Agree' checkboxes, etc)

案例3 - 更多的是个人的事情,但我觉得模型绑定到关系的域对象是在时间的巨大痛苦。更容易在他们比AutoMapper处理MVC的ModelBinder的复杂对象图联系起来。 MVC的HTML辅助也是对基本类型不是很深的关系模型的工作更加顺畅。

Case 3 - More of a personal thing, but I find model binding to relational domain objects to be a giant pain at times. Easier to link them up in AutoMapper than deal with MVC's modelbinder for complicated object graphs. MVC's html helpers also work more smoothly against primitive types than deep relational models.

使用的ViewModels的底片是,它不是很干

The negatives of using ViewModels is that it isn't very DRY.

所以这个故事的寓意是,结合领域模型可以是简单的事情,一个可行的解决方案,但随着复杂性的增加,它变得更容易有一个单独的视图模型,然后在两者之间进行映射。

So the moral of the story is, binding to domain models can be a viable solution for simple things, but as the complexity increases, it becomes easier to have a separate view model and then map between the two.

这篇关于模型时,形式张贴在控制器绑定 - 为什么要使用从域模型视图模型,而不是阶级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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