Domain
The central registry object. Create one per bounded context to register all domain elements, load configuration, and initialize and shut down infrastructure adapters.
See Compose a Domain for a practical guide.
The registry for a domain's elements and the access point to its adapters.
Create one Domain in your main module or in your package's
__init__.py::
from protean import Domain
domain = Domain()
You can also set the root path explicitly::
domain = Domain(root_path="/path/to/domain")
The root path is the first of these that is set:
- The
root_pathargument. - The
DOMAIN_ROOT_PATHenvironment variable. - The folder of the file that created the domain.
- The current working directory.
| PARAMETER | DESCRIPTION |
|---|---|
root_path
|
The folder that holds the domain file.
TYPE:
|
name
|
The domain's name. Defaults to the calling module's name.
TYPE:
|
config
|
Configuration values that override the defaults.
TYPE:
|
identity_function
|
A function that generates identities for domain objects.
TYPE:
|
Source code in src/protean/domain/__init__.py
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
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
Load the domain's elements, validate them, and initialize its adapters.
Before loading, init configures logging with configure_logging().
It skips this when the root logger already has handlers or when the
PROTEAN_NO_AUTO_LOGGING environment variable is 1 or true
in any case. A failure in this step does not stop init.
| PARAMETER | DESCRIPTION |
|---|---|
traverse
|
Import every module in the domain file's folder and its
subfolders, so their elements register. With
TYPE:
|
Source code in src/protean/domain/__init__.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |
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
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | |
aggregate
aggregate(_cls: type[_T], **kwargs: Any) -> 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
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 | |
entity
entity(_cls: type[_T], **kwargs: Any) -> 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
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 | |
value_object
value_object(_cls: type[_T], **kwargs: Any) -> 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
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 | |
command
command(_cls: type[_T], **kwargs: Any) -> 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
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 | |
event
event(_cls: type[_T], **kwargs: Any) -> 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
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 | |
command_handler
command_handler(
_cls: type[_T], **kwargs: Any
) -> 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
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 | |
event_handler
event_handler(_cls: type[_T], **kwargs: Any) -> type[_T]
event_handler(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
event_handler(
_cls: type[_T] | None = None, **kwargs: Any
) -> type[_T] | Callable[[type[_T]], type[_T]]
Source code in src/protean/domain/__init__.py
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 | |
application_service
application_service(
_cls: type[_T], **kwargs: Any
) -> 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
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 | |
domain_service
domain_service(_cls: type[_T], **kwargs: Any) -> 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
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 | |
repository
repository(_cls: type[_T], **kwargs: Any) -> 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
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 | |
projection
projection(_cls: type[_T], **kwargs: Any) -> 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
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 | |
projector
projector(_cls: type[_T], **kwargs: Any) -> 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
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 | |
subscriber
subscriber(_cls: type[_T], **kwargs: Any) -> 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
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 1805 1806 1807 | |
process_manager
process_manager(
_cls: type[_T], **kwargs: Any
) -> 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
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 | |
upcaster
upcaster(_cls: type[_T], **kwargs: Any) -> type[_T]
upcaster(
_cls: None = ..., **kwargs: Any
) -> Callable[[type[_T]], type[_T]]
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
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 | |
database_model
database_model(_cls: type[_T], **kwargs: Any) -> 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
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 | |
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
Store a command in the event store and handle it now or later.
The command is handled synchronously when asynchronous is False
or when the command_processing config is "sync". Otherwise the
engine handles it later.
When command_processing is "sync" and asynchronous is
True, a call inside an active unit of work (a handler, a
@use_case method, a with UnitOfWork(): block or an explicit
UnitOfWork.start()) defers the command. It is stored at once and
handled after the caller's unit of work commits, in its own unit of
work. A failure while it runs, such as a deadline that passed before
the commit, makes the caller's commit raise TransactionError. A
rollback of the caller means the command never runs. The built-in
event stores write outside the unit of work, so the command stays
stored and unhandled.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
Instance of a
TYPE:
|
asynchronous
|
Whether to handle the command later. Defaults to the
TYPE:
|
idempotency_key
|
Key for deduplicating submissions. When the idempotency store already holds a success for this key, the cached result is returned and the command is not stored again.
TYPE:
|
raise_on_duplicate
|
Raise
TYPE:
|
priority
|
Processing priority for the events this command produces.
Defaults to the current
TYPE:
|
correlation_id
|
Correlation ID carried by every command and event in the causal chain. A new one is generated when omitted.
TYPE:
|
deadline
|
Absolute time after which the command must not run. Passed on to downstream commands in the same causal chain.
TYPE:
|
timeout
|
Relative deadline, converted to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any | None
|
The command handler's return value when the command is handled |
Any | None
|
synchronously. |
Any | None
|
repeat of a deferred command's idempotency key in the same unit of |
Any | None
|
work. Otherwise, the command's position in the event store. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If the command is not registered with the
domain, or if both |
DuplicateCommandError
|
If |
CommandExpiredError
|
If the deadline has passed when a synchronous handler is about to run. |
Source code in src/protean/domain/__init__.py
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 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 | |