Class: Sequent::Core::BaseEventHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers::SelfApplier
Defined in:
lib/sequent/core/base_event_handler.rb

Overview

EventHandlers listen to events and handle them according to their responsibility.

Examples:

  • Updating view states

  • Sending emails

  • Executing other commands based on events (chainging)

Example of updating view state, in this case the InvoiceRecord table representing an Invoice

class InvoiceCommandHandler < Sequent::Core::BaseCommandHandler
  on CreateInvoiceCommand do |command|
    create_record(
      InvoiceRecord,
      recipient: command.recipient,
      amount: command.amount
    )
  end
end

Please note that the actual storage is abstracted away in the record_session. Reason is when replaying the entire event_store our default choice, active_record, is too slow. Also we want to give the opportunity to use other storage mechanisms for the view state. See the def_delegators which method to implement. Due to this abstraction you can not traverse into child objects when using ActiveRecord like you are used to:

invoice_record.line_item_records << create_record(LineItemRecord, ...)

In this case you should simply do:

create_record(LineItemRecord, invoice_id: invoice_record.aggregate_id)

Instance Method Summary collapse

Methods included from Helpers::SelfApplier

#handle_message, included

Constructor Details

#initialize(record_session = Sequent::Core::RecordSessions::ActiveRecordSession.new) ⇒ BaseEventHandler

Returns a new instance of BaseEventHandler.



41
42
43
# File 'lib/sequent/core/base_event_handler.rb', line 41

def initialize(record_session = Sequent::Core::RecordSessions::ActiveRecordSession.new)
  @record_session = record_session
end