Hi Guys,
I’m trying to understand why the userId and passwordHash are not set directly in the constructor of the User aggregate. I understand why the constructor applies a UserCreatedEvent but i’m not really sure why one would not directly set the userId and passwordHash in the constructor.
Kind regards,
Robby
public class User extends AbstractAnnotatedAggregateRoot {
private static final long serialVersionUID = 3291411359839192350L;
@AggregateIdentifier
private UserId userId;
private String passwordHash;
protected User() {
}
public User(UserId userId, String username, String name, String password) {
apply(new UserCreatedEvent(userId, name, username, hashOf(password.toCharArray())));
}
public boolean authenticate(char[] password) {
boolean success = this.passwordHash.equals(hashOf(password));
if (success) {
apply(new UserAuthenticatedEvent(userId));
}
return success;
}
@EventHandler
public void onUserCreated(UserCreatedEvent event) {
this.userId = event.getUserIdentifier();
this.passwordHash = event.getPassword();
}
private String hashOf(char[] password) {
return DigestUtils.sha1(String.valueOf(password));
}
@Override
public UserId getIdentifier() {
return userId;
}
}