Can NHibernate linq/lambda expressions be compiled so they aren't reevaluate on every use?
1 Answer
Compiling them (into delegates) would make them execute in memory, which is something you definitely do NOT want.
They must stay as expression trees in order to be parsed into Criteria expressions (2.x contrib provider) or HQL trees (3.x provider), and then into SQL.
7 Comments
Mauricio Scheffer
Why would it make them execute in memory? If all parameters are properly lifted into the delegate, it shouldn't.
Diego Mijelshon
By "compile" I understand calling
Expression<TDelegate>.Compile(). After doing that, you no longer have an expression tree; you have MSIL. And, unless you decompile MSIL (unlikely), you can't transform that into SQL for server-side execution.Mauricio Scheffer
@Diego Mijelshon: yes, you're right. Still, it should be possible in principle to build a pre-compiler for NH linq queries just like System.Data.Linq's CompiledQuery (msdn.microsoft.com/en-us/library/bb548737.aspx) which takes an Expression and returns a delegate. It's just that it's not as trivial as doing
Expression<TDelegate>.Compile().bleevo
I reflected CompiledQuery and it seems to just call Compile()
Mauricio Scheffer
@bleevo: drill down into CompiledQuery.Compile, it's a lot more complex than that.
|