DataAnnotations 与 IDataErrorInfo

发布于 2024-08-18 17:21:05 字数 80 浏览 7 评论 0原文

DataAnnotations 与 IDataErrorInfo

两者的优缺点? 一种相对于另一种的好处? (特别是与MVC相关)

DataAnnotations vs IDataErrorInfo

Pros and Cons of both?
Benefits of one over the other? (especially related to MVC)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-08-25 17:21:05

较晚进入讨论,因为我不想开始新问题。我的出发点是确定适用于中型 ASP.NET MVC 项目的最佳实践。

让我首先总结一下我们的选择:-

1) IDataErrorInfo 很容易实现。您所需要的只是在 Model 类中派生 IDataErrorInfo 。问题是您要让模型绑定强制执行您的业务规则。业务规则应由模型执行。 IDataErrorInfo(以及 DataAnnotations)的另一个问题是(摘自 Steven Sanderson 的书 它无法报告与单个属性相关的多个错误或与整个对象模型相关的多个错误

2) DataAnnotation 对我来说就像模式检查(验证)。这是您的应用程序应该执行的第一个检查。但是(恕我直言),它不适合实施您的业务规则。

3)实现你自己的ModelBinder。虽然这是可以做到的,但认真地说,ModelBinder 的用途是解析数据并将其绑定到模型,而不是执行复杂的验证和业务规则检查。我会将业务规则检查保留在您的模型/域层中实现。

4) 自己动手 - 使用服务层进行验证(请参阅 this。显示的示例具有使用接口类与控制器和模型状态解耦的优点。另一个选项是从模型层抛出适当的异常,后一个选项在您实现服务层时很有用。在单独的应用程序(例如 WCF 应用程序)中,

对于中型到大型项目,您使用了(或打算采用)上述哪些选项,为什么?

您认为

Late entry to the discussion as I do not want to start a new question. Where I am coming from is to determine the best practice to apply to a medium size ASP.NET MVC project.

Let me first summarise our options :-

1) IDataErrorInfo is simple to implement. All you need is to derive IDataErrorInfo in your Model class. The catch is that you are letting your model binding to be enforcing your business rules. Business rules should be enforced by the Model. The other catch for IDataErrorInfo (and likewise for DataAnnotations) is that (paraphrasing from Steven Sanderson's book it could not report multiple errors relating to a single property or multiple errors relating to the whle object model.

2) DataAnnotation to me, is like a schema check (validation). This is the first check that your application should do. However (IMHO), it is not suited to implement your business rules.

3) Implement your own ModelBinder. Although this can be done but seriously speaking, the usage of ModelBinder is to parse and bind your data to your model and not to perform complicated validations and business rule checks. I would leave the Business rules check to be implemented in your Model/Domain layer.

4) Roll your own - Validating with a service layer (see this. The example shown has its advantage of decoupling from the Controller and Model State using an interface class. Another option is to throw an appropriate exception from your model layer. The latter option is useful when you implement your service layer in a separate application (e.g a WCF application).

What do you think? For a medium to large size project, which of the above options have you used (or intend to adopt) and why?

Cheers

晨曦慕雪 2024-08-25 17:21:05

看起来 DataAnnotations 在 MVC 2.0 中获得了官方支持。 Scott Guthrie 发布了一个好的文章关于使用 DataAnnotations 在 2.0 中进行模型验证的文章。鉴于该团队似乎正朝着这个方向发展,您可能会认为这是投了赞成票。

Looks like DataAnnotations are getting official support in MVC 2.0. Scott Guthrie published a good article on doing model validation in 2.0 using DataAnnotations. Given that the team seems headed this direction, you might consider that a vote in its favor.

冬天的雪花 2024-08-25 17:21:05

DataAnnotations 更容易实现,并且在 MVC 2.0 中得到直接支持。

但是,IDataErrorInfo 允许您进行更复杂的验证(即:生成多个属性的验证等)。

然而,两者可以混合并一起使用。没有什么可以阻止您实施这两种技术。

DataAnnotations are easier to implement, and getting directly support in MVC 2.0.

However, IDataErrorInfo allows you do to more complex validation (ie: validation that spawns multiple properties, etc).

The two can be mixed, and used together, however. There is nothing stopping you from implementing both techniques.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文