Wednesday, April 6, 2011

Rich Data Model versus Anemic Data Model

We are moving away from an Anemic Data Model (the Entity contains only data; the Logic to validate/manipulate/cross join this data is in the EJB Service)
towards a Rich Data Model (Entities contain most logic, entity methods are allowed to retrieve data from related entities).

This is more in line with good OO practices (mostly encapsulation), and it avoids have a lot of imperative code in the Service Layer. It improves, of course, code reusability, since the logic is one layer down the Service layer - so you avoid cross-services calls.

The only drawback is that Entities become more Service-like, sometimes they need to make DAO or EJB calls to retrieve extra information - its very difficult to expect an Entity to stay as a pure POJO without any access to Resources.

See also an often-quoted MF article.

No comments: