Why does active record pattern not work with rich domain models?
Asked Answered
C

2

11

I'm reading the architectural patterns chapter of POEAA, and Fowler says that "As the domain logic gets more complicated and you begin moving toward a rich Domain Model (116), the simple approach of an Active Record (160) starts to break down. The one-to-one match of domain classes to tables starts to fail as you factor domain logic into smaller classes. Relational databases don't handle inheritance, so it becomes difficult to use strategies [Gang of Four] and other neat OO patterns. As the domain logic gets feisty, you want to be able to test it without having to talk to the database all the time."

I didn't really understand this. By "one to one match of domain classes to tables", does he mean that only for classes where there is no associations or single table inheritance hierarchy?

And why does factoring domain logic into smaller classes cause the pattern to fail?

Catchall answered 11/4, 2011 at 16:17 Comment(0)
T
5

What he's trying to say is that more complex domain models are usually more than just "data from a table". These more complex models Fowler is talking about are models that get data from different tables, views or maybe even other sources.

The Active Record pattern is not very suitable for this purpose, and the DataMapper pattern combined with just model classes (containing just the business logic and do not communicate with the data access layer) is probably more suitable in situations like these.

The Active Record pattern fails here, because it maps more or less directly to a table in the database.

I don't know the exact pattern definition, so please correct me if i'm wrong.

Tade answered 12/4, 2011 at 14:24 Comment(3)
"The Active Record pattern fails here, because it maps more or less directly to a table in the database" - Bingo.Pasadena
Richard, why does the Active Record pattern fail when it's not a 1 to 1 mapping between the data and tables? If you had data across two tables, couldn't you just change your select query to handle this?Catchall
Blacktie24, first of all it is harder to map a complex data structure to an Active Record Model than it is to a Mapper Model. Second, with complexer data structures/domain models it is a good practice to decouple them as much from the underlying data structure as possible, to make refactoring and testing easier. Now it is hard to say where the turnover point between Active Record and couterparts like Domain Model/DataMapper is, but personally i favour the latter. By the way, my version of the book (16th printing) also mentions "When to use it" on page 161.Tade
A
2

No, I think he is talking about the domain logic. With active record the object carries both data and behavior. So that is a one-to-one match. If you start separating data/behaviour, like in the Data Mapper pattern, it becomes one-to-many. I have the impression that you sometimes really have to read that book like academic nonsense to understand what he means. :-)

Atwood answered 12/4, 2011 at 14:11 Comment(1)
Hey tvlooy, thx for taking the time out to reply. So when the behaviour starts getting separated out into strategies and other patterns, how does this affect the active record pattern?Catchall

© 2022 - 2024 — McMap. All rights reserved.