Hi,
I have a very dynamic application where users can build custom data types (I call them schema). The schema definition is an immutable domain object:
e.g.
class Schema
{
private Schema(string name) { }
public Schema Create(string name)
{
CheckNameIsValid();
return new Schema(name);
}
public Schema Rename(string newName)
{
CheckNameIsValid();
return new Schema(newName);
}
}
public void On(SchemaCreated @event)
{
schema = Schema.Create(@event.Name);
}
public void On(SchemaRenamed @event)
{
schema = schema.Rename(@event.NewName);
}
There are similar constructs for TenantConfiguration and so on.
I need this classes a lot in the write part (e.g. you create a content for a Schema) or in the read part (e.g. GrapQL queries for schema). I use the CQRS approach because Event Sourcing gives me and the users a lot of flexibility to react to changes that happen in the system.
What I do at the moment is to recreate the same objects in my denormalizers. It is good, because events are very small and I have this nice domain object which I can use everywhere, but it also introduces same problems:
1. Events do not contain all important information, e.g. the full schema. So when you consume the event to send it to a webhook you might need to query other information, which makes the integration more complicated.
2. There is a lot of duplicated code.
Do you think it is a bad idea to create self contained events? I am little bit afraid about incompatibilities when something changes, e.g. a refactoring of the Schema class.
Sebastian
Btw: CQRS might be just the wrong approach here and normal domain events would be better ;)