指定 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<Country> getAll();
}
进一步假设我正在抽象这个 DAO,因为我可能提供 2 种实现,一种用于数据库,一种用于 Web 服务。
我想重载 getAll 方法以接受某种排序参数来指示返回值应如何排序。
然而,我不想将接口与特定的实现联系起来。例如,数据库实现将使用 ORDER BY 子句,并且需要数据库列的列表和顺序方向(例如“ASC”或“DESC”),而 Web 服务实现则不需要。
提供此类参数而不将调用者耦合到特定实现的最佳实践是什么?
编辑
对我的问题的一点澄清。我不仅想指定一个参数来指示订单方向,还想指定要订购的内容。
例如,假设我的 Country 模型定义如下:
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;
}
我希望返回的值按 name 升序排列。我可以按照建议使用 ENUM 作为订购方向,但是在不暴露实现细节的情况下指定要订购的属性的最佳实践是什么?
Suppose I have the following DAO interface:
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<Country> getAll();
}
Further suppose that I am abstracting this DAO because I might provide 2 implementations, one for a database and one for a web service.
I want to overload the getAll method to accept some sort of ordering parameter to indicate how the returned value should be ordered.
I don't want to tie the interface to a specific implementation however. For example, a database implementation would use an ORDER BY clause and would need a list of database columns and an order direction such as "ASC" or "DESC" where is a web service implementation will not.
What's the best practice to provide such a parameter without coupling the caller to a specific implementation?
EDIT
Small clarification to my question. I don't just want to specify a parameter to indicate the order direction, but also what to order on.
For example, suppose my Country model was defined as follows:
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;
}
And I want to the returned value to be ordered by name in ascending order. I could use an ENUM, as suggested, for the order direction, but what would be the best practive to specify the property to order on without exposing implementation specifics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要么是布尔值:
要么是枚举:
实际上实现这不是接口的工作。只需确保接口接受的任何输入都可以由您的任何一个类处理。
Either a boolean:
Or an enum:
Actually implementing this isn't the job of the interface. Just make sure any inputs the interface accepts can be handled by either of your classes.