Domain Driven Design and IoC/Dependency Injection

前端 未结 5 1765
借酒劲吻你
借酒劲吻你 2021-02-02 00:43

I\'m trying to apply now what I learned about DDD and I\'m a little bit confused about the flow of dependencies in the Domain model.

My questions are:

  1. Shou
5条回答
  •  清歌不尽
    2021-02-02 01:13

    First thing first, don't get to hung up on it - its easy to over engineer by creating unnecessary service classes etc. Less code is good. Check out the original source material, and the code in either Java or C#.

    1) Should an Entity be aware of Factories, Repositories, Services in the domain?

    It can if required. For example my (java) class annotated with @Entity can also be annotated with @Configurable and have a session/other classes injected into it. That is the point - to encapsulate all necessary business logic and expose a clear simple api located on one Domain class.

    2) Should a Repository be aware of Services in the domain?

    No. But the opposite is likely, a Service would use a repository.

    Services are used when more than one domain object/entity/root aggregate is utilized. So assuming a TAG is a separate entity, this would be fine :

    $articleService->addTag($article, TagEntity); 
    

    However if Tag is anot another root aggregate, you could just do

    $article->tags->add(TagEntity);
    

    And article itself does the save (without any other calls) by having a repository/dao injected inside it.

提交回复
热议问题