使用属性名称中不带字符串的 NHibernate 限制

发布于 2024-09-16 17:59:57 字数 343 浏览 4 评论 0原文

创建条件时,您可以添加适用于属性的限制。有两种创建限制的方法:

Restrictions.Eq(string propertyName, object value)

Restrictions.Eq(IProjectionprojection, object value)

事情是,我不愿意将属性名称作为字符串传递,因为如果它们发生变化,我的项目将照常编译,任何可能的命名不匹配都会仅在单元测试期间才能找到。我正在尝试实现 TDD,但它不会很快发生,因此我正在尝试限制单元测试的依赖性以检查错误(直到我们设法拥抱 TDD)。

有什么想法吗? 提前致谢!

When you create a criteria, you can add Restrictions that apply to a property. There are 2 ways of creating a Restriction:

Restrictions.Eq(string propertyName, object value)

or
Restrictions.Eq(IProjection projection, object value)

Thing is, I don't feel comfortable passing property names as strings, since if they ever change, my project will compile as usual and any possible naming mismatch will only be found during unit testing. I'm trying to implement TDD but it won't happen anytime soon, so I'm trying to limit dependencies of unit testing to check for errors (until we manage to embrace TDD).

Any ideas?
Thanks in advance!

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

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

发布评论

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

评论(3

感性不性感 2024-09-23 17:59:57

使用 LINQ,QueryOver (NH3.x) 或 NH Lambda 扩展 (NH2.x) 或 NHibernate 表达式扩展 (NH1.2)

所有它们比仅使用字符串具有更强的类型。

Use LINQ, QueryOver (NH3.x) or NH Lambda Extensions (NH2.x) or NHibernate Expression Extensions (NH1.2)

All of these are more strongly-typed than just using strings.

栖迟 2024-09-23 17:59:57

我能感觉到你的痛苦。

但是没有办法使用标准 API 中的投影来动态引用您的属性,就像您无法使 nHibernate 映射检测表或列名称更改一样。

在大多数情况下,使用“属性”逐步查找和替换是可行的。

I can feel your pain.

But there is no way to put dynamic reference to your properties using projections in criteria API, just like you can't make nHibernate mapping detect a table or column name change.

Doing a find and replace step by step with "Property" with work in most cases.

撩发小公举 2024-09-23 17:59:57

只要您完全覆盖了单元测试(您不是吗?),这应该不是问题,不是吗:-)?

避免使用字符串作为属性名称的另一个原因是使用 Resharper 等进行重构无法修复这些引用。这就是 Fluent NHibernate 真正的闪光点。

As long as you have complete coverage of unit tests (you don't you?) it shouldn't be a problem, should it :-)?

Another reason to avoid using strings for property names is that refactoring using e.g. Resharper cannot fix those references. This is where Fluent NHibernate really shines.

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