Jump to content

Anemic domain model

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by DannyS712 bot (talk | contribs) at 17:22, 11 May 2020 (Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Anemic domain model is the use of a software domain model where the domain objects contain little or no business logic (validations, calculations, business rules etc.).

Overview

This pattern was first described[1] by Martin Fowler, who considers the practice an anti-pattern. He says:

The fundamental horror of this anti-pattern is that it's so contrary to the basic idea of object-oriented designing; which is to combine data and process them together. The anemic domain model is just a procedural style design, exactly the kind of thing that object bigots like me ... have been fighting since our early days in Smalltalk. What's worse, many people think that anemic objects are real objects, and thus completely miss the point of what object-oriented design is all about.

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 applications, possibly encouraged by technologies such as early versions of EJB's Entity Beans,[1] 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).[2]

Fowler describes the transaction script pattern thus:

Most business applications can be thought of as a series of transactions. A transaction may view some information as organized in a particular way, another will make changes to it. Each interaction between a client system and a server system contains a certain amount of logic. In some cases this can be as simple as displaying information in the database. In others it may involve many steps of validations and calculations. A Transaction Script organizes all this logic primarily as a single procedure, making calls directly to the database or through a thin database wrapper. Each transaction will have its own Transaction Script, although common subtasks can be broken into subprocedures.[3]

In his book "Patterns of Enterprise Application Architecture", Fowler noted that the transaction script pattern is OK for many simple business applications, and obviates a complex OO-database mapping layer.

Reasons why this may occur

AnemicDomainModel may occur in systems that are influenced from Service-Oriented Architectures, where behaviour does not or tends to not travel.

  • Messaging/Pipeline architectures
  • APIs such as SOAP/REST

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[4].
  • 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. [4]

Liabilities

  • Logic cannot be implemented in a truly object-oriented way.
  • Violation of the encapsulation and information hiding principles.
  • Needs a separate business layer to contain the logic otherwise located in a domain model. It also means that domain model'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

Anemic

class Box
{
    public int Height { get; set; }
    public int Width { get; set; }
}

Non-anemic

class Box
{
    public int Height { get; private set; }
    public int Width { get; private set; }

    public Box(int height, int width)
    {
        if (height <= 0) {
            throw new ArgumentOutOfRangeException(nameof(height));
        }
        if (width <= 0) {
            throw new ArgumentOutOfRangeException(nameof(width));
        }
        Height = height;
        Width = width;
    }

    public int Area()
    {
       return Height * Width;
    }
}

See also

References

  1. ^ a b http://www.martinfowler.com/bliki/AnemicDomainModel.html
  2. ^ "Archived copy". Archived from the original on 2013-01-10. Retrieved 2013-02-13.{{cite web}}: CS1 maint: archived copy as title (link)
  3. ^ https://www.martinfowler.com/eaaCatalog/transactionScript.html
  4. ^ a b http://blog.inf.ed.ac.uk/sapm/2014/02/04/the-anaemic-domain-model-is-no-anti-pattern-its-a-solid-design/