asp.net-mvc-4 - Asp.NET MVC - DataAnnotations 和 ModelState.IsValid 对域模型的侵入性太强?

标签 asp.net-mvc-4 entity-framework-5 data-annotations separation-of-concerns

我正在从书中学习 ASP.NET MVC Pro ASP.NET MVC 4 (顺便说一下,到目前为止我喜欢它)。

我还在开始章节,它向我展示了 System.ComponentModel.DataAnnotations命名空间属性,如何用这些注释散布我的模型类,然后如何使用它们来检查模型是否有效(ModelState.IsValid 中的 Controller)。

例如:

public class GuestResponse
{
    [Required(ErrorMessage = "Please enter your name"]
    public string Name { get; set; }
}

...

public ViewResult RsvpForm(GuestResponse guestResponse)
{
    if(ModelState.IsValid)
    {
        return View("Thanks", guestResponse);

    }

}

有几件事让我感到不安。
  • 为什么我想要在我的域模型中散布一堆属性?我喜欢我的领域模型纯粹并且没有任何特定于实现的东西,任何现实世界的模型都太复杂而不能像这样使用声明式验证。
  • 不是ErrorMessage验证属性的参数有点View有关的?类似的东西不属于 UI层?例如...如果由于空间限制,我希望移动版本不是说“请输入您的姓名”而是说“需要姓名”怎么办?但它在我的模型中!
  • 我为什么要使用ModelState.IsValid确定模型的状态?模型不应该告诉我吗?我明白 ModelState正在使用 DataAnnotations我的模型中的属性,但这似乎只适用于非常简单的模型。更复杂的模型甚至可能没有有效/无效状态,它可能只有不同的阶段和状态。我在这里有点漫不经心,但我不喜欢声明性地说明是什么使我的模型有效或无效的想法。

  • 对这些想法的任何建议、保证或验证将不胜感激。

    最佳答案

    以下是我对您问题的回答:

    1) Why do I want a bunch of attributes littered throughout my domain model? I like my domain model pure and free from any stuff that is implementation specific, and any real world model would be too complex to just use declarative validation like this.



    你绝对不想要这个。您想要的是拥有一个专为您的 View 目的而设计的 View 模型。正是这个 View 模型将包含数据注释,而不是您的域模型。然后 Controller 将在域模型和 View 模型之间进行映射,并将 View 模型传递给 View 。将 View 模型视为一个或多个域模型的投影。为了简化域和 View 模型之间的映射,您可以查看 AutoMapper .基本的经验法则是 View 不应该知道您的域模型。

    2) Aren't the ErrorMessage parameters of the validation attributes somewhat View related? Doesn't something like that belong in the UI layer? For example...what if due to space constraints I want the mobile version to instead of saying "Please enter your name" say "Name required"? But here it is in my model!



    完全同意你的看法。这就是为什么你应该有一个专门为 View 目的而设计的 View 模型类的原因。

    3) Why do I want to use ModelState.IsValid to determine the status of the model? Shouldn't the model tell me? I understand that ModelState is making use of the DataAnnotations attributes that are in my model, but this seems like it would only work for very simple models. A more complex model might not even have a valid/invalid state, it might just have various stages and states. I'm sort of rambling here, but I don't like the idea of declaratively saying what makes my model valid or invalid.



    我再一次同意你的看法。声明式验证(例如您使用数据注释开箱即用的内容)非常适用于 Hello World 类型的应用程序,但是一旦您开始编写具有复杂验证规则的实际应用程序,您很快就会意识到声明式方法根本无法解决问题.正是因为这个原因,我才使用 FluentValidation.NET .它为您提供了一种非常漂亮流畅的语法来表达任意复杂的验证规则,它 integrates easily with ASP.NET MVC 并允许 unit test your validation rules 完全隔离。

    关于asp.net-mvc-4 - Asp.NET MVC - DataAnnotations 和 ModelState.IsValid 对域模型的侵入性太强?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20812448/

    相关文章:

    entity-framework - Entity Framework 优化include,提供Inclusion提示

    asp.net - 用于验证的数据注释,至少一个必填字段?

    asp.net-mvc-3 - MVC3 中 [DataType(DataType.Email)] 的验证失败

    c# - 两个查询,一个 View

    c# - 如何在自定义验证属性中访问 View 模型的属性值以更改消息?

    c# - MVC 5 全局用户帐户对象

    javascript - PhoneAttribute 的 MVC 客户端验证

    c# - 认证成功后如何获取用户信息?

    asp.net - 将对象列表添加到 Entity Framework 中的数据库上下文

    c# - 有没有一种扩展代码优先迁移的好方法