-
Notifications
You must be signed in to change notification settings - Fork 126
Closed
Description
Theoretically can happen situation when you would like to move aggregate between namespaces or you choose wrong name for it. We don't want to modify our source of true (event store), but we are using aggregate names as part of identification of stream.
In such use case this example code lets to re-define name used for constructing stream id:
module Loans
class Loan
include Infra::AggregateRoot
include ::Base::Contracts
set_aggregate_name 'Loans::Loan'
...
end
endmodule Infra
module AggregateRoot
extend ActiveSupport::Concern
included do
include ::AggregateRoot
extend AggregateName
end
module AggregateName
# rubocop:disable Style/AccessorMethodName
def set_aggregate_name(aggregate_name)
@aggregate_name = aggregate_name
end
def aggregate_name
@aggregate_name
end
end
end
endmodule Infra
module WithAggregate
include Contracts::Core
include Contracts::Builtin
private
def with_aggregate(aggregate_class, aggregate_id, **args)
aggregate = build(aggregate_class, aggregate_id, **args)
yield aggregate
aggregate.store
aggregate
end
def build(aggregate_class, aggregate_id, **args)
name = aggregate_class.aggregate_name || aggregate_class.name
stream = "#{name}$#{aggregate_id}"
aggregate_class.new(aggregate_id: aggregate_id, **args)
.load(stream, event_store: Rails.configuration.core.event_store)
end
end
endMetadata
Metadata
Assignees
Labels
No labels