Class: Synapse::UnitOfWork::NestableUnitOfWork Abstract
- Inherits:
-
Object
- Object
- Synapse::UnitOfWork::NestableUnitOfWork
- Defined in:
- lib/synapse/uow/nesting.rb
Overview
Partial implementation of a unit of work that can be nested
This implementation provides common actions that will be needed for nestable units of work, such as registration with the unit of work provider and nested commit and rollback operations
Direct Known Subclasses
Instance Method Summary collapse
-
#commit ⇒ undefined
Commits this unit of work.
- #initialize(provider) ⇒ undefined constructor
-
#rollback(cause = nil) ⇒ undefined
Clears this unit of work of any buffered changes.
-
#start ⇒ undefined
Starts the unit of work, preparing it for aggregate registration.
-
#started? ⇒ Boolean
Returns true if this unit of work has been started.
Constructor Details
#initialize(provider) ⇒ undefined
13 14 15 16 17 |
# File 'lib/synapse/uow/nesting.rb', line 13 def initialize(provider) @inner_units = Array.new @provider = provider @started = false end |
Instance Method Details
#commit ⇒ undefined
Commits this unit of work
All reigstered aggregates that have not been registered as stored are saved in their respective repositories, buffered events are sent to their respective event buses, and all registered listeners are notified of the commit.
After the commit (successful or not), the unit of work is unregistered and cleans up any resources it acquired. The effectively means that a rollback is done if the unit of work failed to commit.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/synapse/uow/nesting.rb', line 32 def commit unless started? raise 'Unit of work has not been started yet' end begin notify_prepare_commit store_aggregates unless @outer_unit perform_commit stop perform_cleanup end rescue => cause perform_rollback cause stop unless @outer_unit perform_cleanup end raise cause ensure clear end end |
#rollback(cause = nil) ⇒ undefined
Clears this unit of work of any buffered changes
Any buffered events and registered aggregates are discarded and any registered unit of work listeners are notified of the rollback.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/synapse/uow/nesting.rb', line 68 def rollback(cause = nil) begin if started? @inner_units.each do |inner_unit| @provider.push inner_unit inner_unit.rollback cause end perform_rollback cause end ensure finalize_rollback end end |
#start ⇒ undefined
Starts the unit of work, preparing it for aggregate registration
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/synapse/uow/nesting.rb', line 87 def start if started? raise 'Unit of work has already been started' end perform_start if @provider.started? # This is a nested unit of work @outer_unit = @provider.current if NestableUnitOfWork === @outer_unit @outer_unit.register_inner_unit self else # Outer unit is not aware of inner units, hook in with a listener @outer_unit.register_listener OuterCommitUnitOfWorkListener.new self, @provider end end @provider.push self @started = true end |
#started? ⇒ Boolean
Returns true if this unit of work has been started
114 115 116 |
# File 'lib/synapse/uow/nesting.rb', line 114 def started? @started end |