Friday, April 8, 2011

Patterns Used in Spring's Persistence Layer

The application tier that deals with persistence is often called the persistence tier. Spring helps to enforce a modular architecture in which the persistence tier is divided into several core layers that contain the following:

  • The Domain Model
  • The Data Access Object (DAO) Layer
  • The Service Layer (or Service Fa├žade)

Spring goes a long way towards reducing code complexity and maintenance. Following are mostly used of the patterns in spring’s persistence layer:

  1. Template Pattern: The Template pattern allows a template to be defined in which a series of standard steps are followed, delegating to a subclass for those operations that are specific to the business logic. For example, when working with Hibernate, it is first necessary to create and initialize a new Hibernate session and optionally begin a transaction, before executing any Hibernate operations. When the operations are completed, it is necessary to close the session, and optionally commit or rollback the transaction. It would be rather redundant to repeat these same steps each time it was necessary to interface with Hibernate. Instead, we can leverage Spring's HibernateTemplate or JpaTemplate abstractions, which handle these steps for us.
  2. Active-Record Pattern: In this pattern, an instance of a particular domain class represents a single row within the respective database table. To save changes to the instance (and thereby the appropriate row within the database), a save instance method is called directly on the instance. To delete an instance, we can simply invoke delete() on the instance that needs to be deleted. Query operations are usually invoked as static methods on the domain class itself.

No comments: