-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Domain events vs infrastructure events
Reimplemented the Payments::Order aggregate to use domain events which are simple Data structures instead of infrastructure events carrying all the information like metadata and data containing primitve types. It required additional work on repository to map between domain and infra events when loading and storing the aggregate.
- Loading branch information
Showing
7 changed files
with
213 additions
and
59 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ environment_variables: | |
matcher: | ||
subjects: | ||
- Payments::Order | ||
- Payments::OrderRepository | ||
- Payments::Amount |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,69 @@ | ||
module Payments | ||
class OrderRepository | ||
def initialize(event_store = Rails.configuration.event_store) | ||
@repository = AggregateRoot::Repository.new(event_store) | ||
@event_store = event_store | ||
end | ||
|
||
private attr_reader :repository | ||
private attr_reader :event_store | ||
|
||
def with_order(order_id, &block) | ||
repository.with_aggregate(Order.new(order_id), stream_name(order_id), &block) | ||
def load(order) | ||
event_store | ||
.read | ||
.stream(stream_name(order.order_id)) | ||
.reduce { |_, event| order.apply(infra_to_domain_mapper(event)) } | ||
order.version = order.unpublished_events.count - 1 | ||
order | ||
end | ||
|
||
def store(order) | ||
event_store.publish( | ||
order.unpublished_events.map(&method(:domain_to_infra_mapper)), | ||
stream_name: stream_name(order.order_id), | ||
expected_version: order.version, | ||
) | ||
order.version = order.version + order.unpublished_events.count | ||
end | ||
|
||
private | ||
|
||
def stream_name(order_id) | ||
"Payments::Order$#{order_id}" | ||
end | ||
|
||
def infra_to_domain_mapper(event) | ||
{ | ||
"Infra::Payments::PaymentRequested" => | ||
PaymentRequested.new( | ||
order_id: event.data.fetch(:order_id), | ||
amount: Amount.new(value: event.data.fetch(:amount_value), currency: event.data.fetch(:amount_currency)), | ||
), | ||
"Infra::Payments::PaymentRegistered" => | ||
PaymentRegistered.new( | ||
order_id: event.data.fetch(:order_id), | ||
amount: Amount.new(value: event.data.fetch(:amount_value), currency: event.data.fetch(:amount_currency)), | ||
), | ||
}.fetch(event.event_type) | ||
end | ||
|
||
def domain_to_infra_mapper(event) | ||
{ | ||
"Payments::PaymentRequested" => | ||
Infra::Payments::PaymentRequested.new( | ||
data: { | ||
order_id: event.order_id, | ||
amount_value: event.amount.value, | ||
amount_currency: event.amount.currency, | ||
}, | ||
), | ||
"Payments::PaymentRegistered" => | ||
Infra::Payments::PaymentRegistered.new( | ||
data: { | ||
order_id: event.order_id, | ||
amount_value: event.amount.value, | ||
amount_currency: event.amount.currency, | ||
}, | ||
), | ||
}.fetch(event.event_type) | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Infra | ||
module Payments | ||
PaymentRequested = Class.new(RailsEventStore::Event) | ||
PaymentRegistered = Class.new(RailsEventStore::Event) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require "test_helper" | ||
require "minitest/mock" | ||
|
||
module Payments | ||
class OrderRepositoryTest < ActiveSupport::TestCase | ||
cover OrderRepository | ||
|
||
test "infra events are translated to domain events and applied on load" do | ||
event_store.publish( | ||
[ | ||
Infra::Payments::PaymentRequested.new( | ||
data: { | ||
order_id: "order-1", | ||
amount_value: 100, | ||
amount_currency: "EUR", | ||
}, | ||
), | ||
Infra::Payments::PaymentRegistered.new( | ||
data: { | ||
order_id: "order-1", | ||
amount_value: 100, | ||
amount_currency: "EUR", | ||
}, | ||
), | ||
], | ||
stream_name: "Payments::Order$order-1", | ||
) | ||
|
||
order = Minitest::Mock.new(Order.new("order-1")) | ||
order.expect(:apply, nil, [PaymentRequested.new(order_id: "order-1", amount: Amount.new(100, "EUR"))]) | ||
order.expect(:apply, nil, [PaymentRegistered.new(order_id: "order-1", amount: Amount.new(100, "EUR"))]) | ||
|
||
OrderRepository.new.load(order) | ||
|
||
order.verify | ||
end | ||
|
||
test "no unpublished events on fresh aggregate instance" do | ||
refute OrderRepository.new.load(Order.new("order-1")).unpublished_events.any? | ||
end | ||
|
||
test "aggregate version is set accordingly" do | ||
event_store.publish( | ||
Infra::Payments::PaymentRequested.new(data: { order_id: "order-1", amount_value: 100, amount_currency: "EUR" }), | ||
stream_name: "Payments::Order$order-1", | ||
) | ||
order = OrderRepository.new.load(Order.new("order-1")) | ||
|
||
assert_equal(0, order.version) | ||
end | ||
|
||
test "aggregate version is set for given instance" do | ||
event_store.publish( | ||
Infra::Payments::PaymentRequested.new(data: { order_id: "order-1", amount_value: 100, amount_currency: "EUR" }), | ||
stream_name: "Payments::Order$order-1", | ||
) | ||
event_store.publish( | ||
Infra::Payments::PaymentRequested.new(data: { order_id: "order-1", amount_value: 100, amount_currency: "EUR" }), | ||
stream_name: "dummy", | ||
) | ||
order = OrderRepository.new.load(Order.new("order-1")) | ||
|
||
assert_equal(0, order.version) | ||
end | ||
|
||
test "store translates domain events to infra ones" do | ||
event_store = Minitest::Mock.new | ||
event_store.expect(:publish, nil) do |events, stream_name:, expected_version:| | ||
assert_equal_event( | ||
Infra::Payments::PaymentRequested.new( | ||
data: { | ||
order_id: "order-1", | ||
amount_value: 100, | ||
amount_currency: "EUR", | ||
}, | ||
), | ||
events[0], | ||
) | ||
assert_equal_event( | ||
Infra::Payments::PaymentRegistered.new( | ||
data: { | ||
order_id: "order-1", | ||
amount_value: 100, | ||
amount_currency: "EUR", | ||
}, | ||
), | ||
events[1], | ||
) | ||
assert_equal("Payments::Order$order-1", stream_name) | ||
assert_equal(-1, expected_version) | ||
end | ||
|
||
order = Order.new("order-1") | ||
order.apply(PaymentRequested.new(order_id: "order-1", amount: Amount.new(100, "EUR"))) | ||
order.apply(PaymentRegistered.new(order_id: "order-1", amount: Amount.new(100, "EUR"))) | ||
|
||
OrderRepository.new(event_store).store(order) | ||
|
||
event_store.verify | ||
|
||
assert_equal(1, order.version) | ||
end | ||
end | ||
end |
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