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?

Gordon
  • 296,205
  • 68
  • 508
  • 534
blacktie24
  • 4,697
  • 6
  • 37
  • 50

2 Answers2

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.

Richard Tuin
  • 4,263
  • 2
  • 17
  • 17
  • "The Active Record pattern fails here, because it maps more or less directly to a table in the database" - Bingo. – jsuggs Apr 12 '11 at 15:00
  • 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? – blacktie24 Apr 12 '11 at 15:37
  • 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. – Richard Tuin Apr 12 '11 at 18:12
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. :-)

tvlooy
  • 988
  • 9
  • 16
  • 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? – blacktie24 Apr 12 '11 at 17:43