BaseValueObject
Base class for value objects -- immutable domain elements without identity, defined entirely by their attributes. Two value objects with the same attributes are considered equal.
Key methods:
replace(**kwargs)-- Create a copy with selected fields changed (see Replacing Fields)to_dict()-- Return field values as a dictionarydefaults()-- Override to set computed defaults during initialization
See Value Objects guide for practical usage and Value Objects concept for design rationale.
Bases: BaseModel, OptionsMixin
Base class for value objects -- immutable domain elements without identity, defined entirely by their attributes.
Two value objects with the same attribute values are considered equal.
Value objects are always embedded within aggregates or entities and cannot
exist independently. They become immutable after construction -- any attempt
to modify an attribute raises IncorrectUsageError.
Fields are declared using standard Python type annotations with optional
Field constraints. Value objects cannot contain identifier or
unique fields, and cannot have associations (HasOne, HasMany).
Supports @invariant.post decorators for validation rules that are
checked after construction.
Source code in src/protean/core/value_object.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
defaults
defaults() -> None
Placeholder for defaults. Override in subclass when an attribute's default depends on other attribute values.
Source code in src/protean/core/value_object.py
236 237 238 | |
replace
replace(**kwargs: Any) -> Self
Return a new value object with specified fields replaced.
Similar to dataclasses.replace() — copies all current field values,
overlays the provided kwargs, and constructs a new instance of the
same class. Invariants are re-validated on the new instance.
Passing field=None explicitly sets the field to None (it does
not keep the old value). Only omitted fields retain their original
values.
Raises IncorrectUsageError if any key in kwargs is not a
declared field.
Source code in src/protean/core/value_object.py
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 | |
to_dict
to_dict() -> dict[str, Any]
Return data as a dictionary.
Source code in src/protean/core/value_object.py
289 290 291 292 293 294 | |
value_object_from_entity
Create a BaseValueObject subclass whose fields mirror entity_cls.
This eliminates the boilerplate of manually duplicating an entity's fields into a value object for use in commands and events.
| PARAMETER | DESCRIPTION |
|---|---|
entity_cls
|
The entity (or aggregate) class to project.
TYPE:
|
name
|
Override the generated class name (default:
TYPE:
|
exclude
|
Field names to omit from the generated value object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
type[BaseValueObject]
|
A new |
Source code in src/protean/core/value_object.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |