Domain Driven Design and IoC/Dependency Injection

前端 未结 5 1770
借酒劲吻你
借酒劲吻你 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:03

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

    An entity should never reference a repository or an application service. It is acceptable for entities to reference a factory if it uses it to create a constituent entity. It is also acceptable for an entity to have a dependency on a domain service if it uses that services for certain behavior.

    Should a Repository be aware of Services in the domain?

    Generally no. A repository should only be responsible for persistence.

    Now, in case I want to add a relation with a new tag. What would be the better way to do it?

    It depends on which layer you refer to. In a typical DDD architecture, you'd have both pieces of code. You'd have an article application service which encapsulates the domain and provides a granular method such as addTag where you'd pass an article ID and a tag ID. This method would retrieve the appropriate article and tag instances (if needed) and then:

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

    All outer layers depending on this domain would communicate with it through the application service.

提交回复
热议问题