NHibernate、表达式树和消除重复

NHibernate, expression trees, and eliminating repetition

我们已经围绕我们的 NHibernate 持久层实施了一个安全层,希望能够防止用户在不应该访问的情况下甚至无法从数据库接收返回的对象。该安全层如下所示:

public static IQueryable<T> Secure<T>(this Queryable<T> query){
    //if T does not implement ISecurable, then return query
    //else
        return query.Where(expressionFactory.GetExpression(securityKey));
}

我们基本上通过用调用 ISession.Query().Secure().

的装饰器包装它来限制对 ISession 的访问

所以我们有很多类型 return 和 Expression<Func<T, bool>>,这样我们就可以将它传递给 Where():

public class DocumentSecurityExpressionFactory : ISecurityExpressionFactory<Document> {
    public Expression<Func<Document, bool>> GetExpression(SecurityKey key) {
        return doc => doc.MasterDocument.Compartments.Where(c => c.AssociatedCompartment.Type != ProgramTypes.AccessGroup) //Look at non-access group compartments for access
                        .All(c => key.Compartments.Contains(c.AssociatedCompartment.ID))
                    && (
            //person has to be either NTK 
                        doc.MasterDocument.NeedToKnowAccessList.Count() == 0
                        || doc.MasterDocument.NeedToKnowAccessList.Any(p => p.PersonID == key.PersonID)
                        || doc.MasterDocument.NeedToKnowAccessList.Any(p => key.AccessGroups.Contains(p.CompartmentID))
                        );
    }
}

public class DocumentSummarySecurityExpressionFactory : ISecurityExpressionFactory<DocumentSummary> {
    public Expression<Func<DocumentSummary, bool>> GetExpression(SecurityKey key) {
        return doc => doc.MasterDocument.Compartments.Where(c => c.AssociatedCompartment.Type != ProgramTypes.AccessGroup)
                        .All(c => key.Compartments.Contains(c.AssociatedCompartment.ID))
                        && (
                          doc.MasterDocument.NeedToKnowAccessList.Count() == 0
                        || doc.MasterDocument.NeedToKnowAccessList.Any(p => p.PersonID == key.PersonID)
                        || doc.MasterDocument.NeedToKnowAccessList.Any(p => key.AccessGroups.Contains(p.CompartmentID))
                           );
    }
}

public class LatestDocumentVersionSecurityExpressionFactory : ISecurityExpressionFactory<LatestDocumentVersion> {
    public Expression<Func<LatestDocumentVersion, bool>> GetExpression(SecurityKey key) {
        return version => version.BaseDocument.MasterDocument.Compartments.Where(c => c.AssociatedCompartment.Type != ProgramTypes.AccessGroup)
                            .All(c => key.Compartments.Contains(c.AssociatedCompartment.ID))
                            && (
                                version.BaseDocument.MasterDocument.NeedToKnowAccessList.Count() == 0
                                || version.BaseDocument.MasterDocument.NeedToKnowAccessList.Any(p => p.PersonID == key.PersonID)
                                || version.BaseDocument.MasterDocument.NeedToKnowAccessList.Any(p => key.AccessGroups.Contains(p.CompartmentID))
                            );
    }
}

实际上还有几个看起来像这样的不同类型。

这里的问题应该很清楚了:我们每个做这件事的实体本质上都是一样的。它们每个都有对 MasterDocument 对象的引用,所有逻辑都在该对象上完成。重复这段代码完全糟透了(而且它们都在一个文件中,所以如果他们这样做的话,它们可以一起改变)。

我觉得我应该 能够告诉一个方法如何从类型 T 获取 MasterDocument,然后有一个构建表达式的通用方法。像这样:

public static class ExpressionFactory {
    public static Expression<Func<T, bool>> Get<T>(Expression<Func<T, MasterDocument>> mdSource, SecurityKey key) {
        return t => {
             var md = mdSource.Compile()(t);
             return md.Compartments.Where(c => c.AssociatedCompartment)...
        };
    }
}

然后这样称呼它:

public class DocumentSecurityExpressionFactory : ISecurityExpressionFactory<Document> {
    public Expression<Func<Document, bool>> GetExpression(SecurityKey key) {
        return ExpressionFactory.Get<Document>(doc => doc.MasterDocument, key);
    }
}

现在,我明白为什么这段代码不起作用了。我想不通的是如何正确地构建这个表达式树以极大地简化我们的代码。我想我可以像这样传递 Expression<Func<T, MasterDocument>> mdSource 然后使用表达式 API 来构建它,使用 MemberAccessExpressions 等,但我预料到会看起来像一团糟,但我不是确定什么是较小的邪恶。

非常感谢任何帮助。

你可以做的是使用 Compose 可以将一个表达式与另一个表达式组合的方法:

public static Expression<Func<TFirstParam, TResult>>
    Compose<TFirstParam, TIntermediate, TResult>(
    this Expression<Func<TFirstParam, TIntermediate>> first,
    Expression<Func<TIntermediate, TResult>> second)
{
    var param = Expression.Parameter(typeof(TFirstParam), "param");

    var newFirst = first.Body.Replace(first.Parameters[0], param);
    var newSecond = second.Body.Replace(second.Parameters[0], newFirst);

    return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}

它使用以下方法将一个表达式的所有实例替换为另一个:

public static Expression Replace(this Expression expression,
    Expression searchEx, Expression replaceEx)
{
    return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
    private readonly Expression from, to;
    public ReplaceVisitor(Expression from, Expression to)
    {
        this.from = from;
        this.to = to;
    }
    public override Expression Visit(Expression node)
    {
        return node == from ? to : base.Visit(node);
    }
}

现在你可以写:

public static class ExpressionFactory 
{
    public static Expression<Func<T, bool>> Get<T>(
         Expression<Func<T, MasterDocument>> mdSource, SecurityKey key) 
    {
        return mdSource.Compose(document => 
            document.Compartments.Where(c => c.AssociatedCompartment.Type != ProgramTypes.AccessGroup)
                    .All(c => key.Compartments.Contains(c.AssociatedCompartment.ID))
                    && (
                      doc.MasterDocument.NeedToKnowAccessList.Count() == 0
                    || doc.MasterDocument.NeedToKnowAccessList.Any(p => p.PersonID == key.PersonID)
                    || doc.MasterDocument.NeedToKnowAccessList.Any(p => key.AccessGroups.Contains(p.CompartmentID))
                       );

    }
}