Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecify Ordering to a DAO Method
    primarykey
    data
    text
    <p>Suppose I have the following DAO interface:</p> <pre><code>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&lt;Country&gt; getAll(); } </code></pre> <p>Further suppose that I am abstracting this DAO because I might provide 2 implementations, one for a database and one for a web service.</p> <p>I want to overload the <strong>getAll</strong> method to accept some sort of ordering parameter to indicate how the returned value should be ordered.</p> <p>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.</p> <p>What's the best practice to provide such a parameter without coupling the caller to a specific implementation? </p> <p><strong>EDIT</strong></p> <p>Small clarification to my question. I don't just want to specify a parameter to indicate the order direction, but also <strong>what</strong> to order on.</p> <p>For example, suppose my Country model was defined as follows:</p> <pre><code>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; } </code></pre> <p>And I want to the returned value to be ordered by <strong>name</strong> 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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload