NHibernate的QueryOver可扩展性 [英] NHibernate QueryOver extensibility
问题描述
是否有可能通过某种方式延长QueryOver API?我想补充的是FOL
Is it possible to extend QueryOver API by somehow? What I want to add is the fol
var criteria = QueryOver.Of<InternalAssessor>()
.WhereRestrictionOn(x => x.Sector).HasAtLeastOneFlagSet((int)sector)
在哪里部门是位标志枚举。我们有这样的标准的ICriteria API,我可以做
Where sector is bit flag enum. We had such criterion for ICriteria API and I can do
.Where(BitwiseRestrictions.AtLeastOneFlagSet("Sector", (int)sector))
但想拥有这样做的强类型的方式。是否有延长的任何实例QueryOver?
But want to have strongly typed way of doing it. Are there any examples of extending QueryOver?
推荐答案
还有就是,pretty的简单的方法,如何采取 IQueryOver
,搜索它的底层标准的和追加的,请参见 https://gist.github.com/2304623
There is, pretty straightforward way, how to take IQueryOver
, search its Underlying criteria and append one, see https://gist.github.com/2304623
public static IQueryOver<TRoot, TSubType> WhereBitwiseRestriction<TRoot, TSubType>(
this IQueryOver<TRoot, TSubType> query
, Expression<Func<TSubType, object>> expression
, int number)
{
var name = ExpressionProcessor.FindMemberExpression(expression.Body);
query.UnderlyingCriteria.Add
(
BitwiseRestrictions.AtLeastOneFlagSet(name, number)
);
return query;
}
和使用
var criteria = QueryOver.Of<InternalAssessor>()
...
.WhereRestrictionOn(x => x.Name).IsLike(searchedName) // standard
...
.WhereBitwiseRestriction(x => x.Sector, (int)sector) // custom
...
要完全满足您的要求,我们需要引入一些人在这方面的中间人对象,将举行参照查询
和我们的 BitwiseRestrictions
。另一部分将立即执行,追加号
键,返回查询。类似的是做 QueryOverRestrictionBuilder
在NHibernate的...但不是上述的工作和足够简单?的
To fulfil your request completely, we would need to introduce some man-in-the-middle object, which will hold reference to query
and our BitwiseRestrictions
. Another extension will immediately take it, append number
and return query. Similar is doing the QueryOverRestrictionBuilder
in NHibernate... but is not the above working and simple enough?
这篇关于NHibernate的QueryOver可扩展性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!