Discussion: Support for Refactoring use case for Events #1179
Replies: 4 comments
-
I would like to discuss here how to apply this or similar design to base gem. |
Beta Was this translation helpful? Give feedback.
-
Hi @andrzejsliwa, I'm new to RES and reading through the issues to see how some intricacies like this are handled. This is definitely a challenging issue - would you do this in lieu of database migrations? |
Beta Was this translation helpful? Give feedback.
-
On RES side there's:
Several techniques to consider listed in: |
Beta Was this translation helpful? Give feedback.
-
One more thing is having class Event < RubyEventStore::Event
module WithEventRegistry
module ClassMethods
def event_registry
@@event_registry ||= {}
end
def event_type=(value)
register_event_type(value, self)
@event_type = value
end
def event_type
@event_type
end
def register_event_type(event_type, event_klass)
raise "There is already an event of type #{event_type}" if event_registry.key?(event_type)
event_registry[event_type] = event_klass
end
end
module InstanceMethods
def event_type
self.class.event_type
end
def missing_event_type
<<~EOR
Missing event type.
You can set it with:
class #{self.class} < Event
self.event_type = "#{suggested_event_type}"
end
EOR
end
def suggested_event_type
self.class.name.underscore.gsub("/", ".")
end
end
module Constructor
def initialize(*)
self.class.event_type or raise missing_event_type
super
end
end
def self.included(klass)
klass.extend WithEventRegistry::ClassMethods
klass.include WithEventRegistry::InstanceMethods
klass.prepend WithEventRegistry::Constructor
end
end
include WithEventRegistry
end class Mapper < RubyEventStore::Mappers::PipelineMapper
class DomainEvent < RubyEventStore::Mappers::Transformation::DomainEvent
def load(record)
Event
.event_registry
.fetch(record.event_type)
.new(
event_id: record.event_id,
data: record.data,
metadata: record.metadata.merge(
timestamp: record.timestamp,
valid_at: record.valid_at,
),
)
end
end
def initialize
super(RubyEventStore::Mappers::Pipeline.new(
RubyEventStore::Mappers::Transformation::SymbolizeMetadataKeys.new,
to_domain_event: DomainEvent.new,
))
end
end specify "event_type independent from class name" do
event_class =
Class.new(Event) do
self.event_type = "definitely.not.a.class.name"
end
expect(event_class.new.event_type).to eq("definitely.not.a.class.name")
end |
Beta Was this translation helpful? Give feedback.
-
We have here few use cases:
In order to support both use case we have done example customizations:
In such use case we had to implement custom repository:
Configure using of this repository by our configuration:
by default version is equal 1 implicitly:
Beta Was this translation helpful? Give feedback.
All reactions