Is Anemic Domain Model a bundle of smart services

2019-09-19 06:41发布

问题:

I am a bit confused about what is a anemic domain model in OOP. Is a sort of bundle of Plain Old X Object (where X stands for the language you prefer), without behaviors (and responsibilities).

class AnemicDomainClass {
    private $property;
    public function getProperty() {
        return $this->property;
    }
    public function setProperty($property) {
        $this->property = $property;
    }
}

... where all the logic is inside some services?

class SomeStuffService {
    public static function doSomething(AnemicDomainClass $class) {
        $class->setProperty(42);
    }
}

This appear at the end of AnemicDomainModel article of Martin Fowler

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.

This means what? That is better to prefer with smart object instead of smart services.

回答1:

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.

It means write object oriented code, instead of procedural code acting on data. Object oriented code means modeling concepts into objects that know their own attributes and behavior, and which collaborate together to represent a working solution to a problem.

Using a multi-paradigm language that happens to support OOP, does not mean you are writing object oriented code.