什么可以用作 NHibernate QueryOver 别名?
到目前为止,我知道局部变量或局部属性可以用作别名,
ClassA _aliasA;
_session.QueryOver(x => x.ClassA, () => _aliasA);
或者
ClassA AliasA { get; set; }
_session.QueryOver(x => x.ClassA, () => AliasA);
我想知道还有哪些其他选项是可能的。比如,外部类的属性是一个有效的选项吗?
class ClassGenericAliases
{
ClassA Class { get; set; }
}
_session.QueryOver(x => x.ClassA, () => ClassGenericAliases.ClassA);
静态可以用作别名吗? 还有其他声明别名的选项吗?
I know so far that a local variable or a local property can be used as an alias like so
ClassA _aliasA;
_session.QueryOver(x => x.ClassA, () => _aliasA);
or
ClassA AliasA { get; set; }
_session.QueryOver(x => x.ClassA, () => AliasA);
I want to know what other options are possible. Like, are properties of an external class a valid option?
class ClassGenericAliases
{
ClassA Class { get; set; }
}
_session.QueryOver(x => x.ClassA, () => ClassGenericAliases.ClassA);
Can statics be used as aliases?
Are there other options for declaring aliases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议永远不要在使用别名的方法范围之外使用任何别名。
QueryOver 是 Criteria 的强类型版本,在 Criteria 中别名是字符串值。
但现在它需要将别名分配给一个变量,因此我们只需为其创建一个别名:
从 NHForge 文档中,它显示了以下内容:
http://nhibernate.info/doc/nh/en/index.html#queryqueryover-aliases
因此,坚持只在方法范围内使用变量。
I would recommend never using anything for an Alias outside of the scope of the method that uses the alias.
QueryOver is a strongly typed version of Criteria, in Criteria an alias was a string value.
But now it needs to assign the alias to a variable so we just create one for it:
From NHForge documentation, it says the following:
http://nhibernate.info/doc/nh/en/index.html#queryqueryover-aliases
So stick to just using a variable in the scope of the method.
我需要解决类似的问题并决定采用别名命名约定。然后,无论您需要重用别名,都可以使用 GetCriteriaByAlias() 检查别名,如果不存在则添加它。
如果您有不同的选择投影,那么能够重用别名会非常方便。如果有人无视命名约定,此方法仍然存在问题,但您的单元测试应该会发现这一点。
I needed to solve a similar issue and decided on an alias naming convention. Then where ever you needed to reuse the alias you can check for it using GetCriteriaByAlias() and add it if it is not there.
Being able to reuse the alias is very handy if you have different select projections. This method is still problematic if someone disregards naming conventions, but then your unit tests should pick that up.