将表达式转换为另一个? [英] Converting expression to another?
问题描述
我有一个小挑战,我不知道该如何解决。
我需要从此 Expression< Func< TEntity,URequest,bool>>
转换为此 Expression< Func< TEntity,bool>>
。
第二个将用于查询数据源。
这个想法是让它具有一个带有以下签名的基类构造函数
抽象类Base
{
Base(Expression< Func< TEntity,TRequest,bool>>表达式){....}
}
我可以在派生类中提供这样的表达式
class Derived:Base
{
Derived():
base((enttiy,request)=> entity.SomeProp == request.SomePropValue){}
}
基地以此方式使用
/// Ctor
Base(Expression< Func< TEntity,TRequest,bool>表达式)
{
_pipelineBuilder.AddFetchEntityStep(表达式)
}
AddFetchEntityStep是管道中的步骤
管道如下所示。
步骤1:FetchEntity
步骤2:执行
步骤3:验证
步骤3 :已执行
现在,有两种方法可以通过ID或其他属性来检索实体。
为此,我有
ReadOnlyRepository
{
TEntity GetById< TEntity>(对象ID){....}
TEntity GetBy< TEntity>(Expression< Func< TEntity,bool>"表达式){....}
在AddFetchEntityStep中,我进行了一次检查以了解要调用什么,类似于
public void AddFetchEntityStep(表达式< Func< TEntity,URequest,bool>"表达式= null)
{
if(expression == null)
{
_repo.GetById< TEntity>(1)
}
否则
{
_repo.GetBy(。 ....)////问题
}
}
我知道它们是两个不同的签名
Expression< Func< TEntity,bool>>
和 Expression< Func< TEntity,TRequest,bool> ;>
您不能简化
只要使用lambda表达式的调用者未提供request参数,请考虑使用 Func< URequest,Expression< Func< TEntity,bool>>>
而不是 Expression< Func< TEntity,URequest,bool>>
。
lambda表达式看起来像这样
(请求) => (完整)=> entity.SomeProp == request.SomePropValue
。
然后,您的基类可以调用它来将 request对象绑定到表达式。 / p>
顺便说一句,前缀不是 T
的通用参数名称是反模式。
I have a little challenge which I don't know how to solve.
I need to convert from this Expression<Func<TEntity, URequest, bool>>
to this Expression<Func<TEntity, bool>>
.
The 2nd is going to be used to query a data source.
The idea is this having a base class constructor with the following signature
abstract class Base
{
Base(Expression<Func<TEntity, TRequest, bool>> expression) { .... }
}
I can provide an expression in the derived something like this
class Derived : Base
{
Derived() :
base ((enttiy, request) => entity.SomeProp == request.SomePropValue) { }
}
This is used by the Base as such
/// Ctor
Base(Expression<Func<TEntity, TRequest, bool>> expression)
{
_pipelineBuilder.AddFetchEntityStep (expression)
}
Where AddFetchEntityStep is a step in a pipeline
The pipeline looks like this.
Step 1 : FetchEntity Step 2 : Executing Step 3 : Validate Step 3 : Executed
Now there are two ways of retrieving an entity by Id or by another property.
For this I have a
ReadOnlyRepository
{
TEntity GetById<TEntity>(object id) { .... }
TEntity GetBy<TEntity>(Expression<Func<TEntity, bool>> expression) { .... }
In the AddFetchEntityStep I do a check in order to know what Get to invoke, something like
public void AddFetchEntityStep (Expression<Func<TEntity, URequest, bool>> expression = null)
{
if (expression == null )
{
_repo.GetById<TEntity>(1)
}
else
{
_repo.GetBy(.....) //// The problem
}
}
I know they are two different signatures
Expression<Func<TEntity,bool>>
and Expression<Func<TEntity, TRequest, bool>>
You cannot simply use lambda expression to convert an expression tree to another.
As long as the request parameter is not provided by the caller that specifies lambda expression , consider use Func<URequest,Expression<Func<TEntity,bool>>>
instead of Expression<Func<TEntity, URequest, bool>>
.
And the lambda expression would look like this
(request) => (enttiy) => entity.SomeProp == request.SomePropValue
.
Then your base class could call it to bind a 'request' object to the expression.
Btw, generic parameter names with prefix other than T
is antipattern.
这篇关于将表达式转换为另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!