规范模式 - 使用 lambda 创建复合规范 (C#)

发布于 2024-09-15 05:38:20 字数 230 浏览 3 评论 0原文

如果我有一个定义为表达式的规范,如下所示:

public Expression<Func<Foo, bool>> IsSuperhuman = 
  x => x.CanFly && x.HasXRayVision;

并且我想定义另一个规范“IsSuperheroine”,逻辑为“是超人并且是女性”,我如何在新规范中重用现有规范?

If I have a specification defined as an Expression as below:

public Expression<Func<Foo, bool>> IsSuperhuman = 
  x => x.CanFly && x.HasXRayVision;

And I want to define another specification 'IsSuperheroine' with the logic 'is superhuman and is female', how can I reuse the existing specification within the new one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-09-22 05:38:20

您是否检查过 LinqKit 中的 谓词构建器?它通过将 you 和 和 or 表达式放在一起来构建表达式。

Have you checked out predicate builder in LinqKit? It builds up expressions by letting you and and or expressions together.

烟若柳尘 2024-09-22 05:38:20

这是一种方法:

Expression<Func<Foo, bool>> IsSuperhuman = x => x.CanFly && x.HasXRayVision;

Expression<Func<Foo, bool>> IsSuperheroine = AndAlso(IsSuperhuman, x => x.IsFemale);

...

public static Expression<Func<T, TResult>> AndAlso<T, TResult>(Expression<Func<T, TResult>> expr1, Expression<Func<T, TResult>> expr2)
{
    var arg = Expression.Parameter(typeof(T), expr1.Parameters[0].Name);
    var andExpr = Expression.AndAlso(
        ReplaceParameter(expr1.Body, expr1.Parameters[0], arg),
        ReplaceParameter(expr2.Body, expr2.Parameters[0], arg));
    return Expression.Lambda<Func<T, TResult>>(andExpr, arg);
}

public static Expression ReplaceParameter(Expression expr, ParameterExpression oldParam, ParameterExpression newParam)
{
    return new ReplaceParameterVisitor(oldParam, newParam).Visit(expr);
}

internal class ReplaceParameterVisitor : ExpressionVisitor
{
    private ParameterExpression _oldParam;
    private ParameterExpression _newParam;

    public ReplaceParameterVisitor(ParameterExpression oldParam, ParameterExpression newParam)
    {
        _oldParam = oldParam;
        _newParam = newParam;
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node == _oldParam)
            return _newParam;
        return node;
    }
}

这可能不是最简单的方法,但它有效......

Here's a way to do it :

Expression<Func<Foo, bool>> IsSuperhuman = x => x.CanFly && x.HasXRayVision;

Expression<Func<Foo, bool>> IsSuperheroine = AndAlso(IsSuperhuman, x => x.IsFemale);

...

public static Expression<Func<T, TResult>> AndAlso<T, TResult>(Expression<Func<T, TResult>> expr1, Expression<Func<T, TResult>> expr2)
{
    var arg = Expression.Parameter(typeof(T), expr1.Parameters[0].Name);
    var andExpr = Expression.AndAlso(
        ReplaceParameter(expr1.Body, expr1.Parameters[0], arg),
        ReplaceParameter(expr2.Body, expr2.Parameters[0], arg));
    return Expression.Lambda<Func<T, TResult>>(andExpr, arg);
}

public static Expression ReplaceParameter(Expression expr, ParameterExpression oldParam, ParameterExpression newParam)
{
    return new ReplaceParameterVisitor(oldParam, newParam).Visit(expr);
}

internal class ReplaceParameterVisitor : ExpressionVisitor
{
    private ParameterExpression _oldParam;
    private ParameterExpression _newParam;

    public ReplaceParameterVisitor(ParameterExpression oldParam, ParameterExpression newParam)
    {
        _oldParam = oldParam;
        _newParam = newParam;
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node == _oldParam)
            return _newParam;
        return node;
    }
}

It is probably not the simplest way to do it, but it works...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文