什么可以用作NHibernate QueryOver别名? [英] What can be used as a NHibernate QueryOver alias?

查看:82
本文介绍了什么可以用作NHibernate QueryOver别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我知道可以将局部变量或局部属性用作别名

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);

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?

推荐答案

我建议不要在使用别名的方法范围之外使用任何别名.

I would recommend never using anything for an Alias outside of the scope of the method that uses the alias.

QueryOver是Criteria的强类型版本,在Criteria中,别名是字符串值.

QueryOver is a strongly typed version of Criteria, in Criteria an alias was a string value.

IList cats = sess.CreateCriteria(typeof(Cat))
    .CreateAlias("Kittens", "kt")
    .CreateAlias("Mate", "mt")
    .Add( Expression.EqProperty("kt.Name", "mt.Name") )
    .List();

但是现在它需要将别名分配给一个变量,因此我们只为其创建一个变量:

But now it needs to assign the alias to a variable so we just create one for it:

Cat catAlias = null;
Kitten kittenAlias = null;

IQueryOver<Cat,Cat> catQuery =
    session.QueryOver<Cat>(() => catAlias)
        .JoinAlias(() => catAlias.Kittens, () => kittenAlias)
        .Where(() => catAlias.Age > 5)
        .And(() => kittenAlias.Name == "Tiddles");

从NHForge文档中,它表示以下内容:

From NHForge documentation, it says the following:

http://nhibernate.info/doc/nh/en/index.html#queryqueryover-aliases

15.5.别名

15.5. Aliases

在传统的ICriteria接口中,别名是使用 魔术字符串",但是它们的值不对应于中的名称 对象域.例如,当使用 .CreateAlias("Kitten","kittenAlias"),字符串"kittenAlias"可以 与域中的属性或类不对应.

In the traditional ICriteria interface aliases are assigned using 'magic strings', however their value does not correspond to a name in the object domain. For example, when an alias is assigned using .CreateAlias("Kitten", "kittenAlias"), the string "kittenAlias" does not correspond to a property or class in the domain.

在QueryOver中,别名使用空变量分配.这 变量可以在任何地方声明(但在运行时应为null).这 然后,编译器可以针对使用的变量检查语法 正确,但是在运行时不会评估变量(只是 用作别名的占位符).

In QueryOver, aliases are assigned using an empty variable. The variable can be declared anywhere (but should be null at runtime). The compiler can then check the syntax against the variable is used correctly, but at runtime the variable is not evaluated (it's just used as a placeholder for the alias).

QueryOver中的每个Lambda表达式函数都有一个对应的 重载以允许使用别名,并使用.JoinAlias函数 使用别名遍历关联,而无需创建子QueryOver.

Each Lambda Expression function in QueryOver has a corresponding overload to allow use of aliases, and a .JoinAlias function to traverse associations using aliases without creating a sub-QueryOver.

因此请坚持只使用方法范围内的变量.

So stick to just using a variable in the scope of the method.

这篇关于什么可以用作NHibernate QueryOver别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆