a.Active Record Pattern

  1. Motivation

      1. An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.

  2. Summary

      1. The essence of an Active Record is a Domain Model in which the classes match very closely the record structure of an underlying database.

      2. Each Active Record is responsible for saving and loading to the database and also for any domain logic that acts on the data.

      3. The data structure of the Active Record should exactly match that of the database: one field in the class for each column in the table.

  1. When to Use

    1. I would mark this pattern obsolete for custom implementation because technologies like Entity Frameworks provide similar behavior out of the box.

    2. Active Record is a good choice for domain logic that isn't too complex, such as creates, reads, updates, and deletes. Derivations and validations based on a single record work well in this structure.

  1. Overview Tutorials

  1. Specific Considerations

      1. Active Record Vs Row Data Gateway

        1. Active Record is very similar to Row Data Gateway.

        2. The principal difference is that a Row Data Gateway contains only database access while an Active Record contains both data source and domain logic.

      2. Data Mapper Vs Active Records

        1. In an initial design for a Domain Model the main choice is between Active Record and Data Mapper.

        2. Active Record has the primary advantage of simplicity. It's easy to build Active Records, and they are easy to understand. Their primary problem is that they work well only if the Active Record objects correspond directly to the database tables: an isomorphic schema.

        3. If your business logic is complex, you'll soon want to use your object's direct relationships, collections, inheritance, and so forth. These don't map easily onto Active Record, and adding them piecemeal gets very messy. That's what will lead you to use Data Mapper instead.

  1. Related Patterns

      1. Row Data Gateway : It is similar pattern.

      2. Transaction Script:Active Record is a good pattern to consider if you're using Transaction Script

  1. References