class Synapse::Repository::Repository

Represents a mechanism for loading and storing aggregates @abstract

Attributes

event_bus[RW]

@return [EventBus]

unit_provider[RW]

@return [UnitOfWorkProvider]

Public Instance Methods

add(aggregate) click to toggle source

Adds a new, unmanaged aggregate to the repository

This method will not force the repository to save the aggregate immediately. Instead, it is registered with the current unit of work. To force storage of an aggregate, commit the current unit of work.

@abstract @raise [ArgumentError] If the version of the aggregate is not null @param [AggregateRoot] aggregate @return [undefined]

# File lib/synapse/repository/repository.rb, line 43
def add(aggregate)
  raise NotImplementedError
end
load(aggregate_id, expected_version = nil) click to toggle source

Loads an aggregate with the given aggregate identifier

If an expected version is specified and the aggregate's actual version doesn't equal the expected version, the implementation can choose to do one of the following:

  • Raise an exception immediately

  • Raise an exception at any other time while the aggregate is registered with the current unit of work.

@abstract @raise [AggregateNotFoundError]

If the aggregate with the given identifier could not be found

@raise [ConflictingModificationError]

If the expected version doesn't match the aggregate's actual version

@param [Object] aggregate_id @param [Integer] expected_version If this is nil, no version validation is performed @return [AggregateRoot]

# File lib/synapse/repository/repository.rb, line 29
def load(aggregate_id, expected_version = nil)
  raise NotImplementedError
end

Protected Instance Methods

aggregate_type() click to toggle source

Returns the type of aggregate that this repository handles

@abstract @return [Class]

# File lib/synapse/repository/repository.rb, line 53
def aggregate_type
  raise NotImplementedError
end
assert_compatible(aggregate) click to toggle source

Asserts that an aggregate being added is compatible with this repository and is newly created

@raise [ArgumentError] If aggregate is not of the correct type @raise [ArgumentError] If aggregate has a version number @param [AggregateRoot] aggregate @return [undefined]

# File lib/synapse/repository/repository.rb, line 82
def assert_compatible(aggregate)
  unless aggregate.is_a? aggregate_type
    raise ArgumentError, 'Incompatible aggregate type'
  end

  if aggregate.version
    raise ArgumentError, 'Only newly created aggregates may be added'
  end
end
assert_version_expected(aggregate, expected_version) click to toggle source

Asserts that a loaded aggregate has the expected version

@raise [ConflictingAggregateVersionError]

If aggregate version is later than the expected version

@param [AggregateRoot] aggregate @param [Integer] expected_version @return [undefined]

# File lib/synapse/repository/repository.rb, line 99
def assert_version_expected(aggregate, expected_version)
  if expected_version && aggregate.version && aggregate.version > expected_version
    raise ConflictingAggregateVersionError.new aggregate, expected_version
  end
end
current_unit() click to toggle source

@return [UnitOfWork]

# File lib/synapse/repository/repository.rb, line 128
def current_unit
  @unit_provider.current
end
delete_aggregate(aggregate) click to toggle source

Deletes the given aggregate from the underlying storage mechanism

@abstract @param [AggregateRoot] aggregate @return [undefined]

# File lib/synapse/repository/repository.rb, line 62
def delete_aggregate(aggregate)
  raise NotImplementedError
end
register_aggregate(aggregate) click to toggle source

Registers the given aggregate with the current unit of work

@param [AggregateRoot] aggregate @return [undefined]

# File lib/synapse/repository/repository.rb, line 109
def register_aggregate(aggregate)
  current_unit.register_aggregate aggregate, @event_bus do |aggregate|
    if aggregate.deleted?
      delete_aggregate aggregate
    else
      save_aggregate aggregate
    end
  end
end
register_listener(listener) click to toggle source

Registers the given unit of work listener with the current unit of work

@param [UnitOfWorkListener] listener @return [undefined]

# File lib/synapse/repository/repository.rb, line 123
def register_listener(listener)
  current_unit.register_listener listener
end
save_aggregate(aggregate) click to toggle source

Saves the given aggregate using the underlying storage mechanism

@abstract @param [AggregateRoot] aggregate @return [undefined]

# File lib/synapse/repository/repository.rb, line 71
def save_aggregate(aggregate)
  raise NotImplementedError
end