DataAnnotations 与 IDataErrorInfo
DataAnnotations 与 IDataErrorInfo
两者的优缺点? 一种相对于另一种的好处? (特别是与MVC相关)
DataAnnotations vs IDataErrorInfo
Pros and Cons of both?
Benefits of one over the other? (especially related to MVC)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
较晚进入讨论,因为我不想开始新问题。我的出发点是确定适用于中型 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
看起来 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.
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.