Domain Command
This chapter dives into the concept of Domain Commands within Evento Framework, a key element for triggering actions and modifications related to your domain objects in an event-driven architecture.
Understanding Domain Commands
Domain commands represent a specific type of command used to interact with domain objects in your application. Domain objects, referred to as Aggregates, encapsulate domain logic and data pertaining to a particular entity or concept within your problem domain.
Here's what characterizes a Domain Command:
Focus: Domain commands are designed to be processed by the aggregates themselves.
Purpose: They trigger actions or modifications on the state of the targeted aggregate.
Functionality: Commands typically carry data within their payload that specifies the desired action and any necessary information for the aggregate to perform the change.
Benefits of Using Domain Commands
Utilizing domain commands offers several advantages:
Separation of Concerns: It promotes a clean separation between domain logic (handled by aggregates) and infrastructure concerns (like message routing and persistence).
Structured Communication: Domain commands enforce a consistent structure for interacting with aggregates. This includes the target aggregate ID for routing and potential default locking mechanisms.
Domain-Centric Design: Domain commands encourage a focus on domain concepts and behavior, promoting a clear understanding of your domain model.
Reusability: By extending the base
Command
interface, domain commands leverage existing functionalities while adding domain-specific requirements.
Implementing a Domain Command
Evento Framework utilizes an abstract class to define the structure of Domain Commands. Here's a typical breakdown of a Domain Command class:
Command
Extension: This interface inherits functionalities from the baseCommand
interface, likely including a method to access the payload object.getAggregateId()
: This method is crucial for routing the command to the appropriate aggregate for handling. It retrieves the ID of the target aggregate.getLockId()
(Optional): This method might be used for optimistic locking purposes. By default, it might returngetAggregateId()
to use a locking strategy based on the single aggregate.Cache Invalidation Control: The
invalidateAggregateCaches
andinvalidateAggregateSnapshot
flags provide fine-grained control over cache invalidation behavior.
Concrete Domain Command Implementations:
While the provided code snippet defines an abstact, actual usage involves creating concrete implementations extending DomainCommand
. These implementations represent specific actions you want to perform on your domain objects. Here's an example:
This UpdateCustomerCommand
extends DomainCommand
and specifies the customer ID and new email address within its payload. The getAggregateId()
method returns the customer ID, allowing the command to be routed to the appropriate customer aggregate for processing.
Cache Invalidation Mechanism
The invalidateAggregateCaches
flag indicates whether the caches associated with the target aggregate should be invalidated. When set to true
, the system should invalidate any cached data related to the aggregate after the command is processed. This ensures that subsequent queries retrieve the most up-to-date data.
The invalidateAggregateSnapshot
flag determines whether the aggregate's snapshot should be invalidated. A snapshot is a persistent representation of the aggregate's state at a specific point in time. By invalidating the snapshot, you force the system to rebuild it from the event stream when it's next requested.
Best Practices
Strategic Use of Flags: Carefully consider when to set the
invalidateAggregateCaches
andinvalidateAggregateSnapshot
flags. Excessive cache invalidation can impact performance.Cache Implementation: The specific implementation of cache invalidation will depend on your caching strategy.
Snapshot Management: Implement a mechanism to efficiently create and manage aggregate snapshots.
Error Handling: Consider error handling for situations where cache invalidation fails.
By effectively utilizing the cache invalidation features of DomainCommand
, you can optimize system performance and ensure data consistency.
Last updated