Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface UserRegistrationService {
- void isEmailAlreadyUsed(String email);
- }
- interface PasswordHashService {
- String hash(String plainTextPassword);
- }
- class User {
- private String email;
- private String passwordHash;
- private User(String email, String passwordHash) {
- this.email = email;
- this.passwordHash = passwordHash;
- }
- public static User registerNew(
- String email,
- String passwordInPlainText,
- UserRegistrationService userRegistrationService,
- PasswordHashService passwordHashService
- ) {
- if (userRegistrationService.isEmailAlreadyUsed(email) {
- throw new DomainException(...);
- }
- return new User(email, passwordHashService.hash(passwordInPlainText));
- }
- }
- class RegisterNewUserCommand {
- public String email;
- public String passwordInPlainText;
- }
- class RegisterNewUserCommandHandler {
- private UserRegistrationService userRegistrationService;
- private PasswordHashService passwordHashService;
- private UserRepository userRepository;
- public RegisterNewUserCommandHandler(
- UserRegistrationService userRegistrationService,
- PasswordHashService passwordHashService,
- UserRepository userRepository
- ) {
- this.userRegistrationService = userRegistrationService;
- this.passwordHashService = passwordHashService;
- this.userRepository = userRepository;
- }
- public void handle(RegisterNewUserCommand command) {
- User registeredUser = User.registerNew(
- command.email,
- command.passwordInPlainText,
- userRegistrationService,
- passwordHashService
- );
- userRepository.save(registeredUser);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment