Avoiding anemic domain model - a real example

后端 未结 4 866
再見小時候
再見小時候 2021-01-29 21:13

I am trying to understand Anemic Domain Models and why they are supposedly an anti-pattern.

Here is a real world example.

I have an Employee class, which has a t

相关标签:
4条回答
  • 2021-01-29 21:59

    I think your design is fine here. As you know, the anemic domain model anti-pattern is a backlash against the trend of avoiding any behaviour coded in domain objects. But conversely it doesn't mean all behaviour relating to a domain object must be encapsulated by that object.

    As a rule of thumb, behaviour that is intrinsicly tied to the domain object and is defined entirely in terms of that one domain object instance can be included in the domain object. Otherwise, to keep responsibilities clear, it's best to put it externally in a collaborator/service like you have done.

    0 讨论(0)
  • 2021-01-29 22:07

    It's all in your head - consider rotation service to be a part of the domain model and the problem dissolves.

    Rotation needs to keep information about many employees, so it belongs to neither lead, nor to any single employee object. It does to deserve to be a domain object in itself.

    Just renaming "RotationService" to something like "Organization.UserSupportDepartment" makes it obvious.

    0 讨论(0)
  • 2021-01-29 22:10

    In this case this doesn't constitute an Anemic Domain Model. An Anemic Domain Model is specifically about validating and transforming the objects. So an example of this would be if an external function actually changed the state of the Employees or updated their details.

    what is happening in this case is you are taking all of the employees and making a selection of one of them based on their information. It is fine to have a separate object that examines others and makes decisions with regard to what it finds. It is NOT ok to have an object that is used to transition an object from one state to another.

    An example of an Anemic Domain Model in your case would be to have an external method

    updateHours(Employee emp) // updates the working hours for the employee
    

    that takes an Employee object and updates its hours worked for the week, making sure that flags are raised if the hours exceed a certain limit. The problem with this is that if you only have Employee objects then you have no knowledge of how to modify their hours within the correct constraints. In this case the way to deal with it would be to move the updateHours method into the Employee class. That is the crux of the Anemic Domain Model anti pattern.

    0 讨论(0)
  • 2021-01-29 22:14

    If your domain model contains only roles and things, not activities as behavior, then it is anemic. However, I'm talking about behavior in regards to a model not an object. I talk of the difference between them in another answer... https://stackoverflow.com/a/31780937/116442

    From your question, you break my first two domain analysis modelling rules:-

    1. Behavior modeled as (recorded) Activities are at the heart of a domain model. Add them first.
    2. Model domain activities as Classes, not methods.

    I would add an activity "Enquiry" to the model. With it the model has behavior, and can combine and work as group of objects without an external controller or script.

    0 讨论(0)
提交回复
热议问题