Entity¶
entity
¶
Base Entity class for domain blocks.
Entity
¶
Bases: ABC
Base class for all domain entities.
An entity is defined by its identity rather than its attributes. Two entities with the same
identifier are considered equal, regardless of their other attributes.
Concrete subclasses are automatically frozen (selective freeze on '_id') after __init__
completes. Intermediate abstract classes remain unfrozen so their concrete leaf subclasses
can finish setting up via super().__init__().
Example
class User(Entity[int]):
def __init__(self, user_id: int | None, name: str) -> None:
super().__init__(user_id)
self.name = name
u1 = User(1, "Alice")
u2 = User(1, "Alice")
u3 = User(2, "Alice")
draft = User(None, "Bob")
assert u1 == u2 # same type and id
assert u1 != u3 # different id
assert draft.is_persisted() is False
Source code in src/forging_blocks/domain/entity.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 | |
id: TId | None
property
¶
Return the entity's identifier, or None if it's a draft entity.
__init_subclass__(**kwargs: Any) -> None
¶
Automatically apply selective freeze to concrete subclasses.
Source code in src/forging_blocks/domain/entity.py
__setattr__(name: str, value: Any) -> None
¶
Prevent modification of '_id' once set to a non-None value.
Source code in src/forging_blocks/domain/entity.py
__delattr__(name: str) -> None
¶
__eq__(other: object) -> bool
¶
Check equality based on type and identifier.
Source code in src/forging_blocks/domain/entity.py
__hash__() -> int
¶
Return the hash based on the entity's identifier.
Raises:
| Type | Description |
|---|---|
DraftEntityIsNotHashableError
|
If the entity is a draft (id is None). |