Domain Driven Design and IoC/Dependency Injection

前端 未结 5 1764
借酒劲吻你
借酒劲吻你 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?

    • Application services: no
    • Domain services: yes, because they are in the domain layer
    • Factories: yes, because they are in the domain layer
    • Repository interfaces: yes, because they are in the domain layer
    • Repository implementations: no, because they are in the infrastructure layer

    Notice the differente between interface and implementation: that's why you should use interface & implementations.

    And FYI, Factories and Repositories are services after all, so you can generalize:

    • Service interfaces: yes if they are in the domain layer

    Easy

    A domain service is one which is defined within the domain layer, though the implementation may be part of the infrastructure layer. A repository is a domain service whose implementation is indeed in the infrastructure layer, while a factory is also a domain service whose implementation is generally within the domain layer.

    (Source: http://www.methodsandtools.com/archive/archive.php?id=97p2)

    Should a Repository be aware of Services in the domain?

    Usually no, why would it after all? The repository manage persistence. But I don't think that's "prohibited" because the infrastructure layer (persistence) knows about the domain layer.

    Another thing that is bothering my mind is how to treat to collections when I want to add and entity to the collection.

    Prefer the OOP approach when possible:

    $article = new Article();
    $article->addTag($tag);
    $articleRepository->save($article);
    

    I makes way more sense.

    a domain service is any business logic that does not easily live within an entity.

    (http://www.methodsandtools.com/archive/archive.php?id=97p2)

    or also:

    When a significant process or transformation in the domain is not a natural responsibility of an ENTITY or VALUE OBJECT, add an operation to the model as a standalone interface declared as a SERVICE.

    (Eric Evans)

    To sum up, create a domain service if you feel you need to, this is not automatic.

提交回复
热议问题