...

/

Solution: The Model Has an Active Record

Solution: The Model Has an Active Record

Let's solve the antipattern Magic Beans using Active Record.

Controllers handle application input while views handle application output, both relatively simple and well-defined tasks. Frameworks are best at helping us put these together quickly. But it’s hard for a framework to provide a one-size-fits-all solution for models because models comprise the rest of the object-oriented design for our application.

This is where we actually need to identify what the objects are in our application and what data and behavior those objects have. It’s true what Robert L. Glass said:the majority of software development is intellectual and creative.

Grasping the model

Fortunately, there’s a lot of wisdom in the field of object-oriented design to guide us. Craig Larman’s book Applying UML and PatternsCraig Larman. Applying UML and Patterns: an Introduction to Object-Oriented Analysis and Design and Iterative Development. Prentice-Hall, Englewood Cliffs, NJ, Third, 2004. describes guidelines called the General Responsibility Assignment Software Patterns (GRASP). Some of these guidelines are especially relevant to separating models from their data access objects:

Information expert

The object responsible for an operation should have all the data needed to fulfill that operation. Since some operations in our application involve multiple tables (or no tables) and Active Record is good at working ...