NHibernate.Validator 与 DataAnnotations

发布于 2024-09-08 03:47:21 字数 409 浏览 5 评论 0原文

在我的 APS.NET MVC 项目中,我使用 DataAnnotations 进行验证。

我从 L2S 迁移到 NHibernate orm,事实上发现 NHibernate 有自己的验证器 (NHibernate.Validator)

迁移到是否有意义NHibernate.Validator 也是如此吗?

例如 DataAnnotations 有 [Required] 属性和 NHibernate.Validator [NotEmpty、NotNull、NotNullNotEmpty],它让我思考该使用什么。

In my APS.NET MVC project I'm using DataAnnotations for validations.

I moved from L2S to NHibernate orm and in fact found that NHibernate has its own validator (NHibernate.Validator)

Does it make sense to move to NHibernate.Validator as well?

For example DataAnnotations has [Required] attribute and NHibernate.Validator [NotEmpty, NotNull, NotNullNotEmpty] and it makes me think what to use.

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

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

发布评论

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

评论(1

成熟的代价 2024-09-15 03:47:21

我已经在生产项目中使用过这两种方法,如果您有时间进行切换,我会强烈推荐 NHibernate.Validator,原因如下:

  1. NHibernate.Validators 有一个
    更丰富的验证属性集
    (例如,少数你
    上面提到)
  2. 如果实施
    正确地,NHibernate.Validators
    验证更容易进行单元测试。

第一个对我来说并不重要,可能也不适合你,因为 DataAnnotations 中的属性集非常完整(如果需要,你可以回退到 RegEx),但不是。 2 对我来说很重要,因为我希望能够将数据验证作为我的域模型单元测试的一部分,而不是仅通过 WatiN 或 Selenium 通过 UI/Web 测试来测试这些内容。使用验证器还允许我混合域模型规则验证(属性 X 或 Y 必须有一个值,但两者不能为空),而不必去另一个地方这样做。

有关使用 NHibernate 验证器的一些基本指南,请查看这篇文章:http://nhibernate.info/blog/2009/04/02/nhibernate-validator-and-asp-net-mvc.html,我也建议获取 S 的源代码#arp Architecture,Billy McCafferty 用于创建 DDD 风格 ASP.NET MVC 应用程序的出色框架。特别是,请查看他的 Validators 实现以及您需要创建的 Validator ModelBinder,以将 NHibernate 验证错误传输到 MVC ModelErrors 中。在此处下载 S#arpArchitecture 源代码:http://github.com/codai/Sharp-Architecture

底线是:使用 NHibernate.Validators 是更具可扩展性、可测试性的选项,但需要做一些工作才能正确使用它。 DataAnnotations 已融入框架中,并且更易于运行,这是毫无疑问的。

希望有帮助。

I've used both in production projects and, if you have the time to make the switch, I would highly reccomend NHibernate.Validator for a couple of reasons:

  1. NHibernate.Validators has a
    richer set of validation attributes
    (for example, the handful you
    mention above)
  2. If implemented
    properly, NHibernate.Validators
    validations are easier to unit test.

No. 1 wasn't huge for me, and may not be for you, as the set of attributes in DataAnnotations is pretty complete (and you can fall back to RegEx, if need be), but no. 2 was a big deal for me because I wanted to be able to include data validation as a part of my Domain Model unit tests, as opposed to testing those only through UI/Web testing via WatiN or Selenium. Using Validators also allowed me to mix Domain Model rule validation (Property X OR Y must have a value, but both cannot be null) without having to go to another place to do so.

For some basic guidance on using NHibernate Validators, check out this article: http://nhibernate.info/blog/2009/04/02/nhibernate-validator-and-asp-net-mvc.html, and I would also reccomend getting the source for S#arp Architecture, Billy McCafferty's great framework for creating DDD-style ASP.NET MVC applications. In particular, check out his implementation of Validators and the Validator ModelBinder you'll need to create to transfer NHibernate validation errors into MVC ModelErrors. Download the S#arpArchitecture source here: http://github.com/codai/Sharp-Architecture.

The bottom line is this: Using NHibernate.Validators is the more extensible, testable option, but it will take some doing for you to use it properly. DataAnnotations is baked into the framework and easier to get running with, there's no question about that.

Hope that helps.

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