I am building an application and trying to embrace Domain Driven design principles, CQRS and event sourcing.
We are having two applications one for reading and another for writing. The write application is using a event store data base. And after it completes a transaction on a aggregate, it publishes a domain event to the read application.
There is a need for both the read and write applications to get events and build the aggregate to its current state by applying the events.
- The write application does this in the repository when a aggregate is queried by the id.
- The read application does this in the domain event handler when it receives the event from the write application.
There is a lot of common code between the read application and write application especially for the building the aggregates and applying the events on aggregates.
The problem I am trying to solve is remove the duplicate code. I had some options in my mind, I have a fair understanding of the merits and demerits of these options. I am not sure if I am overlooking at something. Also trying to understand if there is no other easier way to do it. The options I have are as below
Option 1: Have a shared library that is used by read application and write application. Now there will be 3 code repositories. The independent deployability is partly lost here.
Option 2: Have one common code base for read and write application. Have two deployments for read application and write application by having different configurations. The independent deployability is lost here.
Option 3: The write application has the aggregate's state building logic. When the read application gets any event about an aggregate. It queries ( via http) the write application and builds the models
Option 4: I am doing something fundamentally wrong in CQRS. That is the reason for duplicated code. So I need to revisit concept.