NHibernate QueryOver Extensibility

Is there any way to extend the QueryOver API? I want to add: fol

var criteria = QueryOver.Of<InternalAssessor>() .WhereRestrictionOn(x => x.Sector).HasAtLeastOneFlagSet((int)sector) 

Where sector is an enumeration of a bit flag. We had such a criterion for the ICriteria API, and I can do

 .Where(BitwiseRestrictions.AtLeastOneFlagSet("Sector", (int)sector)) 

But I want to have a strongly typed way to do this. Are there any examples of the QueryOver extension?

+1
source share
1 answer

There is a fairly simple way to take IQueryOver , find its basic criteria and add 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; } 

And use it

 var criteria = QueryOver.Of<InternalAssessor>() ... .WhereRestrictionOn(x => x.Name).IsLike(searchedName) // standard ... .WhereBitwiseRestriction(x => x.Sector, (int)sector) // custom ... 

In order to fully fulfill your request, we need to enter some “person in the middle” object, which will contain a link to the query and our BitwiseRestrictions . Another extension will immediately accept it, add number and return a request. Does QueryOverRestrictionBuilder in NHibernate do the QueryOverRestrictionBuilder ... but doesn't work above and is simple enough?

+2
source

Source: https://habr.com/ru/post/892910/


All Articles