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 -- immutable DTOs representing an intent to change aggregate state.
Commands are named with imperative verbs (PlaceOrder,
RegisterUser, CancelReservation) and processed by command handlers.
They are immutable after construction and carry metadata for tracing
(correlation ID, causation ID, origin stream).
Fields are declared using standard Python type annotations with optional
Field constraints.
Meta Options
| Option | Type | Description |
|---|---|---|
part_of |
type |
The aggregate class this command targets. Required. |
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
56 57 58 59 60 61 62 63 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 | |
BaseEvent
BaseEvent(*args: Any, **kwargs: Any)
Bases: BaseMessageType
Base class for domain events -- immutable facts representing state changes that have occurred in the domain.
Events are named in past tense (OrderPlaced, CustomerRegistered,
PaymentConfirmed) and enable decoupled communication between system
components. They are immutable after construction and carry metadata for
tracing (correlation ID, causation ID, stream position).
Fields are declared using standard Python type annotations with optional
Field constraints.
Meta Options
| Option | Type | Description |
|---|---|---|
part_of |
type |
The aggregate class that raises this event. Required. |
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
59 60 61 62 63 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 | |