I try to practice DDD but I have a doubt about this type of rules that I wrote bellow:
class UserAggregator under domain layer.
public class UserAggregator{
private UUID userId;
private String userName;
private String country;
//getter
boolean isUserNamePatternCorrect(){
return this.userName.startsWith("TOTO");
}
}
Repository interface under domain layer.
public interface UserRepository {//User Table
public UserAggregator findUser(UUID id);
public void deleteUser(UserAggregator userToARchive);
}
public interface ArchiveUserRepository { //ArchiveUser Table
public archiveUser(UUID id);
}
the UserRepository/ArchiveUserRepository are implemented under infrastructure layer using spring data.
Service under infrastructure layer.
public class MyService {
@Autowired
UserRepository userRepo;
@Autowired
ArchiveUserRepository archiveUserRepo;
public void updateUserName(UUID userId){
UserAggregator userAgg = userRepo.findUser(userId);
if(Objects.nonNull(userAgg)){
boolean isCorrect = userAgg.isUserNamePatternCorrect();
if(!isCorrect){//-----------------------------1
userRepo.deleteUser(userAgg);//-----------2
archiveUserRepo.archiveUser(userAgg);//---3
}
}
//....
}
}
my management rule says, if the userName does not match the pattern then archive the user into archive table and remove it from user table.
as you notice, the rule (from //1 to //3) is written in the service class and not into my agregator!
Is this correct ? or how to manage this ?
As it seems to be an infrastructure rule on how to behave in that particular case of having an incorrect username, I don't see any problem on your code. The responsabilities belong to the repositories IMHO, so I think that this approach is more than correct.