Search code examples
abp-framework

About using DomainService


  1. I cannot fully understand in which case I will use DomainService and in which case ApplicationService. Your documents are not detailed enough on this subject. Can you give a more detailed explanation with an example?
  2. What could be a problem if I only use ApplicationService and never use DomainService.
  3. Which operations should I do in DomainService?
  4. What kind of a way should I follow in order to distinguish these two services from each other.

Solution

  • Short answer: Only use Domain Service if you need that additional level of abstraction.

    Long answer as follows.

    1. When do I use Domain Service?

    From the documentation Domain Services #Introduction:

    Creating a Domain Service is especially needed when;

    • You implement a core domain logic that depends on some services (like repositories or other external services).
    • The logic you need to implement is related to more than one aggregate/entity, so it doesn't properly fit in any of the aggregates.

    Example from the documentation Domain Services #Introduction: When assigning an Issue to a User, check that the number of Issues assigned is not more than or equal to a certain number.

    Example from the tutorial Part 6: Authors: Domain layer: When changing the Name of an Author, check that there is not already an author with the given name.

    2. What could be a problem if I don't use Domain Service?

    You could miss a check that is a core domain logic or business rule.

    But generally, no problem. From the tutorial Part 6: Authors: Domain layer:

    DDD tip: Do not introduce domain service methods unless they are really needed and perform some core business rules.

    3. Which operations should I do in Domain Service?

    See examples in #1 and #4.

    4. How should I distinguish Domain Service from Application Service?

    From the documentation Domain Services #Application Services vs Domain Services:

    While both of Application Services and Domain Services implement the business rules, there are fundamental logical and formal differences;
    • Application Services implement the use cases of the application (user interactions in a typical web application), while Domain Services implement the core, use case independent domain logic.
    • Application Services get/return Data Transfer Objects, Domain Service methods typically get and return the domain objects (entities, value objects).
    • Domain services are typically used by the Application Services or other Domain Services, while Application Services are used by the Presentation Layer or Client Applications.

    From the tutorial Part 8: Author: Application Layer:

    • Used the AuthorManager (domain service) to create a new author.
    • Used the IAuthorRepository.InsertAsync to insert the new author to the database.
    • Used the ObjectMapper to return an AuthorDto representing the newly created author.

    DDD tip: Some developers may find useful to insert the new entity inside the _authorManager.CreateAsync. We think it is a better design to leave it to the application layer since it better knows when to insert it to the database (maybe it requires additional works on the entity before insert, which would require to an additional update if we perform the insert in the domain service). However, it is completely up to you.