Anemic Domain Model

Source: Internet
Author: User

Ref: http://www.martinfowler.com/bliki/AnemicDomainModel.html

25 November 2003
Reactions

This is one of those anti-patterns that's been around for quite a long time, yet seems to be having a particle spurt at the moment. I was chatting with Eric Evans on this, and we 've both noticed they seem to be getting more popular. as great boosters of a proper domain model, this is not a good thing.

the basic symptom of an anemic domain model is that at first blush it looks like the real thing. there are objects, 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 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.

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

now object-oriented purism is all very well, but I realize that I need more fundamental arguments against this anemia. in essence the problem with anemic domain models is that they incur all of the costs of a domain model, without yielding any of the benefits. the primary cost is the awkwardness of mapping to a database, which typically results in a whole layer of O/R Mapping. this is worthwhile iff you use the powerful OO techniques to organize complex logic. by pulling all the behavior out into services, however, you essential end up with transaction scripts, and thus lose the advantages that the domain model can bring. as I discussed in P of EAA, Domain Models aren't always the best tool.

It's also worth emphasizing that putting behavior into the domain objects shocould not contradict the solid approach of using layering to separate domain logic from such things as persistence and presentation responsibilities. the logic that shoshould be in a domain object is domain logic-Validations, calculations, Business Rules-whatever you like to call it. (there are cases when you make an argument for putting data source or presentation logic in a domain object, but that's orthogonal to my view of anemia .)

One source of confusion in all this is that too oo experts do recommend putting a layer of procedural services on top of a domain model, to form a service layer. but this isn't an argument to make the domain model void of behavior, indeed service layer advocates use a service layer in conjunction with a behaviorally rich domain model.

Eric Evans's excellent book domain driven design has the following to say about these layers.

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.

The key point here is that the service layer is thin-all the key logic lies in the domain layer. He reiterates this point in his service pattern:

Now, the more common mistake is to give up too easily on fitting the behavior into an appropriate object, gradually slipping toward procedural programming.

I don't know why this anti-pattern is so common. I suspect it's due to keep people who haven't really worked with a proper domain model, particle ly if they come from a data background. some technologies encourage it; such as J2EE's entity beans which is one of the reasons I prefer pojo Domain Models.

In general, the more behavior you find in the services, the more likely you are to be robbing yourself of the benefits of a domain model. if all your logic is in services, you 've robbed yourself blind.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.