Class: Aggregates::AggregateRoot

Inherits:
Object
  • Object
show all
Includes:
MessageProcessor
Defined in:
lib/aggregates/aggregate_root.rb

Overview

An AggregateRoot is a central grouping of domain object(s) that work to encapsulate parts of our Domain or Business Logic.

The general design of aggregate roots should be as follows:

- Create functions that encapsulate different changes in your Aggregate Roots. These functions should enforce
constraints on the application. Then capture state changes by creating events.

- Create event handlers that actually performed the state changes captured by the events
  made by processing commands using the above functions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageProcessor

#handle_message, included, #invoke_handlers, #with_message_handlers

Constructor Details

#initialize(id, event_stream) ⇒ AggregateRoot

Creates a new instance of an aggregate root. This should not be called directly. Instead, it should be called by calling AggregateRoot.get_by_id. :reek:BooleanParameter



23
24
25
26
27
# File 'lib/aggregates/aggregate_root.rb', line 23

def initialize(id, event_stream)
  @id = id
  @sequence_number = 1
  @event_stream = event_stream
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/aggregates/aggregate_root.rb', line 18

def id
  @id
end

Instance Method Details

#apply(event, params = {}) ⇒ Object

Takes an event type and some parameters with which to create it. Then performs the following actions

1.) Builds the final event object.
2.) Processes the event locally on the aggregate.
3.) Produces the event on the event stream so that is saved by the storage backend and processed
    by the configured processors of the given type.


39
40
41
42
43
44
# File 'lib/aggregates/aggregate_root.rb', line 39

def apply(event, params = {})
  event = build_event(event, params)
  results = process_event(event)
  @event_stream.publish(event)
  results
end

#process_event(event) ⇒ Object



29
30
31
32
# File 'lib/aggregates/aggregate_root.rb', line 29

def process_event(event)
  @sequence_number += 1
  handle_message event
end