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

Instance Method Summary collapse

Instance Attribute Details

#deletedBoolean (readonly) Also known as: deleted?

Returns True if this aggregate has been marked for deletion.

Returns:

  • (Boolean)

    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

#idObject (readonly)

Returns The identifier of this aggregate.

Returns:

  • (Object)

    The identifier of this aggregate



27
28
29
# File 'lib/synapse/domain/aggregate_root.rb', line 27

def id
  @id
end

#versionInteger (readonly)

Returns The version of this aggregate.

Returns:

  • (Integer)

    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.

Parameters:

  • listener (Proc)

Returns:

  • (undefined)


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_committedundefined

Marks this aggregate as committed by a repository

Returns:

  • (undefined)


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_countInteger

Returns the number of uncommitted events published by this aggregate

Returns:

  • (Integer)


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_eventsDomainEventStream

Returns a domain event strema containing any uncommitted events published by this aggregate

Returns:



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