I ended up doing it this way...
My code implementation
public static bool IsDescendantOf(this string childHierarchy, string parentHierarchy)
{
//In most cases this will be translated to the SQL implementation by LINQ, but on the off chance it's used on an in memory collection, the simplest implementation
//Is to verify that the child hierarchy starts with the hierarchy of the parent.
//for example....
// "/11/534/2134/".StartsWith("/11/534/") //would be TRUE
return childHierarchy.StartsWith(parentHierarchy);
}
The HqlGenerator
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.BooleanMethodCall("_IsDescendantOf", new[] { visitor.Visit(arguments[0]).AsExpression(), visitor.Visit(arguments[1]).AsExpression() });
}
And then where you configure nHibernate I have this line
cfg.AddSqlFunction("_IsDescendantOf", new NHibernate.Dialect.Function.SQLFunctionTemplate(NHibernate.NHibernateUtil.Boolean, "?1.IsDescendantOf(?2) = 1"));
Where cfg
is an instance of your NHibernate.Cfg.Configuration
class