The data layer
This section describes the existing domain objects that make the up data layer that your RESTful service will reuse.
As you know from Off to the races: Building a RESTful API, Acme Racing invested in a data layer for a previous project and wants to reuse it for the new Web service. This, of course, makes your job even easier. In a nutshell, the data layer consists of three business objects:
Result. They are effectively managed by Spring and Hibernate; however, these frameworks are hidden from you; you simply have a JAR file that works nicely (that is, lets you easily create new races, find existing runners, and so on).
The business objects support a series of finder methods that make obtaining race and runner instances quite easy. Objects can be persisted, updated, and removed from the underlying database via
remove() methods, respectively.
For example, a
Race object supports a series of finder methods and facilitates manipulating persisted data nicely. The
Race object's API is straightforward, as shown in Listing 10:
Collection<Race> findAll(); Race findById(long id); Race findByName(String name); void create(Race race); void update(Race race); void remove(Race race);
Race instance has a number of properties, as shown in Listing 11:
private long id; private String name; private Date date; private double distance; private Set<Runner> participants; private Set<Result> results; private String description;
Race's properties are available via getters and setters. What's more, collections of items (such as
results) support adding individual items. Therefore, the
Race object has an
addParticipant() method, shown in Listing 12:
public void addParticipant(final Runner participant) ;
As you've seen, working with this domain model is quite easy.