Hands-on DDD featuring Event Storming, CQRS, Event Sourcing in .NET — Part 5 End-to-end testing and Replay Events🎥🚀

Chaitanya (Chey) Penmetsa
CodeNx
Published in
4 min readFeb 12, 2024

In this blog we will run both command and query API we built in last 2 blogs and see how the data will be flowing as well as replaying events for rebuilding an aggregate from scratch.

For reading previous blogs on Command and Query API please see below links.

For running both API’s you can set them as multi project startup in solution settings in visual studio or you can add both the API’s into launch settings in VS code.

Testing Commands

First let us send “CreateLease” command using below payload

{
"id": "8e5adbc9-8eee-416b-9fd9-3f7f67e346e6",
"customerEmails": [
"jd@gmail.com",
"jds@gmail.com"
],
"floorPlan": "Glorious",
"leaseStartDate": "2024-04-05T18:02:35.773Z",
"leaseTermLengthInMonths": 12,
"rentAmount": 1255,
"parkingSpace": "",
"apartmentNumber": "20 101"
}

Our write database which is mongo is updated by the event.

At same time we got an event published and read database data gets populated by consumer part as shown below.

As we keep sending commands like “Send Lease”, “Sign Lease”, and “Edit Lease” you can see events get appended to our event store as shown below.

But on our read database we will only have single record which gets updated as events gets published.

Replay Events

Now we will show most powerful feature of using event store, let us say the lease entity got deleted from read database. Now all we have to do was replay all the events associated for that aggregate.

Upon triggering the Command API with the “replay” command, it will retrieve all events linked to that aggregate and transmit them to the consumer. The consumer then processes all events, constructing the entity. This illustrates the effectiveness of event sourcing and the event store. You might question whether such complexity is necessary for small projects. In my opinion, the answer is no. It’s crucial to assess microservices carefully and decide if implementing CQRS and Event Sourcing is warranted for your project.

Complete code can be found here:

With that we conclude this blog series and see you in next blog with more exciting content.

🙏Thanks for taking the time to read the article. If you found it helpful and would like to show support, please consider:

  1. 👏👏👏👏👏👏Clap for the story and bookmark for future reference
  2. Follow me on Chaitanya (Chey) Penmetsa for more content
  3. Stay connected on LinkedIn.

Wishing you a happy learning journey 📈, and I look forward to sharing new articles with you soon.

--

--

Chaitanya (Chey) Penmetsa
CodeNx
Editor for

👨🏽‍💻Experienced and passionate software enterprise architect helping solve real-life business problems with innovative, futuristic, and economical solutions.