问题描述
我使用库,可以即时组合表达式。
这是写入Entity Framewok数据访问层的纯粹的幸福,因为可以重复使用和组合多个表达式,这样既可读又高效的代码。
考虑下面的代码:
private static readonly Expression< Func< Message, int,MessageView>> _selectMessageViewExpr =
(Message msg,int requestedUserId)=>
new MessageView
{
MessageID = msg.ID,
RequestingUserID = requestedUserId,
Body =(msg.RootMessage == null)? msg.Body:msg.RootMessage.Body,
Title =((msg.RootMessage == null)?msg.Title:msg.RootMessage.Title)? string.Empty
};
我们声明一个将 Message
MessageView
(为了清楚起见,我删除了详细信息)。
现在,数据访问代码可以使用此表达式获取单个消息:
var query = CompiledQueryCache.Instance.GetCompiledQuery(
GetMessageView,
()=> CompiledQuery.Compile(
_getMessagesExpr
.Select(msg => _selectMessageViewExpr.Invoke(msg,userId))//重用表达式
.FirstOrDefault((MessageView mv,int id)=> mv.MessageID == id)
.Expand $ b)
);
这是美丽的,因为相同的表达式可以重用于获取消息列表: p>
var query = CompiledQueryCache.Instance.GetCompiledQuery(
GetMessageViewList,
()=> CompiledQuery。编译(
BuildFolderExpr(folder)
.Select(msg => _selectMessageViewExpr.Invoke(msg,userId))
.OrderBy(mv => mv.DateCreated,SortDirection.Descending)
.Paging()
.Expand()
),
文件夹
);
如你所见,投影表达式存储在 _selectMessageViewExpr
并用于构建几个不同的查询。
但是,我花了很多时间跟踪一个奇怪的错误,其中该代码崩溃在 Expand()
调用。
错误说:
只有在一段时间后,我才意识到一切都会在局部变量中引用表达式时被调用在
上调用:
var selector = _selectMessageViewExpr; //引用字段
var query = CompiledQueryCache.Instance.GetCompiledQuery(
GetMessageView,
()=> CompiledQuery.Compile(
_getMessagesExpr
.Select(msg => selector.Invoke(msg,userId))//使用变量
.FirstOrDefault((MessageView mv,int id)=> mv.MessageID == id)
.Expand()
)
);
此代码按预期工作。
我的问题是:
这个问题可能通过查看生成的代码并检查LinqKit源来回答,但是我认为也许有人与LinqKit开发相关可以回答这个问题。 / p>
感谢。
我下载源代码并试图分析。 ExpressionExpander
不允许引用存储在常量之外的变量中的表达式。它期望表达式调用 Invoke
方法引用由 ConstantExpression
表示的对象,而不是另一个 MemberExpression
。
因此,我们无法提供可重用的表达式作为类的任何成员(甚至是公共字段,而不是属性)。嵌套成员访问(如 object.member1.member2
...等)也不受支持。
但这可以通过遍历初始表达式并重新提取子字段值来修复。
c $ c> TransformExpr 方法代码 ExpressionExpander
类到
code> var lambda = Expression.Lambda(input);
object value = lambda.Compile()。DynamicInvoke();
if(value is Expression)
return Visit((Expression)value);
else
返回输入;
现在可以使用。
这个解决方案我之前提到的(递归遍历树)是由 ExpressionTree
编译器:
I'm using LinqKit library which allows combining expressions on the fly.
This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and efficient code.
Consider following piece of code:
private static readonly Expression<Func<Message, int, MessageView>> _selectMessageViewExpr =
( Message msg, int requestingUserId ) =>
new MessageView
{
MessageID = msg.ID,
RequestingUserID = requestingUserId,
Body = ( msg.RootMessage == null ) ? msg.Body : msg.RootMessage.Body,
Title = ( ( msg.RootMessage == null ) ? msg.Title : msg.RootMessage.Title ) ?? string.Empty
};
We declare an expression that projects Message
onto MessageView
(I removed the details for clarity).
Now, the data access code can use this expression to get individual message:
var query = CompiledQueryCache.Instance.GetCompiledQuery(
"GetMessageView",
() => CompiledQuery.Compile(
_getMessagesExpr
.Select( msg => _selectMessageViewExpr.Invoke( msg, userId ) ) // re-use the expression
.FirstOrDefault( ( MessageView mv, int id ) => mv.MessageID == id )
.Expand()
)
);
This is beautiful because the very same expression can be reused for getting a message list as well:
var query = CompiledQueryCache.Instance.GetCompiledQuery(
"GetMessageViewList",
() => CompiledQuery.Compile(
BuildFolderExpr( folder )
.Select( msg => _selectMessageViewExpr.Invoke( msg, userId ) )
.OrderBy( mv => mv.DateCreated, SortDirection.Descending )
.Paging()
.Expand()
),
folder
);
As you can see, projection expression is stored in _selectMessageViewExpr
and is used for building several different queries.
However, I spent a lot of time tracing a strange error where this code crashed at Expand()
call.
The error said:
It's only after a while that I realized that everything works when expression is referenced in a local variable before being called Invoke
on:
var selector = _selectMessageViewExpr; // reference the field
var query = CompiledQueryCache.Instance.GetCompiledQuery(
"GetMessageView",
() => CompiledQuery.Compile(
_getMessagesExpr
.Select( msg => selector.Invoke( msg, userId ) ) // use the variable
.FirstOrDefault( ( MessageView mv, int id ) => mv.MessageID == id )
.Expand()
)
);
This code works as expected.
My question is:
This question can probably be answered by looking at generated code and checking LinqKit sources, however I thought maybe someone related to LinqKit development could answer this question.
Thanks.
I downloaded sourcecode and tried to analyse it. ExpressionExpander
does not allow to reference expressions that are stored in variables other than constant. It expects expression that Invoke
method is being called upon to reference to object represented by ConstantExpression
, not another MemberExpression
.
So we cannot provide our reusable expression as reference to any member of the class (even public fields, not properties). Nesting member access (like object.member1.member2
... etc) is not supported too.
But this can be fixed by traversing initial expression and recusrsively extracting subfields values.
I have replaced TransformExpr
method code of ExpressionExpander
class to
var lambda = Expression.Lambda(input);
object value = lambda.Compile().DynamicInvoke();
if (value is Expression)
return Visit((Expression)value);
else
return input;
and it works now.
In this solution everything I mentioned before (recursively traversing tree) is done for us by ExpressionTree
compiler :)
这篇关于有什么特别的原因LinqKit的扩展器不能从字段中获取表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!