Loading...
Skip to Content

Understanding the Domain Model Through the Three Little Pigs

The Three Little Pigs

 Once upon a time, the village where the three little pigs lived was a peaceful and safe place. Each pig had a house of its own and lived happily. The first pig hastily built a house of straw, but it was not sturdy. The second pig built a somewhat better house of wood, but it still was not solid. The youngest pig wisely used bricks and built a strong house.

A wolf was lurking in this village. The wolf set out to test each house, and the houses of the first and second pigs collapsed easily. But the youngest pig's brick house withstood the wolf's attack.

Through this story we can see three different approaches.
The first pig was like a transaction script. He rushed to finish the job and get results quickly, but the result was not sturdy enough.
The second pig resembled an active record. He tried to find a better way, but flaws remained.
And the youngest pig was like a domain class. He laid a solid foundation and built a sturdy structure.

A transaction script is like a house of straw.

A house made of straw is simple and quick to build, but it lacks strong defenses and durability. Likewise, a transaction script that handles business rules directly in SQL and the like deals with each function simply and works with the database directly, but it can fall short when it comes to handling complex business rules or the complexity of the domain.

An active record is like a log cabin.

A house made of logs is robust and solid, but it may still have a few weak points. Similarly, an active record maps database records directly to objects and makes database manipulation convenient, but because business logic and database access are intermingled, it can be hard to maintain.

A domain class is like a house of bricks.

Bricks provide a solid, stable structure that responds well to change and challenge. Domain classes are designed around business rules and domain objects, separating domain logic from database access, which makes them well suited to handling the complexity of a system. This structure provides flexibility and extensibility, allowing the software system to remain stable and evolve over time.