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
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 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 | |
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->MyDomainmy_domain_1->MyDomain1my_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_domainMy Domain->my_domainMy-Domain->my_domainMy Domain 1->my_domain_1My Domain 1.0->my_domain_1_0
init
init(traverse: bool = True) -> None
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
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
domain_context
domain_context(**kwargs: Any) -> DomainContext
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
949 950 951 952 953 954 955 956 957 958 959 | |
aggregate
aggregate(_cls: type[_T]) -> type[_T]
aggregate(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
aggregate(
_cls: type[_T" backlink-type="used-by" backlink-anchor="protean.domain.Domain.aggregate" optional hover>_T] | None = None, **kwargs: Any
) -> type[_T" backlink-type="returned-by" backlink-anchor="protean.domain.Domain.aggregate" optional hover>_T] | Callable[[type[_T" backlink-type="returned-by" backlink-anchor="protean.domain.Domain.aggregate" optional hover>_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 | |
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
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 | |
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
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 | |
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
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 | |
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
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 | |
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
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 | |
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
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 | |
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
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 | |
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
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 | |
repository
repository(_cls: type[_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
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 | |
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
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 | |
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
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 | |
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
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 | |
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
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 | |
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
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 | |
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
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 | |
process
process(
command: Any,
asynchronous: bool | None = None,
idempotency_key: str | None = None,
raise_on_duplicate: bool = False,
priority: int | None = None,
correlation_id: str | None = None,
deadline: datetime | None = None,
timeout: timedelta | None = None,
) -> Any | None
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:
|
deadline
|
Absolute time after which the command must not
be executed. Stored in metadata and propagated to downstream commands.
Mutually exclusive with
TYPE:
|
timeout
|
Relative deadline (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any | None
|
Optional[Any]: Returns either the command handler's return value or nothing, based on preference. |
Source code in src/protean/domain/__init__.py
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 | |