title: Extending linq-to-nhibernate with custom hql generator
body: <p>
  The goal is to do something like this using NHibernate and Linq:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Session.Query&lt;MyClass&gt;().Where(x =&gt; x.DateGreaterThen(DateTime.Now))
  </code></pre>
  
  <p>This example is a little bit simplified, but the idea is that in Where predicate I would like to call MyClass method. So MyClass looks like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyClass
  {
      public static readonly Expression&lt;Func&lt;MyClass, DateTime, bool&gt;&gt; GreaterThenExpression =
          (x, dt) =&gt; x.MyDateTimeProperty &gt; dt.Date;
  
      private static readonly Func&lt;MyClass, DateTime, bool&gt; GreaterThenFunc = GreaterThenExpression.Compile();
  
      public Guid Id { get; set; }
  
      public DateTime MyDateTimeProperty { get; set; }
  
      public bool DateGreaterThen(DateTime dt)
      {
          return GreaterThenFunc(this, dt);
      }
  }
  </code></pre>
  
  <p>Custom generator:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class EntityMethodGenerator&lt;T1, T2, TResult&gt; : BaseHqlGeneratorForMethod
  {
      private Expression&lt;Func&lt;T1, T2, TResult&gt;&gt;  _returnValueExpression;
  
      public EntityMethodGenerator()
      {
          SupportedMethods = new[]
                                 {
                                     ReflectionHelper.GetMethodDefinition&lt;MyClass&gt;(myClass =&gt; myClass.DateGreaterThen(DateTime.Now))
                                 };
      }
  
      public static void Register(ILinqToHqlGeneratorsRegistry registry, Expression&lt;Action&lt;T1&gt;&gt; method, Expression&lt;Func&lt;T1, T2, TResult&gt;&gt; returnValueExpression)
      {
          var generator = new EntityMethodGenerator&lt;T1, T2, TResult&gt; { _returnValueExpression = returnValueExpression };
  
          registry.RegisterGenerator(ReflectionHelper.GetMethodDefinition(method), generator);
      }
  
      public override HqlTreeNode BuildHql(
          MethodInfo method,
          Expression targetObject,
          ReadOnlyCollection&lt;Expression&gt; arguments,
          HqlTreeBuilder treeBuilder,
          IHqlExpressionVisitor visitor)
      {
          return visitor.Visit(_returnValueExpression);
      }
  }
  </code></pre>
  
  <p>And finally custom custom generators registry:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class OwnLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
  {
      public OwnLinqToHqlGeneratorsRegistry()
      {
          EntityMethodGenerator&lt;MyClass, DateTime, bool&gt;.Register(this, (myClass) =&gt; myClass.DateGreaterThen(DateTime.Now), MyClass.GreaterThenExpression);
      }   
  }
  </code></pre>
  
  <p>But this is not working, I am getting <code>System.Data.SqlClient.SqlException : Invalid column name 'dt'.</code> I am suspecting that my BuildHql method is not implemented correctly, any help with fixing this?</p>
  
  <p>By the way, I would like to use expressions in generator, like my <code>GreaterThenExpression</code> rather than building HqlTree manually in <code>BuildHql</code> method</p>