Message¶
Message module for domain messaging patterns.
This module provides the base Message class and MessageMetadata for implementing
domain messages following Domain-Driven Design (DDD) and CQRS principles.
message
¶
Message module for domain messaging patterns.
This module provides the base Message class and MessageMetadata for implementing
domain messages following Domain-Driven Design (DDD) and CQRS principles.
MessageMetadata
¶
Bases: ValueObject[dict[str, Any]]
Metadata associated with domain messages.
Contains infrastructure-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 domain data while keeping
infrastructure concerns in metadata.
Example
metadata = MessageMetadata(message_type="OrderCreated")
Or with custom values¶
custom_metadata = MessageMetadata(
... message_type="UserCreated",
... message_id=UUID("123e4567-e89b-12d3-a456-426614174000"),
... created_at=datetime(2025, 6, 11, 19, 36, 6, tzinfo=timezone.utc)
... )
Source code in src/forging_blocks/domain/messages/message.py
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 | |
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, Any]
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/domain/messages/message.py
to_dict() -> dict[str, Any]
¶
Convert metadata to dictionary representation.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of the metadata. |
Message
¶
Bases: ValueObject[MessageRawType], ABC
Base class for all domain messages.
Messages are immutable value objects that represent intent or facts in the domain.
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 domain 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/domain/messages/message.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 | |
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/domain/messages/message.py
__eq__(other: Any) -> bool
¶
Check equality based on _equality_components.
to_dict() -> dict[str, Any]
¶
Convert the message to a dictionary representation.
Combines metadata, message type, and domain data.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Complete dictionary representation of the message. |