Code, JavaEE

CQRS end-to-end

The following is an exercise to perform cqrs actions for one user story

[As a user I want to create an account]

Some of the persistence details have been omitted, the basic form is here.


public class CqrsTest {

@Test
 public void testCqrs() {
 // create a user ... this is all the Controller has to do!
 CreateUserCommand cmd = new CreateUserCommand(UUID.randomUUID().toString(), "davidking","davidking");
 CreateUserCommandHandler handler = new CreateUserCommandHandler(new UserRepository());
 handler.handle(cmd);
 }
}

interface Repository<T> { T getById(UUID id); void add(T aggregateRoot); }
interface Command {}
interface CommandHandler<Command> { public void handle(Command command); }

class UserRepository implements Repository<User> {
 private List<User> repo = new LinkedList<>();
 @Override
 public User getById(UUID id) {
 return new User(id.toString(), "davidking", "davidking");
 }

@Override
 public void add(User aggregateRoot) {
 repo.add(aggregateRoot);
 }
}

class CreateUserCommand implements Command {
 public String id;
 public String username;
 public String password;

CreateUserCommand(String id, String username, String password) {
 this.id = id;
 this.username = username;
 this.password = password;
 }
}
class CreateUserCommandHandler implements CommandHandler<CreateUserCommand> {
 private Repository<User> repository;

public CreateUserCommandHandler(Repository<User> repository) {
 this.repository = repository;
 }

@Override
 public void handle(CreateUserCommand createUserCommand) {
 User user = new User(createUserCommand.id,
 createUserCommand.username,
 createUserCommand.password);

repository.add(user);
 }
}

import events.UserCreatedEvent;
import events.UserCreatedEventHandler;

/**
 *
 * @author david.king
 */
public class User {
 private String id;
 private String username;
 private String password;

 public User(final String id, final String username, final String password) {
 this.id = id;
 this.username = username;
 this.password = password;

// figure out how to make this easier...
 UserCreatedEvent userCreatedEvent = new UserCreatedEvent(id, username, password);
 UserCreatedEventHandler handler = new UserCreatedEventHandler();
 handler.handle(userCreatedEvent);
 }

 public String id() {
 return id;
 }

 public String username() {
 return username;
 }

 public String password() {
 return password;
 }

@Override
 public String toString() {
 return "User{" + "id=" + id + ", username=" + username + ", password=" + password + '}';
 }

}

public interface Event {
}

public interface EventHandler<Event> {

public void handle(Event event);

}

public class UserCreatedEvent implements Event {
 private String id;
 private String username;
 private String password;

public UserCreatedEvent(String id, String username, String password) {
 this.id = id;
 this.username = username;
 this.password = password;
 }

@Override
 public String toString() {
 return "UserCreatedEvent{" +
 "id='" + id + '\'' +
 ", username='" + username + '\'' +
 ", password='" + password + '\'' +
 '}';
 }
}

public class UserCreatedEventHandler implements EventHandler<UserCreatedEvent> {
 @Override
 public void handle(UserCreatedEvent userCreatedEvent) {
 System.out.format("%n Saving event to the event store. %s", userCreatedEvent);

System.out.format("%n%s", "Creating UserReport with event details.");

System.out.format("%n%s", "Saving userReport to reportingRepository.");
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s