Messages
Commands and events are the two message types in Protean. Both are immutable DTOs that carry data and tracing metadata.
Guides: Commands · Events · Raising Events
BaseCommand
BaseCommand(*args: Any, **kwargs: Any)
Bases: BaseMessageType
Base class for domain commands. A command is an immutable message that asks an aggregate to change its state.
Name a command with an imperative verb (PlaceOrder, RegisterUser).
A command handler processes it. Declare fields with type annotations and
optional Field constraints. A command cannot be changed after it is
built. It carries tracing metadata: correlation ID, causation ID and origin
stream. The Commands guide covers usage.
Meta Options
| Option | Type | Description |
|---|---|---|
part_of |
type |
The aggregate or process manager this command targets. Required. |
version |
int |
Message version number (alternative to __version__ class attribute). Default 1. |
Create a new command instance.
Accepts keyword arguments matching the declared fields. Optionally,
a dict can be passed as a positional argument to serve as a
template. Keyword arguments take precedence over template values.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Optional template dictionaries for field values.
TYPE:
|
**kwargs
|
Field values for the command.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If field validation fails. |
Example::
# Keyword arguments
PlaceOrder(order_id="abc", amount=100)
# Template dict pattern
PlaceOrder({"order_id": "abc"}, amount=100)
Source code in src/protean/core/command.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
BaseEvent
BaseEvent(*args: Any, **kwargs: Any)
Bases: BaseMessageType
Base class for domain events, immutable facts about state changes that have happened in the domain.
Name events in the past tense (OrderPlaced, CustomerRegistered).
An event cannot be changed after it is created. It carries metadata for
tracing: correlation ID, causation ID and stream position.
Fields are declared with Python type annotations and optional Field
constraints. See the events guide.
Meta Options
| Option | Type | Description |
|---|---|---|
part_of |
type |
The aggregate or process manager that raises this event. Required. |
published |
bool |
Whether this event is part of the bounded context's published language. Default False. |
version |
int |
Message version number (alternative to __version__ class attribute). Default 1. |
Create a new event instance.
Accepts keyword arguments matching the declared fields. Optionally,
a dict can be passed as a positional argument to serve as a
template. Keyword arguments take precedence over template values.
Events are typically created by calling self.raise_() inside an
aggregate method rather than being instantiated directly.
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Optional template dictionaries for field values.
TYPE:
|
**kwargs
|
Field values for the event.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If field validation fails. |
Example::
# Raised from an aggregate method
self.raise_(OrderPlaced(order_id=self.id, amount=self.total))
# Template dict pattern (e.g. during reconstitution)
OrderPlaced({"order_id": "abc"}, amount=100)
Source code in src/protean/core/event.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |