验证应用程序块和.NET 4.0代码合同

发布于 2024-08-25 03:54:28 字数 86 浏览 4 评论 0 原文

有人在 .NET 4.0 中使用代码契约实现了企业库 VAB 吗?

如果是这样,您能分享一些见解吗?对性能有帮助吗?还有其他需要考虑的因素吗?

Has anyone implemented the Enterprise Library VAB along wtih Code Contracts in .NET 4.0?

If so, can you share some insights? Did it help in performance? Any other factors to be considered?

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

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

发布评论

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

评论(1

岁月染过的梦 2024-09-01 03:54:28

这两个框架具有不同的范围,因此它们可以轻松地在同一项目中使用。但是,您必须防止在域实体中使用代码契约。当您执行此操作时,所有调用者必须确保它们不会设置无效值(执行此操作时将发生编译时错误)。但是,使用验证应用程序块时,必须允许您的实体具有(暂时)无效状态。否则 VAB 无法检测到无效对象。

换句话说,代码契约旨在防止编程错误,而不是用户错误,并且它为此提供了编译时支持。 VAB 可以防止用户错误,而不是编程错误*,并为此提供运行时支持。

*好吧,VAB 也可以用于编程错误(事实上,我自己确实以这种方式使用它),但主要场景是用户输入 IMO。

Both frameworks have a different scope so they could easily be used in the same project. However, you'll have to prevent using Code Contracts in your domain entities. When you do this, all callers must ensure that they don't set invalid values (a compile time error will occur when you do this). With Validation Application Block however, your entities must be allowed to have an (temporarily) invalid state. Otherwise VAB is unable to ever detect invalid objects.

Let me put it otherwise, Code Contracts is meant to prevent programming errors, not user errors and it gives compile time support for this. VAB prevents user errors, not programming errors* and gives runtime support for this.

*Okay, VAB could also be used for programming errors (and in fact, I do use it myself in this way) but main scenario is user input IMO.

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