Domain
The central registry object. Create one per bounded context to register all domain elements, manage configuration, and coordinate infrastructure adapters.
See Compose a Domain for a practical guide.
The domain object is a one-stop gateway to:
- Registering Domain Objects/Concepts
- Querying/Retrieving Domain Artifacts like Entities, Services, etc.
- Retrieve injected infrastructure adapters
Usually you create a Domain instance in your main module or
in the __init__.py file of your package like this::
from protean import Domain
domain = Domain()
The Domain will automatically detect the root path of the calling module. You can also specify the root path explicitly::
domain = Domain(root_path="/path/to/domain")
The root path resolution follows this priority:
- Explicit
root_pathparameter if provided DOMAIN_ROOT_PATHenvironment variable if set- Auto-detection of caller's file location
- Current working directory as last resort
:param root_path: the path to the folder containing the domain file (optional, will auto-detect if not provided) :param name: the name of the domain (optional, will use the module name if not provided) :param config: optional configuration dictionary :param identity_function: optional function to generate identities for domain objects
Source code in src/protean/domain/__init__.py
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
has_outbox
property
has_outbox: bool
Whether the outbox pattern is active.
Derived from server.default_subscription_type: outbox is enabled
when subscription type is "stream". For backward compatibility,
an explicit enable_outbox = true also activates the outbox.
camel_case_name
cached
property
camel_case_name: str
Return the CamelCase name of the domain.
The CamelCase name is the name of the domain with the first letter capitalized.
Examples:
- my_domain -> MyDomain
- my_domain_1 -> MyDomain1
- my_domain_1_0 -> MyDomain10
normalized_name
cached
property
normalized_name: str
Return the normalized name of the domain.
The normalized name is the underscored version of the domain name.
Examples:
- MyDomain -> my_domain
- My Domain -> my_domain
- My-Domain -> my_domain
- My Domain 1 -> my_domain_1
- My Domain 1.0 -> my_domain_1_0
init
init(traverse=True)
Parse the domain folder, and attach elements dynamically to the domain.
Protean parses all files in the domain file's folder, as well as under it, to load elements. So, all domain files are to be nested under the file contain the domain definition.
One can use the traverse flag to control this functionality, True by default.
When enabled, Protean is responsible for loading domain elements and ensuring all functionality is activated.
The developer is responsible for activating functionality manually when autoloading is disabled. Element activation can be done by importing them in central areas of domain execution, like Application Services.
For example, asynchronous aspects of a domain like its Subscribers and Event Handlers should be imported in their relevant Application Services and Aggregates.
This method bubbles up circular import issues, if present, in the domain code.
Source code in src/protean/domain/__init__.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
domain_context
domain_context(**kwargs)
Create a DomainContext. Use as a with
block to push the context, which will make current_domain
point at this domain.
::
with domain.domain_context():
init_db()
Source code in src/protean/domain/__init__.py
844 845 846 847 848 849 850 851 852 853 854 | |
aggregate
aggregate(_cls: type[_T]) -> type[_T]
aggregate(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
aggregate(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1292 1293 1294 1295 1296 1297 1298 1299 1300 | |
entity
entity(_cls: type[_T]) -> type[_T]
entity(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
entity(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1400 1401 1402 1403 1404 | |
value_object
value_object(_cls: type[_T]) -> type[_T]
value_object(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
value_object(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1464 1465 1466 1467 1468 1469 1470 1471 1472 | |
command
command(_cls: type[_T]) -> type[_T]
command(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
command(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1324 1325 1326 1327 1328 1329 1330 1331 1332 | |
event
event(_cls: type[_T]) -> type[_T]
event(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
event(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1352 1353 1354 1355 1356 1357 1358 1359 1360 | |
command_handler
command_handler(_cls: type[_T]) -> type[_T]
command_handler(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
command_handler(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1340 1341 1342 1343 1344 | |
event_handler
event_handler(_cls: type[_T]) -> type[_T]
event_handler(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
event_handler(
_cls: type[_T" backlink-type="used-by" backlink-anchor="protean.domain.Domain.event_handler" optional hover>_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1368 1369 1370 1371 1372 1373 1374 1375 1376 | |
application_service
application_service(_cls: type[_T]) -> type[_T]
application_service(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
application_service(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1308 1309 1310 1311 1312 1313 1314 1315 1316 | |
domain_service
domain_service(_cls: type[_T]) -> type[_T]
domain_service(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
domain_service(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1384 1385 1386 1387 1388 1389 1390 1391 1392 | |
repository
repository(_cls: type[_T" backlink-type="used-by" backlink-anchor="protean.domain.Domain.repository" optional hover>_T]) -> type[_T]
repository(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
repository(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1436 1437 1438 1439 1440 | |
projection
projection(_cls: type[_T]) -> type[_T]
projection(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
projection(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1480 1481 1482 1483 1484 1485 1486 1487 1488 | |
projector
projector(_cls: type[_T]) -> type[_T]
projector(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
projector(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1496 1497 1498 1499 1500 1501 1502 1503 1504 | |
subscriber
subscriber(_cls: type[_T]) -> type[_T]
subscriber(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
subscriber(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1448 1449 1450 1451 1452 1453 1454 1455 1456 | |
process_manager
process_manager(_cls: type[_T]) -> type[_T]
process_manager(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
process_manager(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1544 1545 1546 1547 1548 1549 1550 1551 1552 | |
upcaster
upcaster(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Register an event upcaster with the domain.
Upcasters transform raw event payloads from an old schema version to
a newer one. They are applied lazily during deserialization so that
@apply handlers and event handlers always see the current schema.
| PARAMETER | DESCRIPTION |
|---|---|
event_type |
The event class this upcaster targets (current version).
TYPE:
|
from_version |
Source version number (e.g.
TYPE:
|
to_version |
Target version number (e.g.
TYPE:
|
Example::
@domain.upcaster(event_type=OrderPlaced, from_version=1, to_version=2)
class UpcastOrderPlacedV1ToV2(BaseUpcaster):
def upcast(self, data: dict) -> dict:
data["currency"] = "USD"
return data
Source code in src/protean/domain/__init__.py
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 | |
database_model
database_model(_cls: type[_T]) -> type[_T]
database_model(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
database_model(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1424 1425 1426 1427 1428 | |
process
process(
command: Any,
asynchronous: Optional[bool] = None,
idempotency_key: Optional[str] = None,
raise_on_duplicate: bool = False,
priority: Optional[int] = None,
correlation_id: Optional[str] = None,
) -> Optional[Any]
Process command and return results based on specified preference.
By default, Protean does not return values after processing commands. This behavior
can be overridden either by setting command_processing in config to "sync" or by specifying
asynchronous=False when calling the domain's handle method.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
Command to process (instance of a
TYPE:
|
asynchronous
|
Specifies if the command should be processed asynchronously. Defaults to True.
TYPE:
|
idempotency_key
|
Caller-provided key for command deduplication. When provided, enables submission-level dedup via the idempotency store.
TYPE:
|
raise_on_duplicate
|
If
TYPE:
|
priority
|
Processing priority for events produced by this command.
TYPE:
|
correlation_id
|
Correlation ID for distributed tracing.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Optional[Any]
|
Optional[Any]: Returns either the command handler's return value or nothing, based on preference. |
Source code in src/protean/domain/__init__.py
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 | |