Last active
January 28, 2019 17:44
-
-
Save mariuszkapcia/ffe089dda41ece87b48a4ebca35f498e to your computer and use it in GitHub Desktop.
Basic AggregateRoot pattern implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyAggregateRoot | |
def load(stream_name, event_store:) | |
@version = -1 | |
event_store.read.stream(stream_name).forward.each do |event| | |
apply(event) | |
@version += 1 | |
end | |
@stream_name = stream_name | |
@unpublished_events = [] | |
end | |
def apply(event) | |
send("apply_#{event.class.to_s.demodulize.underscore}", event) | |
unpublished_events.push(event) | |
end | |
def store(event_store:) | |
unpublished_events.each do |event| | |
event_store.publish( | |
event, | |
stream_name: @stream_name, | |
expected_version: version | |
) | |
@version += 1 | |
end | |
@unpublished_events = [] | |
end | |
def unpublished_events | |
@unpublished_events ||= [] | |
end | |
def version | |
@version ||= -1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment