How to avoid Anemic Domain Models and maintain Separation of Concerns?

前端 未结 4 1533
我在风中等你
我在风中等你 2021-01-30 06:03

It seems that the decision to make your objects fully cognizant of their roles within the system, and still avoid having too many dependencies within the domain model on the dat

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 06:18

    I aggree with DeadBeef - therein lies the tension. I don't really see though how a domain model is 'anemic' simply because it doesn't save itself.

    There has to be much more to it. ie. It's anemic because the service is doing all the business rules and not the domain entity.

    Service(IRepository) injected
    
    Save(){
    
    DomainEntity.DoSomething();
    Repository.Save(DomainEntity);
    
    }
    
    'Do Something' is the business logic of the domain entity.
    
    **This would be anemic**:
    Service(IRepository) injected
    
    Save(){
    
    if(DomainEntity.IsSomething)
      DomainEntity.SetItProperty();
    Repository.Save(DomainEntity);
    
    }
    

    See the inherit difference ? I do :)

提交回复
热议问题