[英]Calling Service With Pageable Parameter - Best Practice Question
I have a service MyMainService
that makes a call to another service MySecondaryService
within the same package. 我有一个服务MyMainService
,该服务调用同一包中的另一个服务MySecondaryService
。 The method getPagesOfMyObjects
being called in MySecondaryService
returns a Page of objects of type MyObject
and one of the method's parameters is a Pageable
object. 该方法getPagesOfMyObjects
被称为在MySecondaryService
返回类型的对象的页面MyObject
,并且该方法的参数中的一个是Pageable
对象。 MyMainService
is, at present, not concerned with pagination. 目前, MyMainService
与分页无关。 But because I need to call the getPagesOfMyObjects
method in MySecondaryService
I need to provide it with a Pageable
object that won't exclude any records that I might need. 但是,由于我需要在getPagesOfMyObjects
中调用getPagesOfMyObjects
方法, MySecondaryService
需要为其提供一个Pageable
对象,该对象不会排除我可能需要的任何记录。 My current solution (not very good) is to pass this as the Pageable
parameter 我当前的解决方案(不是很好)是将其作为Pageable
参数传递
public class MyMainServiceImpl implements MyMainService {
@Autowired
private MySecondaryService mySecondaryService
public void myMethod() {
Page<MyObject> pages = mySecondaryService.getPagesOfMyObjects(PageRequest.of(0, 10000));
List<MyObject> myObjects = pages.getContent();
//do stuff
}
}
As you can see it has 10000 hard-coded as its size parameter. 如您所见,它的大小参数已硬编码为10000。 This doesn't feel like good practice to me. 对我来说,这不是好习惯。 Is there a better way to call this service? 有没有更好的方法来致电此服务? I don't have the option of re-writing the existing getPagesOfMyObjects
method. 我没有选择重写现有的getPagesOfMyObjects
方法。
最后,我使用Pageable.unpaged()
作为参数来代替PageRequest.of(0, 10000)
,它似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.