One service for each entity?

前端 未结 2 1968
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-11 06:55

Again - i\'m confused about DDD things :)

I have architeture (I\'m still working on it) that in short hand looks like that:

DataLayer:
 EntityDao -> I         


        
2条回答
  •  北海茫月
    2021-02-11 06:58

    1. One service per entity?

    No. You do not need to create one service for one entity. In DDD you would create services for operations that do not naturally map to a single entity (or value object). A good service (from Evans) :

    • The operation relates to a domain concept that is not a natural part of an entity or value object.
    • The interface is defined in terms of elements of the domain.
    • The operation is stateless

    So a service can consume many entities and there might be many entities that aren't consumed by a single service at all.

    2a. Should services have "query" methods (..)?

    No. Generally speaking those are repository methods and are not placed on services. However, there can be operations on a service that return a collection of entities.

    2b.Should I stop to use Repositories in upper layers (UI) to get sets ("Find"-like methods) of entity's and start to use only services?

    That might be a good idea. Often, when an application uses many repositories in the UI layer, the UI performs domain operations on multiple entities. These operations should typically be implemented in the domain layer; either in the entities themselves, or in services.

    3. Should I use Services and Repositories in parallel from the UI?

    Better not, see above; although there might be situations where you can quickly create part of your UI by doing so.

    4. Somehow I feel that Repositoriess don't fit in Domain layer ...

    You're right, you should only put repository interfaces in the domain. See Kostassoid's answer for an example.

提交回复
热议问题