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: Element, 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
347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
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
400 401 402 | |
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
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
to_dict
to_dict() -> dict[str, Any]
Return data as a dictionary.
Source code in src/protean/core/value_object.py
472 473 474 475 476 477 | |
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
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |