Class: Sequent::Core::CommandService

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/core/command_service.rb

Overview

Single point in the application where subclasses of Sequent::Core::BaseCommand are executed. This will initiate the entire flow of:

  • Validate command

  • Call correct Sequent::Core::BaseCommandHandler

  • CommandHandler decides which Sequent::Core::AggregateRoot (s) to call

  • Events are stored in the Sequent::Core::EventStore

  • Unit of Work is cleared

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ CommandService

Create a command service with the given configuration.

+event_store+ The Sequent::Core::EventStore
+aggregate_repository+ The Sequent::Core::AggregateRepository
+transaction_provider+ How to do transaction management.
+command_handlers+ List of command handlers that need to handle commands
+command_filters+ List of filter that respond_to :execute(command). Can be useful to do extra checks (security and such).


25
26
27
# File 'lib/sequent/core/command_service.rb', line 25

def initialize(configuration)
  self.configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



16
17
18
# File 'lib/sequent/core/command_service.rb', line 16

def configuration
  @configuration
end

Instance Method Details

#execute_commands(*commands) ⇒ Object

Executes the given commands in a single transactional block as implemented by the transaction_provider

For each command:

  • All filters are executed. Any exception raised will rollback the transaction and propagate up

  • If the command is valid all command_handlers that handles_message? is invoked

  • The repository commits the command and all uncommitted_events resulting from the command



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sequent/core/command_service.rb', line 36

def execute_commands(*commands)
  begin
    transaction_provider.transactional do
      commands.each do |command|
        filters.each { |filter| filter.execute(command) }

        raise CommandNotValid.new(command) unless command.valid?
        parsed_command = command.parse_attrs_to_correct_types
        command_handlers.select { |h| h.handles_message?(parsed_command) }.each { |h| h.handle_message parsed_command }
        repository.commit(parsed_command)
      end
    end
  ensure
    repository.clear
  end

end

#remove_event_handler(clazz) ⇒ Object



54
55
56
# File 'lib/sequent/core/command_service.rb', line 54

def remove_event_handler(clazz)
  event_store.remove_event_handler(clazz)
end