We created a CQRS-based system using a relational database on the domain side and a NoSQL database on the read side. The side of the domain follows the classical relational approach, while the side of the reading is denormalized. Data replication and transformation is performed using events generated by command handlers.
I have two questions regarding read-side synchronization:
What is the best way to completely rebuild the reading model using relational data on the domain side?
Suppose the reading model is not synchronized. But even if it is always synchronized, you can import a test database or perform several bulk operations. Therefore, it may be necessary to start the system from an existing recording model without having an appropriate synchronized reading model. Since we do not use Event Sourcing, it is not possible to play all events.
I am currently considering ReadModelBuilderwhich basically does SELECT * FROM for each table and converts each entity into a read-side view. But this introduces redundancy. ReadModelBuilder needs to know how the conversion is done. The same thing happens with event handlers, which usually perform read-side synchronization after the command handler has completed some write operations.
I was thinking of dropping event handlers and replacing them with a class-level synchronization mechanism. For example, instead of FooRenamedEventHandlerrenaming to foo.name, it will call FooReadModelBuilderwhich overwrites the full instance Foo. But I think this has flaws. FooRenamedEventHandler can do much better with overuse foo.namein the read model.
UPDATE: Another approach may be to allow ReadModelBuilderreading model objects to be created by segmenting the domain instance into events that, when executed sequentially, will create a complete object on the reading side. For instance:
Article Name Price. , ReadModelBuilder ArticleCreatedEvent, ArticleRenamedEvent ArticlePriceChangedEvent. , , .
, ReadModelBuilders :
_
interface IReadModelBuilder<TEntity>
{
Event[] GetReplicationSequence(TEntity instance);
}
_