Class: Synapse::Repository::LockingRepository Abstract

Inherits:
Synapse::Repository show all
Defined in:
lib/synapse/repository/locking.rb

Overview

This class is abstract.

Partial implementation of a repository that handles integration with a lock manager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lock_manager) ⇒ undefined

Parameters:



11
12
13
14
# File 'lib/synapse/repository/locking.rb', line 11

def initialize(lock_manager)
  @lock_manager = lock_manager
  @logger = Logging.logger[self.class]
end

Instance Attribute Details

#lock_managerLockManager (readonly)

Returns:



7
8
9
# File 'lib/synapse/repository/locking.rb', line 7

def lock_manager
  @lock_manager
end

Instance Method Details

#add(aggregate) ⇒ undefined

Parameters:

  • aggregate (AggregateRoot)

Returns:

  • (undefined)

Raises:

  • (ArgumentError)

    If the version of the aggregate is not null



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/synapse/repository/locking.rb', line 45

def add(aggregate)
  @lock_manager.obtain_lock aggregate.id

  begin
    assert_compatible aggregate

    register_aggregate aggregate
    register_listener LockCleaningUnitOfWorkListener.new aggregate.id, @lock_manager
  rescue
    @logger.debug 'Exception raised while adding an aggregate, releasing lock'

    @lock_manager.release_lock aggregate.id
    raise
  end
end

#load(aggregate_id, expected_version = nil) ⇒ AggregateRoot

Parameters:

  • aggregate_id (Object)
  • expected_version (Integer) (defaults to: nil)

    If this is nil, no version validation is performed

Returns:

  • (AggregateRoot)

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/synapse/repository/locking.rb', line 24

def load(aggregate_id, expected_version = nil)
  @lock_manager.obtain_lock aggregate_id

  begin
    aggregate = perform_load aggregate_id, expected_version

    register_aggregate(aggregate).tap do
      register_listener LockCleaningUnitOfWorkListener.new aggregate_id, @lock_manager
    end
  rescue
    @logger.debug 'Excepton raised while loading an aggregate, releasing lock'

    @lock_manager.release_lock aggregate_id
    raise
  end
end