Skip to content

Discussion: Support for Refactoring use case for Aggregate #112

@andrzejsliwa

Description

@andrzejsliwa

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
end
module 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
end
module 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
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions