Data access

Context object
Name / classification Context Object Also known as Context, Encapsulate Context Intent Decouple data from protocol-specific classes and store the scoped data in an object independent of the underlying protocol technology. Explanation Real-world example This application has different layers labelled A, B and C with each extracting specific information from a similar context for further use in the software. Passing down each pieces of information individually would be inefficient, a method to efficiently store and pass information is needed.
Data Access Object
Intent Object provides an abstract interface to some type of database or other persistence mechanism. Explanation Real world example There’s a set of customers that need to be persisted to database. Additionally we need the whole set of CRUD (create/read/update/delete) operations so we can operate on customers easily. In plain words DAO is an interface we provide over the base persistence mechanism. Wikipedia says In computer software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism.
Metadata Mapping
Intent Holds details of object-relational mapping in the metadata. Explanation Real world example Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code. In plain words Metadata Mapping specifies the mapping between classes and tables so that we could treat a table of any database like a Java class. Wikipedia says Create a “virtual object database” that can be used from within the programming language.
Private Class Data
Intent Private Class Data design pattern seeks to reduce exposure of attributes by limiting their visibility. It reduces the number of class attributes by encapsulating them in single Data object. Explanation Real world example Imagine you are cooking a stew for your family for dinner. You want to prevent your family members from consuming the stew by tasting it while you are cooking, otherwise there will be no more stew for dinner later.
Repository
Intent Repository layer is added between the domain and data mapping layers to isolate domain objects from details of the database access code and to minimize scattering and duplication of query code. The Repository pattern is especially useful in systems where number of domain classes is large or heavy querying is utilized. Explanation Real world example Let’s say we need a persistent data store for persons. Adding new persons and searching for them according to different criteria must be easy.
Resource Acquisition Is Initialization
Intent Resource Acquisition Is Initialization pattern can be used to implement exception safe resource management. Class diagram Applicability Use the Resource Acquisition Is Initialization pattern when You have resources that must be closed in every condition
Serialized Entity Pattern
Intent To easily persist Java objects to the database. Explanation Java serialization allow us to convert the object to a set of bytes. We can store these bytes into database as BLOB(binary long objects) and read them at any time and reconstruct them into Java objects. Programmatic Example Walking through our customers example, here’s the basic Customer entity. 1@Getter 2@Setter 3@EqualsAndHashCode 4@ToString 5@AllArgsConstructor 6public class Country implements Serializable { 7 8 private int code; 9 private String name; 10 private String continents; 11 private String language; 12 public static final long serialVersionUID = 7149851; 13 // Constructor -> 14 // getters and setters -> 15} Here is CountrySchemaSql, this class have method allow us to serialize Country object and insert it into the database, also have a method that read serialized data from the database and deserialize it to Country object.
Service Layer
Intent Service Layer is an abstraction over domain logic. It defines application’s boundary with a layer of services that establishes a set of available operations and coordinates the application’s response in each operation. Explanation Typically applications require different kinds of interfaces to the data they store and the logic they implement. Despite their different purposes, these interfaces often need common interactions with the application to access and manipulate its data and invoke its business logic.
Unit Of Work
Intent When a business transaction is completed, all the updates are sent as one big unit of work to be persisted in one go to minimize database round-trips. Explanation Real-world example Arms dealer has a database containing weapon information. Merchants all over the town are constantly updating this information and it causes a high load on the database server. To make the load more manageable we apply to Unit of Work pattern to send many small updates in batches.