1

From what I've read, domain services typically don't import or rely on Repository directly in theory,The responsibilities of domain services focus on handling business logic and coordinating interactions between domain objects.

But in actual development, I found that I had to call the data warehousing interface in the domain service to implement my requirements. I do not know whether it is because of the discrepancy between the theory and the actual landing, or because of my misunderstanding of the concept of domain service and application service.

To this end, I wrote a code snippet, the process is: by checking whether the current user is normal, if normal.it will check whether it is the same account as the author of the article, if it is the same account can modify the text.

I hope you can tell me whether this design is correct? If it is wrong, what should be done? If so, why is it theoretically recommended that domain services not import dependency repositories directly?

//Domain service: UserService
public class UserService {

    //Check whether the current account status is normal
    public boolean verifyUserStatus(String userId){
        User user = userRepository.selectById(userId);
        return user.verifyUserStatus();
    }
}

//Domain service:ArticleService
public class ArticleService {
    public boolean changeArticleContent(String articleId, String userId,String content) {
        Article article = articleRepository.selectById(articleId);

        //Check whether the current user and the author are the same
        boolean verifyAuthorResult = article.verifyAuthor(userId);
        if (verifyAuthorResult){

            //Modify article content and update to database
            article.changeArticleContent(content);
            articleRepository.updateById(article);
            return true;
        }
        return false;
    }
}

//Application service
public class ArticleApplicationService {
    //Verify the user status, and allow to modify the article content if the user status is normal
    public boolean changeArticle(String userId, String articleId, String content) {
        boolean verifyUserResult = userService.verifyUserStatus(userId);
        if (verifyUserResult) {
            return articleService.changeArticle(articleId, userId, content);
        }
        return false;
    }
}
2

1 Answer 1

3

I think the issue here is that you are putting methods on your Models.

Your logic needs the data from both an article and a user to perform its calculation. You should be able to write a Domain Service like :

UserVerificationService
{
    bool IsValid(User u, Article a)
    {
      return u.Status == "x" && (a.authorId == u.Id ¦¦ u.IsSuperUser)}
    }
}

This is pure logic with no calls to the persistence layer.

The reason to have a domain service is for these cases where you cross bounded contexts and or Aggregate roots. The can sit between and outside of the Domains, so that you don't have to couple two or more unrelated models.

If you take the approach you outline, you can get into the situation where services in the user domain reference services in the account domain and vice versa. Creating infinite loops if not circular references.

With the anemic approach you can reference both domains and the Domain Service at the top level only, which you have to in any case, and keep all your references pointing "down hill"

Also, you should try to avoid loading objects from Ids in services which might end up being buried deep in code which might already have loaded those objects, or have in memory, altered versions of them. You don't want to force a database read/write when its not required.

2
  • So you are saying that domain services should not invoke the persistence layer, but just handle the interaction logic of multiple domain objects?
    – Ares
    Commented Jan 4 at 1:42
  • 1
    I would separate those statements out a bit. 1. "Domain Services", are a solution for when you have cross domain logic. . 2. Avoid passing repositories into services/objects in general
    – Ewan
    Commented Jan 4 at 9:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.