Showing posts with label Active Record. Show all posts
Showing posts with label Active Record. Show all posts

Saturday, 24 August 2013

Data Source Architectural Patterns

🚀 Master Enterprise Java & Software Architecture!

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

🔔 Subscribe Now

What are Data Source Architectural Patterns?

Connecting enterprise business logic to relational database systems is one of the most critical aspects of application design. If data access code is scattered haphazardly across your application, changes to the database structure quickly break business rules.

In Martin Fowler’s classic work, Patterns of Enterprise Application Architecture (PofEAA), Data Source Architectural Patterns provide proven architectural strategies to isolate, organize, and manage the communication between in-memory business objects and database tables.

The 4 Core Data Source Architectural Patterns

Martin Fowler categorizes four core patterns that define how your application interfaces with a relational database:

1. Table Data Gateway

An object that acts as a Gateway to an entire database table. A single instance handles all rows in the table, providing methods to query, insert, update, and delete rows (e.g., returning RecordSets or generic collections).

2. Row Data Gateway

An object that acts as a Gateway to a single row or record in a data source. One instance corresponds to exactly one row in the table, encapsulating data access logic for that record while keeping business logic separate.

3. Active Record

An object that wraps a single row in a database table or view, encapsulates the database access, and adds domain business logic directly on that data. It combines both data persistence and business rules within the same class.

4. Data Mapper

A layer of mappers that moves data between objects and a database while keeping them completely independent of each other and the mapper itself. This pattern provides complete decoupling, allowing the in-memory domain model and database schema to evolve independently.

How to Choose the Right Pattern?

  • Simple Applications & CRUD: Table Data Gateway or Row Data Gateway are excellent when coupled with Transaction Script or Table Module patterns.
  • Medium Complexity: Active Record is ideal when business logic is relatively simple and closely follows database tables.
  • Complex Domain Logic: Data Mapper is the best choice when using a rich Domain Model, ensuring domain entities remain pure and untainted by persistence code.

Recommended Design Pattern Tutorials:


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

Click the below Image to Enlarge
Data Source Architectural  Patterns















See also:

  • All Design Patterns Links
  • 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
  • Tutorials