title: NHibernate and runtime expression
body: <p>I write access permission checker for entity obtain from db.
  I have declared base rule class:</p>
  
  <pre><code>abstract class AccessRule
  {
      private Func&lt;object, Entities.User, bool&gt; _canViewCompiledExpression;
  
      public abstract Expression&lt;Func&lt;object, Entities.User, bool&gt;&gt; RawCanEditExpression { get; }
      public abstract Type ObjectType { get; }
      public Expression&lt;Func&lt;object, bool&gt;&gt; CreateCanViewExpression(AccessContext accessContext, Entities.User user)
      {
          var entityArgument = Expression.Parameter(typeof(object), "arg1");
          var userExpression = Expression.Constant(user, typeof(Entities.User));
          var body = Expression.Invoke(RawCanViewExpression, entityArgument, userExpression);
  
          return (Expression&lt;Func&lt;object, bool&gt;&gt;)Expression.Lambda(body, entityArgument);
      }
     }
  
    abstract class AccessRule&lt;T&gt; : AccessRule
      where T : Entity
    {
      public override Type ObjectType =&gt; typeof(T);
  
      public abstract Expression&lt;Func&lt;T, Entities.User, bool&gt;&gt; CanViewExpression { get; }
      public override Expression&lt;Func&lt;object, Entities.User, bool&gt;&gt; RawCanViewExpression =&gt; ExpressionHelper.BoxExpression(CanViewExpression);    
     }
  </code></pre>
  
  <p>And this is helper for convert Expression> to Expression>:</p>
  
  <pre><code>public static Expression&lt;Func&lt;object, Entities.User, bool&gt;&gt; BoxExpression&lt;T&gt;(Expression&lt;Func&lt;T, Entities.User, bool&gt;&gt; expression)
  {
      var objParameter = Expression.Parameter(typeof(object), "arg1");
      var userParameter = Expression.Parameter(typeof(Entities.User), "arg2");
  
      var convertExpression = Expression.Convert(objParameter, typeof(T));
  
      var body = Expression.Invoke(expression, convertExpression, userParameter);
      var result = Expression.Lambda&lt;Func&lt;object, Entities.User, bool&gt;&gt;(body, objParameter, userParameter);
  
      return result;
  }
  </code></pre>
  
  <p>now, for example one rule, for one item:</p>
  
  <pre><code>public override Expression&lt;Func&lt;Entities.Order, Entities.User, bool&gt;&gt; CanViewExpression
          =&gt; (obj, user) =&gt; obj.Owner == user;
  </code></pre>
  
  <p>now I apply my expression (wrapped to specification class) for query to DB. I extract all conditions from expression (such Expression>, Expression>, MethodCallExpression and UnaryExpression) based on this code <a href="https://github.com/rjperes/DevelopmentWithADot.NHibernateSpecifications/blob/master/DevelopmentWithADot.NHibernateSpecifications/SpecificationExtensions.cs" rel="nofollow noreferrer">https://github.com/rjperes/DevelopmentWithADot.NHibernateSpecifications/blob/master/DevelopmentWithADot.NHibernateSpecifications/SpecificationExtensions.cs</a></p>
  
  <p>and apply this to Query&lt;> method.</p>
  
  <p>I check result expression by compile it to Func and invoke on each items on database. all ok. 
  but then i execute query on real connection i catch NullReferenceException in NHibertante core code.</p>
  
  <p>after investigation i found that exception generated in NHibernate.Linq.Visitors.ExpressionKeyVisitor.VisitConstantExpression method because _constantToParameterMap is null but used without any checks:</p>
  
  <pre><code>protected override Expression VisitConstantExpression(ConstantExpression expression)
  {
      NamedParameter param;
  
      if (_constantToParameterMap.TryGetValue(expression, out param) &amp;&amp; insideSelectClause == false)
  </code></pre>
  
  <p>null passed from NHibernate.Linq.Visitors.WhereJoinDetectors.VisitMemberExpression method</p>
  
  <pre><code>var key = ExpressionKeyVisitor.Visit(expression, null);
  </code></pre>
  
  <p>I used NHIbernate 4.1.1</p>
  
  <p>can anybody help with this problem?</p>
  
  <p><strong>UPDATE 1</strong>
  this is debug view of result expression:</p>
  
  <pre><code>.Lambda #Lambda1&lt;System.Func`2[System.Object,System.Boolean]&gt;(System.Object $arg1) {
      .Invoke (.Lambda #Lambda2&lt;System.Func`3[System.Object,Project.Entities.User,System.Boolean]&gt;)(
          $arg1,
          .Constant&lt;Project.Entities.User&gt;(Project.Entities.User))
  }
  
  .Lambda #Lambda2&lt;System.Func`3[System.Object,Project.Entities.User,System.Boolean]&gt;(
      System.Object $arg1,
      Project.Entities.User $arg2) {
      .Invoke (.Lambda #Lambda3&lt;System.Func`3[Project.Entities.Order,Project.Entities.User,System.Boolean]&gt;)(
          (Project.Entities.Windfarm)$arg1,
          $arg2)
  }
  
  .Lambda #Lambda3&lt;System.Func`3[Project.Entities.Order,Project.Entities.User,System.Boolean]&gt;(
      Portal.Domain.Entities.Windfarm $obj,
      Portal.Domain.Entities.User $user) {
      .Call($obj.Owner).Equeal($user)
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong>
  after some experiments i found that error occurred only if i try access user inside rule... i.e if i replace check with expression, which does not contains access to user all work fine. so problem is in constant expression, but i can't understand why and how to solve it</p>