过滤存储库或服务中的对象? [英] Filtering of objects in repository or service?

查看:141
本文介绍了过滤存储库或服务中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据一些参数获取对象列表。该对象是否属于特定类别,它是否具有属性x y z等。我创建了一个相当简单的存储库,通过id获取对象。如果我想要一个基于不同参数和标准的对象列表,你会在存储库中添加一个方法,或者在服务层中有一个方法继续运行,直到它有一个符合标准的对象列表?它只是使用存储库get by id方法并针对许多标准进行检查?

I want to get a list of object based on some parameters. Does the object belong to a particular category, does it have property x y z etc. I have created a fairly simple repository that get objects by id. If I want a list of objects based on different parameters and criterias, would you add a method in the repository that does that or have a method in the service layer that keeps running until it has a list of objects that fits the criterias? It would simply use the repository get by id method and check it against many criterias?

推荐答案

以下策略基于project也使用域对象作为持久对象。

The below strategy is based on that the project uses domain objects as persistent objects also.

如果查询仅供用户搜索(例如,搜索ui的命令),我想调用存储库直接在接口组件中。

If the query is just for user searching(for example, an order searching ui), I'd like to invoke the repository directly in the interfaces component.

@Controller
public class OrderAdminController {
     @RequestMapping(........)
     public String search(@ModelAttribute OrderCriteria criteria) {
          PagedList<Order> orders = orderRepository.findBy(criteria);
          //criteria contains dateWhenPlaced, customerNameLike, orderStatus matching and so on.
          .......
     }
}

如果查询包含域概念,我想使用规范。在这种情况下,SQL filterting最终用于基础结构(为了性能,我们不能在内存中加载所有聚合并按java过滤)但域逻辑不会泄漏(到应用程序层和基础结构)

If the query contains domain concepts, I'd like to use a Specification. In this case, SQL filterting is finally used at infrastructure(for performance, we can't load all aggregates in memory and do filter by java ) but the domain logic does not leak(to application layer and infrastructure)

public class CancelOverdueOrdersBatch {

    public void run() {
        List<Order> overdues = orderRepository.
             findSatisfying(new OverdueOrderSpecification(clock));
        for (Order order: overdues) {
            //.....cancel
        }
    }
}

public class OverdueOrderSpecification {
    private Clock clock;
    private int overdueMinutes = 30;

    public OrderCriteria asCriteria() { //invoked by repository implementation
         return new OrderCriteria.Builder().orderStatusEq(UNPAID).
             placedBefore(overdueMinutesAgo()).build();
    }

    private Date overdueMinutesAgo() {
        return ....//placedDate calculation based on clock.now() and overdueMinutes 
    }
}

这篇关于过滤存储库或服务中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆