CommandHandler
CommandHandlers respond to certain Commands. CommandHandlers inherit from Sequent::CommandHandler.
To respond to a certain Command, a CommandHandler needs to register a block containing the corresponding action to be taken.
class UserCommandHandler < Sequent::CommandHandler
  on CreateUser do |command|
    repository.add_aggregate(User.new(
      aggregate_id: command.aggregate_id,
      firstname: command.firstname,
      lastname: command.lastname,
    ))
  end
end
The Sequent::CommandHandler exposes two convenience methods:
repository, a shorthand for Sequent.configuration.aggregate_repositorydo_with_aggregate, basically a shorthand forrespository.load_aggregate
A CommandHandler can respond to multiple commands:
class UserCommandHandler < Sequent::CommandHandler
  on CreateUser do |command|
    repository.add_aggregate(User.new(
      aggregate_id: command.aggregate_id,
      firstname: command.firstname,
      lastname: command.lastname,
    ))
  end
  on ApplyForLicense do |command|
    do_with_aggregate(command, User) do |user|
      user.apply_for_license
    end
  end
end
A CommandHandler can of course communicate with multiple AggregateRoots.
class UserCommandHandler < Sequent::CommandHandler
  on ApplyForLicense do |command|
    do_with_aggregate(command, User) do |user|
      license_server = repository.load_aggregate(command.license_server_id, LicenseServer)
      user.apply_for_license(license_server.generate_license_id)
    end
  end
end
If you didn’t set enable_autoregistration to true you will need to add your CommandHandler manually to your Sequent configuration in order to use them.
  Sequent.configure do |config|
    config.command_handlers = [
      UserCommandHandler.new
    ]
  end
Testing your CommandHandlers
Tip: If you use rspec you can test your CommandHandler easily by including the Sequent::Test::CommandHandlerHelpers in your rspec config.
You can then test your CommandHandlers via the stanza:
it 'creates a user' do
  given_command CreateUser.new(args)
  then_events UserCreated
end