Saturday, 10 August 2013

Active Record Design Pattern

🚀 Master Enterprise Java & Software Architecture!

Subscribe to Ram N Java for clean, visual, and beginner-friendly programming tutorials!

🔔 Subscribe Now

What is the Active Record Design Pattern?

The Active Record Pattern is one of the most prominent Data Source Architectural Patterns defined in Martin Fowler's classic enterprise book, Patterns of Enterprise Application Architecture (PofEAA).

Core Definition: An Active Record is an object that wraps a row in a database table or view, encapsulates database access, and adds domain logic directly on that data.

Unlike traditional passive entities or data transfer objects, an Active Record object carries both data and behavior: it knows how to read and write itself to the database table, as well as execute business calculations.

Real-World Example: Person Table and Person Class

Suppose we have a database table named person with three columns:

  • last_name (e.g., "Joe", "Goni")
  • first_name
  • number_of_dependents

In an Active Record implementation, the fields of the Person class correspond directly to the table columns, while the class also contains CRUD persistence operations and business calculations:

public class Person {
    // 1. Data Structure (Matches database columns exactly)
    private String lastName;
    private String firstName;
    private int numberOfDependents;

    // 2. Database Access Methods (CRUD)
    public void insert() { /* execute INSERT SQL */ }
    public void update() { /* execute UPDATE SQL */ }
    public void delete() { /* execute DELETE SQL */ }
    public static Person find(Long id) { /* execute SELECT SQL */ }

    // 3. Domain Business Logic
    public double getExemption() {
        return numberOfDependents * 1500.0;
    }

    public boolean isFlaggedForAudit() {
        return numberOfDependents > 5;
    }

    public double getTaxableEarnings() {
        // Business logic calculation
        return calculateEarnings() - getExemption();
    }
}

Key Characteristics of Active Record

  • Exact Schema Match: The class attributes mirror the database table columns one-to-one.
  • Self-Sustaining Records: Each instance is an in-memory representation of a single table row that handles its own persistence and validation.
  • Combines Persistence and Logic: Brings together database interaction and domain behavior into a unified, intuitive class.

Active Record vs. Row Data Gateway: What is the Difference?

Both patterns wrap a single database row, but they differ fundamentally in their responsibilities:

  • Row Data Gateway: Contains only database access logic (CRUD) and field values. It has no domain business logic.
  • Active Record: Contains both database access logic and domain business logic that acts on the row's data.

When Should You Use the Active Record Pattern?

Active Record is an ideal architectural choice when:

  • Your domain logic is straightforward and not overly complex (such as calculations, basic rules, and direct CRUD operations).
  • The domain objects closely mirror the relational database schema.
  • You want rapid development without introducing multiple mapping and abstraction layers.

Recommended Design Pattern Tutorials:


Click here to watch in Youtube : https://www.youtube.com/watch?v=7eEa7YwzKpQ

Click the below Image to Enlarge
Active Record Design Pattern














See also:

  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials