Module: Synapse::Domain::AggregateRoot
- Included in:
- EventSourcing::AggregateRoot
- Defined in:
- lib/synapse/domain/aggregate_root.rb
Overview
Mixin module for a basic aggregate root that is not event-sourced
The persistence mechanism is left up to the aggregate root that uses this mixin. Any sort of ORM can be used to persist aggregates.
If optimistic locking is used, the ORM must increment the version field before saving.
class Order
include Synapse::Domain::AggregateRoot
include MongoMapper::Document
key :version, Integer
before_save { self.version += 1 }
end
Instance Attribute Summary collapse
-
#deleted ⇒ Boolean
(also: #deleted?)
readonly
True if this aggregate has been marked for deletion.
-
#id ⇒ Object
readonly
The identifier of this aggregate.
-
#version ⇒ Integer
readonly
The version of this aggregate.
Instance Method Summary collapse
-
#add_registration_listener(&listener) ⇒ undefined
Adds a listener that will be notified when this aggregate registers an event to be published.
-
#mark_committed ⇒ undefined
Marks this aggregate as committed by a repository.
-
#uncommitted_event_count ⇒ Integer
Returns the number of uncommitted events published by this aggregate.
-
#uncommitted_events ⇒ DomainEventStream
Returns a domain event strema containing any uncommitted events published by this aggregate.
Instance Attribute Details
#deleted ⇒ Boolean (readonly) Also known as: deleted?
Returns True if this aggregate has been marked for deletion.
22 23 24 |
# File 'lib/synapse/domain/aggregate_root.rb', line 22 def deleted @deleted end |
#id ⇒ Object (readonly)
Returns The identifier of this aggregate.
27 28 29 |
# File 'lib/synapse/domain/aggregate_root.rb', line 27 def id @id end |
#version ⇒ Integer (readonly)
Returns The version of this aggregate.
30 31 32 |
# File 'lib/synapse/domain/aggregate_root.rb', line 30 def version @version end |
Instance Method Details
#add_registration_listener(&listener) ⇒ undefined
Adds a listener that will be notified when this aggregate registers an event to be published
If an event registration listener is added after events have already been registered, it will still get a change to process the uncommitted events in this aggregate.
75 76 77 |
# File 'lib/synapse/domain/aggregate_root.rb', line 75 def add_registration_listener(&listener) event_container.add_registration_listener listener end |
#mark_committed ⇒ undefined
Marks this aggregate as committed by a repository
36 37 38 39 40 41 |
# File 'lib/synapse/domain/aggregate_root.rb', line 36 def mark_committed if @event_container @last_sequence_number = @event_container.last_sequence_number @event_container.mark_committed end end |
#uncommitted_event_count ⇒ Integer
Returns the number of uncommitted events published by this aggregate
47 48 49 50 51 52 53 |
# File 'lib/synapse/domain/aggregate_root.rb', line 47 def uncommitted_event_count unless @event_container return 0 end @event_container.size end |
#uncommitted_events ⇒ DomainEventStream
Returns a domain event strema containing any uncommitted events published by this aggregate
59 60 61 62 63 64 65 |
# File 'lib/synapse/domain/aggregate_root.rb', line 59 def uncommitted_events unless @event_container return SimpleDomainEventStream.new end @event_container.to_stream end |