加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用实体框架并使用orderby和skip/take的规范模式

发布时间:2020-12-15 18:36:13 所属栏目:百科 来源:网络整理
导读:我已经选择了一个使用规范模式的项目,这是我之前没有使用的模式,我不得不去研究模式.我注意到它没有orderby和skip / take功能,我找不到任何显示如何用模式实现它的地方. 我正在努力思考如何将其添加到规范模式中.但是我遇到过问题,例如规范处理“Expression
我已经选择了一个使用规范模式的项目,这是我之前没有使用的模式,我不得不去研究模式.我注意到它没有orderby和skip / take功能,我找不到任何显示如何用模式实现它的地方.

我正在努力思考如何将其添加到规范模式中.但是我遇到过问题,例如规范处理“Expression>”而我认为我不能将它与orderby等一起存储

基本上有一个这样的类:

public class Specification<T> : ISpecification<T>
{
    public Expression<Func<T,bool>> Predicate { get; protected set; }

    public Specification(Expression<Func<T,bool>> predicate)
    {
        Predicate = predicate;
    }

    public Specification<T> And(Specification<T> specification)
    {
        return new Specification<T>(this.Predicate.And(specification.Predicate));
    }

    public Specification<T> And(Expression<Func<T,bool>> predicate)
    {
        return new Specification<T>(this.Predicate.And(predicate));
    }

    public Specification<T> Or(Specification<T> specification)
    {
        return new Specification<T>(this.Predicate.Or(specification.Predicate));
    }

    public Specification<T> Or(Expression<Func<T,bool>> predicate)
    {
        return new Specification<T>(this.Predicate.Or(predicate));
    }

    public T SatisfyingItemFrom(IQueryable<T> query)
    {
        return query.Where(Predicate).SingleOrDefault();
    }

    public IQueryable<T> SatisfyingItemsFrom(IQueryable<T> query)
    {
        return query.Where(Predicate);
    }
}

这允许创建一个规范,传入一个where子句.它还允许使用“And”,“Or”链接规则.例如:

var spec = new Specification<Wave>(w => w.Id == "1").And(w => w.WaveStartSentOn > DateTime.Now);

如何为“OrderBy”和“Take”添加方法?

由于这是现有代码,我不能做任何会影响现有代码的更改,重构它将是一项非常重要的工作.所以任何解决方案都需要很好地发挥作用.

解决方法

怎么样
public class Specification<T> : ISpecification<T>
{
    public Expression<Func<T,bool>> Predicate { get; protected set; }
    public Func<IQueryable<T>,IOrderedQueryable<T>> Sort {get; protected set; }
    public Func<IQueryable<T>,IQueryable<T>> PostProcess {get; protected set;

    public Specification<T> OrderBy<TProperty>(Expression<Func<T,TProperty>> property)
    {
        var newSpecification = new Specification<T>(Predicate) { PostProcess = PostProcess } ;
        if(Sort != null) {
             newSpecification.Sort = items => Sort(items).ThenBy(property);
        } else {
             newSpecification.Sort = items => items.OrderBy(property);
        }
        return newSpecification;
    }

    public Specification<T> Take(int amount)
    {
        var newSpecification = new Specification<T>(Predicate) { Sort = Sort } ;
        if(PostProcess!= null) {
             newSpecification.PostProcess= items => PostProcess(items).Take(amount);
        } else {
             newSpecification.PostProcess= items => items.Take(amount);
        }
        return newSpecification;
    }

    public Specification<T> Skip(int amount)
    {
        var newSpecification = new Specification<T>(Predicate) { Sort = Sort } ;
        if(PostProcess!= null) {
             newSpecification.PostProcess= items => PostProcess(items).Skip(amount);
        } else {
             newSpecification.PostProcess= items => items.Skip(amount);
        }
        return newSpecification;
    }
}

去做:

> OrderByDescending的类似结构
>更新您的其他方法,以便在调用“And”时不会丢失“Sort”值和“PostProcess”值,例如

然后你满意的方法变成:

private IQueryable<T> Prepare(IQueryable<T> query) 
{
    var filtered = query.Where(Predicate);
    var sorted = Sort(filtered);
    var postProcessed = PostProcess(sorted);
    return postProcessed;
}

public T SatisfyingItemFrom(IQueryable<T> query)
{
    return Prepare(query).SingleOrDefault();
}

public IQueryable<T> SatisfyingItemsFrom(IQueryable<T> query)
{
    return Prepare(query);
}

TODO:检查Sort& “准备”方法中的PostProcess不为空

用法:

var spec = new Specification<Wave>(w => w.Id == "1")
              .And(w => w.WaveStartSentOn > DateTime.Now)
              .OrderBy(w => w.WaveStartSentOn)
              .Skip(20)
              .Take(5);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读