指定对DAO方法的排序

假设我有以下DAO接口:

public interface CountryData { /** * Get All Countries. * * @return A Collection of Countries. * @throws DataAccessException Thrown if there is an error communicating with the data store. The Exception's * Cause Exception can usually be examined to determine the exact nature of the * error. * @since 1.0.0 */ public List getAll(); } 

进一步假设我正在抽象这个DAO,因为我可能提供2个实现,一个用于数据库,一个用于Web服务。

我想重载getAll方法以接受某种排序参数来指示应该如何排序返回的值。

但是,我不想将接口绑定到特定的实现。 例如,数据库实现将使用ORDER BY子句,并且需要一个数据库列列表和一个订单方向,例如“ASC”或“DESC”,其中Web服务实现不会。

在不将调用者与特定实现耦合的情况下提供此类参数的最佳实践是什么?

编辑

我的问题很少澄清。 我不仅要指定一个参数来指示订单方向,还要指定要订购的内容。

例如,假设我的国家模型定义如下:

 public final class Country implements Serializable { private int id; private String name; public Country() { } public Country(Country countryToCopy) { this.id = countryToCopy.getId(); this.name = countryToCopy.getName(); } public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } 

我希望返回的值按名称按升序排序。 我可以按照建议使用ENUM作为订单方向,但是在不暴露实现细节的情况下指定要订购的属性的最佳方法是什么?

布尔值:

 public List getAll(boolean ascending); 

或者枚举:

 enum SortOrder { ASCENDING, DESCENDING, RANDOM, NONE } public List getAll(SortOrder order); 

实际上,实现这不是界面的工作。 只需确保接口接受的任何输入都可以由您的任何一个类处理。