Message¶
message
¶
Message module for messaging patterns.
This module provides the base Message class and MessageMetadata for implementing
foundation messages influenced by Domain-Driven Design (DDD) and CQRS principles.
MessageMetadata
¶
Bases: ValueObject[dict[str, object]]
Metadata associated with foundational messages.
Contains technical-level information about messages such as:
- Unique message identifier
- When the message was created
- correlation_id is used to trace related messages across systems.
- correlation_id is used to link messages that belong to the same business process.
- causation_id is used to identify the immediate predecessor message that caused
This separation allows messages to focus on foundational data while keeping
infrastructure handling concerns in metadata without foundation understand anything about
infrastructure rules.
Example
Source code in src/forging_blocks/foundation/messages/message.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
message_id: UUID
property
¶
Get the unique identifier for this message.
Returns:
| Type | Description |
|---|---|
UUID
|
The unique message identifier. |
causation_id: UUID
property
¶
Get the causation ID for this message.
Returns:
| Type | Description |
|---|---|
UUID
|
The causation identifier. |
created_at: datetime
property
¶
Get the timestamp when this message was created.
Returns:
| Type | Description |
|---|---|
datetime
|
When the message was created (UTC timezone). |
correlation_id: UUID
property
¶
Get the correlation ID for this message.
Returns:
| Type | Description |
|---|---|
UUID
|
The correlation identifier. |
message_type: str
property
¶
Get the type of this message.
Returns:
| Type | Description |
|---|---|
str
|
The message type name. |
value: dict[str, object]
property
¶
Get the raw dictionary representation of the metadata.
__init__(message_type: str, message_id: UUID | None = None, created_at: datetime | None = None, correlation_id: UUID | None = None, causation_id: UUID | None = None) -> None
¶
Initialize message metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_type
|
str
|
The type/name of the message. |
required |
message_id
|
UUID | None
|
Unique identifier for the message. If None, generates a |
None
|
created_at
|
datetime | None
|
When the message was created. If None, uses current UTC time. |
None
|
correlation_id
|
UUID | None
|
Identifier to correlate related messages. If None, |
None
|
causation_id
|
UUID | None
|
Identifier of the message that caused this one. If None, |
None
|
Source code in src/forging_blocks/foundation/messages/message.py
to_dict() -> dict[str, object]
¶
Convert metadata to dictionary representation.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dictionary representation of the metadata. |
from_dict(data: dict[str, object]) -> MessageMetadata
classmethod
¶
Create metadata from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, object]
|
Dictionary containing the serialised metadata fields. |
required |
Returns:
| Type | Description |
|---|---|
MessageMetadata
|
A new MessageMetadata instance reconstituted from data. |
Source code in src/forging_blocks/foundation/messages/message.py
Message
¶
Bases: ValueObject[MessageRawType], ABC
Base class for all foundation messages.
Messages are immutable value objects that represent intent or facts in the application.
This is the base class for Events (something that happened) and Commands
(something to do).
Features:
- Immutable by design (inherits from ValueObject)
- Contains MessageMetadata for infrastructure concerns
- Focus on data in subclasses
- Each message instance is unique (based on metadata.message_id)
This class should not be used directly. Use Event or Command instead.
Source code in src/forging_blocks/foundation/messages/message.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
metadata: MessageMetadata
property
¶
Get the message metadata.
Returns:
| Type | Description |
|---|---|
MessageMetadata
|
The message metadata containing ID, timestamp, etc. |
message_id: UUID
property
¶
Convenience property to get the message ID.
Returns:
| Type | Description |
|---|---|
UUID
|
The unique message identifier. |
created_at: datetime
property
¶
Convenience property to get when the message was created.
Returns:
| Type | Description |
|---|---|
datetime
|
When the message was created. |
__init__(metadata: MessageMetadata | None = None) -> None
¶
Initialize the message with metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
MessageMetadata | None
|
Message metadata. If None, creates new metadata with |
None
|
Source code in src/forging_blocks/foundation/messages/message.py
__eq__(other: object) -> bool
¶
Check equality based on _equality_components.
to_dict() -> dict[str, object]
¶
Convert the message to a dictionary representation.
Combines metadata, message type, payload, and domain data.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Complete dictionary representation of the message. |
Source code in src/forging_blocks/foundation/messages/message.py
get_domain_data() -> dict[str, object]
¶
Return the domain-level data carried by this message.
The default implementation delegates to _payload. Subclasses
that use the decorator-based approach (@event_dataclass etc.)
override this to return only the message fields.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dictionary of domain data fields. |
Source code in src/forging_blocks/foundation/messages/message.py
from_dict(data: dict[str, object]) -> Self
classmethod
¶
Create a message instance from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, object]
|
Dictionary containing the serialised message. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new message instance reconstituted from data. |