Command
Understanding CommandMessage and Command Abstract Class is crucial for sending instructions and triggering actions within your event-driven applications built with Evento Framework. These concepts allow you to define commands with target aggregates, potentially leverage locking mechanisms, and structure them within messages for effective communication.
Command Abstract Class:
Purpose: This class defines the core functionalities expected of a command object.
Inheritance: It typically extends the
TrackablePayload
class, ensuring commands adhere to basic payload functionalities (carrying data).Methods:
getAggregateId()
: Retrieves the ID of the aggregate that the command targets. This is crucial for routing the command to the appropriate aggregate for handling.getLockId()
(with potential@SuppressWarnings
annotation): Might retrieve a lock ID associated with the command for data consistency during execution (implementation details might vary).
Aggregate ID
The aggregateId
plays a crucial role in managing event streams and ensuring data consistency within your Event-Driven system. It acts as a unique identifier for an aggregate instance. This ID is used for two key purposes:
Retrieving Event Stream: By providing the
aggregateId
to the system's State Store, you can efficiently retrieve the complete sequence of events (event stream) that belongs to that specific aggregate. This allows you to reconstruct the current state of the aggregate by replaying all the events in the stream.Storing Generated Events: When a Domain Command is processed by an aggregate, it triggers the creation of new events that reflect the state change. These newly generated events are then stored within the system's State Store, but crucially, they are associated with the same
aggregateId
as the command that triggered them. This ensures that all events related to a particular aggregate instance are grouped together in the event stream, maintaining a clear audit trail and simplifying retrieval for later processing or rebuilding the aggregate's state.
In simpler terms, the aggregateId
acts like a reference point that connects commands, generated events, and the overall state of an aggregate, ensuring data consistency and organized event storage within your Event-Driven system.
CommandMessage:
Abstract Concept: There might not be a concrete
CommandMessage
class in Evento Framework.Functionality: The concept likely refers to the usage of the
Message
abstract class (discussed earlier) to create messages specifically for commands.Implementation: When sending a command, a developer would likely:
Create a concrete command object extending the
Command
class (e.g.,UpdateCustomerCommand
).Use this command object as the payload when constructing a
Message
object using theMessage
class constructor (new Message(commandObject)
).
Understanding the Relationship:
The
Command
class defines the expected structure and behavior of a command object.The
Message
class provides a way to encapsulate the command object (as the payload) along with other message details (type, timestamp, etc.) within a message structure.
Benefits of this approach:
Separation of Concerns: The
Command
class focuses on the command itself, while theMessage
class handles the overall message structure.Flexibility: Different command types can be implemented while adhering to the core functionalities defined in the
Command
class.Structured Communication: The
Message
class ensures commands are sent with additional context (type, timestamp) for better processing within the event-driven system.
Additional Considerations:
Evento Framework might have specific ways to handle serialization and deserialization of commands within
SerializedPayload
objects (not shown in the provided code snippets).There might be helper classes or annotations to simplify command creation and message construction.
Last updated