Anemic domain model
   HOME

TheInfoList



OR:

The anemic domain model is described as a programming
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
where the
domain objects Domain-driven design (DDD) is a major software design approach, focusing on modeling software to match a domain according to input from that domain's experts. Under domain-driven design, the structure and language of software code (class name ...
contain little or no
business logic In computer software, business logic or domain logic is the part of the program that encodes the real-world business rules that determine how data can be created, stored, and changed. It is contrasted with the remainder of the software that might ...
like validations, calculations, rules, and so forth. The business logic is thus baked into the architecture of the program itself, making refactoring and maintenance more difficult and time-consuming.


Overview

This pattern was first described by Martin Fowler, who considers the practice an
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
. He says: In an anemic domain design, business logic is typically implemented in separate classes which transform the state of the domain objects. Fowler calls such external classes ''transaction scripts''. This pattern is a common approach in
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
applications, possibly encouraged by technologies such as early versions of EJB's
Entity Bean An "Entity Bean" is a type of Enterprise JavaBean, a server-side Java EE component, that represents persistent data maintained in a database. An entity bean can manage its own persistence (Bean managed persistence) or can delegate this function to i ...
s, as well as in .NET applications following the Three-Layered Services Application architecture where such objects fall into the category of "Business Entities" (although Business Entities can also contain behavior). Fowler describes the transaction script pattern thus: In his book "Patterns of Enterprise Application Architecture", Fowler noted that the transaction script pattern may be proper for many simple business applications, and obviates a complex OO-database mapping layer. An anemic domain model might occur in systems that are influenced from Service-Oriented Architectures, where behaviour does not or tends to not travel, such as messaging/pipeline architectures, or
SOAP Soap is a salt of a fatty acid used in a variety of cleansing and lubricating products. In a domestic setting, soaps are surfactants usually used for washing, bathing, and other types of housekeeping. In industrial settings, soaps are use ...
/
REST Rest or REST may refer to: Relief from activity * Sleep ** Bed rest * Kneeling * Lying (position) * Sitting * Squatting position Structural support * Structural support ** Rest (cue sports) ** Armrest ** Headrest ** Footrest Arts and entert ...
APIs. Architectures like COM+ and Remoting allow behaviour, but increasingly the web has favoured disconnected and stateless architectures.


Criticism

There is some criticism as to whether this software design pattern should be considered an anti-pattern, since many see also benefits in it, for example: * Clear separation between logic and data. * Works well for simple applications. * Results in stateless logic, which facilitates scaling out. * Avoids the need for a complex OO-Database mapping layer. * More compatibility with mapping and injection frameworks expecting dumb properties rather than a specific constructor or property population order. A common criticism is the idea that anemic domain model makes it easier to follow the
SOLID Solid is one of the State of matter#Four fundamental states, four fundamental states of matter (the others being liquid, gas, and Plasma (physics), plasma). The molecules in a solid are closely packed together and contain the least amount o ...
principles:
"The ‘S’ refers to the
Single Responsibility Principle The single-responsibility principle (SRP) is a computer programming principle that states that "A module should be responsible to one, and only one, actor." The term actor refers to a group (consisting of one or more stakeholders or users) that ...
, which suggests that a class should do one thing, and do it well (...)" .
But, according to
Robert C. Martin Robert Cecil Martin (born 5 December 1952), colloquially called "Uncle Bob", is an American software engineer, instructor, and best-selling author. He is most recognized for developing many software design principles and for being a founder of t ...
, this is a misunderstanding of that principle:
"Of all the SOLID principles, the Single Responsibility Principle (SRP) might be the least well understood. That’s likely because it has a particularly inappropriate name. It is too easy for programmers to hear the name and then assume that it means that every module should do just one thing. Make no mistake, there is a principle like that. A function should do one, and only one, thing. We use that principle when we are refactoring large functions into smaller functions; we use it at the lowest levels. But it is not one of the SOLID principles—it is not the SRP. (...) the final version of the SRP is: A module should be responsible to one, and only one, actor."


Liabilities

Certain liabilities the programmer must consider are introduced by using an anemic domain model: * Logic cannot be implemented in a truly object-oriented way. * Violation of the encapsulation and
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
principles. * Needs a separate business layer to contain the logic otherwise located in a
domain model In software engineering, a domain model is a conceptual model of the domain that incorporates both behavior and data.Fowler, Martin. ''Patterns of Enterprise Application Architecture''. Addison Wesley, 2003, p. 116. In ontology engineering, a do ...
. It also means that
domain model In software engineering, a domain model is a conceptual model of the domain that incorporates both behavior and data.Fowler, Martin. ''Patterns of Enterprise Application Architecture''. Addison Wesley, 2003, p. 116. In ontology engineering, a do ...
's objects cannot guarantee their correctness at any moment, because their validation and mutation logic is placed somewhere outside (most likely in multiple places). * Needs a service layer when sharing domain logic across differing consumers of an object model. * Makes a model less expressive.


Example

An anemic domain model would have one write code like the following (written in C#), which by itself does not implement any of the business concerns, in this case, that a height or a width cannot be zero or negative, or that somewhere else there is a requirement for the area of the rectangle. This means that those functionalities are implemented somewhere else, no longer on the "business" side of the program, but somewhere else hidden within its architecture. class Rectangle A non-anemic rewrite of the above class could look like the following. The business concerns are now handled in the domain object, while the architecture can be more domain-agnostic. class Rectangle


See also

* Plain old Java object *
Domain-driven design Domain-driven design (DDD) is a major software design approach, focusing on modeling software to match a domain according to input from that domain's experts. Under domain-driven design, the structure and language of software code (class name ...
* GRASP information expert, an anemic domain model is the typical result of not applying the information expert principle, i.e. you can avoid an anemic domain model by trying to assign responsibilities to the same classes that contain the data


References

{{reflist


External links


Anemic Domain Model
by Martin Fowler
Three-Layered Services ApplicationApplication Architecture for .NET: Designing Applications and ServicesArticle on why anemic model may be considered good designWriting Clean Code in ASP.NETHow to Avoid Anemic Domain Model
Software architecture Anti-patterns