I m trying to learn DDD and I m having some troubles dealing with the Infrastructure services.
In fact, I understand what they aim to, but I don't see where they fit inside of my application.
Actually, I m having the following stuff
- An application Service (REST API)
- Some Domain Services (managed with Specification Pattern)
- Some Infrastructure Services (Repositories, and Email sender)
Question 1 I was wondering if it was the domain service role to use (meaning, infrastructure services injected in Domain services), something like (in a very minimalist way) :
class UserDomainService {
constructor (userRepository, emailService) {
this.userRepository = userRepository
this.emailService = emailService
}
saveUser (user) {
/** Apply some business rules here ...*/
const user = this.userRepository.save(user)
this.emailService.send('New user added : ' + user.firstName)
}
}
Or should it be the role of the Application service, like this way :
class ApplicationService {
constructor (userDomainService, userRepository, emailService) {
this.userDomainService = userDomainService
this.userRepository = userRepository
this.emailService = emailService
}
postUser (user) {
if (this.userDomainService.manageSomeRules(user)) {
this.userRepository.save(user)
this.emailService.send('New user added : ' + user.firstName)
return user
}
return new RuleNotSatisfied()
}
}
Question 2
Who's responsible to convert the received user, from POST endpoints (REST API), to a good entity ?
EDIT on question 2 : What kind of service can convert Entity to DTO and vice versa ? Where should I put them ?