How montarelab/microservices-cqrs-eventsource works
Main Components
• Command Side (Write Operations):
• Command API (located in the Post.Cmd project) which receives commands such as NewPostCommand, LikePostCommand, etc.
• Command Handlers that implement business logic.
• Aggregate Root and domain logic that ensure consistency.
• Event Sourcing mechanisms that write state changes as events.
• Event Store implemented with MongoDB.
• Event Producers that publish events to Kafka.
• Query Side (Read Operations):
• Query API (from the Post.Query project) which exposes endpoints for fetching data.
• Query Handlers that process incoming queries and fetch data from the read database.
• A denormalized Read Model stored in MSSQL.
• Event Consumers that pick up events from Kafka and update the read model.
• Messaging and Integration:
• Kafka used as the central message bus for sending and consuming domain events.
• Data flows between the Command and Query sides via published events, ensuring eventual consistency.
File Structure
• The top-level structure under the “src” directory clearly distinguishes the core libraries (CQRS.Core) from the service-specific projects (SM-POST which includes Post.Cmd, Post.Query, Post.Common, etc.).
• Directories such as “Commands”, “Events”, “Consumers”, “Handlers”, and “Producers” indicate a strong pursuit of the CQRS pattern with event-driven behavior.
• Configuration files (e.g., appsettings.json for both Command and Query APIs, docker-compose.yml) emphasize deployment and orchestration using Docker Compose.
• The presence of infrastructure directories indicates explicit roles (e.g., command dispatchers, event sourcing handlers).
Detail the Relationships and Interactions
• A client sends a command to the Command API. The command is processed by the command handler which enforces business logic.
• After processing, the event reflecting the state change is stored in the MongoDB-based Event Store.
• The event is simultaneously published onto Kafka.
• Kafka then forwards these events to the Query side where consumers pick them up.
• Query Handlers update the MSSQL-based Read Database accordingly.
• When a read request (query) comes in, the Query API fetches information from the updated, denormalized database and returns it to the client.
Architectural Patterns & Design Principles
• CQRS: Splitting the system into separate models for write and read operations.
• Event Sourcing: Capturing all changes as immutable events.
• Microservices: Distinct projects/microservices (command and query) communicating via Kafka.
• Dependency Injection & MediatR usage to dispatch commands and queries.
• Clear separation of concerns across layers (domain, infrastructure, API).