««« | »»»
OR Mappers: pro e contro i domain models
Una obiezione che viene subito in mente contro l’idea di ridurre i domain objects a semplici EOGenericRecord è quello dell’antipattern (o code smell, fate voi) del cosidetto Anemic Domain Model:
The basic symptom of an Anemic Domain Model is that at first blush it looks like the real thing. There are objects, many named after the nouns in the domain space, and these objects are connected with the rich relationships and structure that true domain models have.
The catch comes when you look at the behavior, and you realize that there is hardly any behavior on these objects, making them little more than bags of getters and setters. Indeed often these models come with design rules that say that you are not to put any domain logic in the the domain objects. Instead there are a set of service objects which capture all the domain logic. These services live on top of the domain model and use the domain model for data.
Sembra una descrizione esatta di quello a cui porta il mio approccio. Ci sono però delle ulteriori considerazioni da fare.
Prima di tutto, l’EOGenericRecord non è proprio una semplice Data Class e fornisce già un bel po’ di logica pronta all’uso e se non basta c’è la versione pompata di Wonder. Di conseguenza il mio approccio sembra più a quello che viene definito un Service Layer:
Application Layer [his name for Service Layer]: Defines the jobs the software is supposed to do and directs the expressive domain objects to work out problems. The tasks this layer is responsible for are meaningful to the business or necessary for interaction with the application layers of other systems. This layer is kept thin. It does not contain business rules or knowledge, but only coordinates tasks and delegates work to collaborations of domain objects in the next layer down. It does not have state reflecting the business situation, but it can have state that reflects the progress of a task for the user or the program.
Domain Layer (or Model Layer): Responsible for representing concepts of the business, information about the business situation, and business rules. State that reflects the business situation is controlled and used here, even though the technical details of storing it are delegated to the infrastructure. This layer is the heart of business software.
In secondo luogo non mi sembra che le argomentazioni di Fowler siano molto convincenti. È vero che l’accoppiamento fra dati e procedure è la ragion d’essere dell’OOP ma questo non dimostra che sia sempre l’approccio migliore (cosa che anche Fowler riconosce). Inoltre non viene portato un singolo esempio concreto di una situazione in cui un Domani Model è chiaramente superiore, a parte gli esempi ovvi.
Distinguere chiaramente i pro e i contro è difficile anche perché i criteri di valutazione sono in parte teorici e in parte pratici (almeno nel mio caso). Ci torneremo.
Per proseguire
Commenti e trackback sono disabilitati.