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;
}
}