The Unit of Work (UoW) pattern groups one or more repository operations into a single atomic transaction. HexCore’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Indroic/HexCore/llms.txt
Use this file to discover all available pages before exploring further.
IUnitOfWork is an async context manager that opens a database transaction on entry, rolls back automatically on any exception, and provides a clean commit() / rollback() API for explicit control. It also acts as the event dispatcher — it tracks entities modified during the transaction and publishes their domain events after a successful commit. This page documents the interface, the get_repository() helper, and common usage patterns.
IUnitOfWork
IUnitOfWork lives in hexcore.domain.uow. It is an abstract base class; concrete implementations (e.g. for SQLAlchemy or Beanie) live in the infrastructure layer.
Constructor
The base__init__ sets two instance attributes that concrete implementations extend:
Registry of repository instances attached to this UoW. Populated by the concrete implementation during
__aenter__.Reference to the active
EventBus. Set by the concrete implementation. Used by dispatch_events() to publish collected domain events.Async context manager
IUnitOfWork implements __aenter__ and __aexit__ so it can be used with async with:
async with block, __aexit__ automatically calls rollback() before re-raising the exception.
Abstract methods
Flush all pending changes to the database and make them permanent. After a successful commit the concrete implementation should also call
dispatch_events() to publish domain events from all tracked entities.Discard all pending changes. Called automatically by
__aexit__ when an exception occurs. You can also call it explicitly to abort a transaction.Register a
BaseEntity instance for event collection. The UoW will call entity.pull_domain_events() during collect_domain_events() to gather events before dispatching them.You rarely call
collect_entity directly. The @register_entity_on_uow decorator (applied to repository save() implementations) calls this automatically whenever an entity has pending domain events.Pull domain events from all tracked entities and return them as a flat list. Clears the entity’s event queue in the process (via
pull_domain_events()).Collect domain events from all tracked entities and publish each one to the
event_bus. Typically called inside commit() after the database transaction succeeds.Remove all entity references from the UoW’s tracking list without pulling or dispatching events. Useful for resetting state between test cases.
@register_entity_on_uow decorator
hexcore.infrastructure.uow.decorators provides the @register_entity_on_uow decorator. Apply it to a repository’s save() method to make the UoW automatically track any entity that has pending domain events.
entity._domain_events after the wrapped method returns. If the list is non-empty it calls self.uow.collect_entity(entity).
get_repository() helper
hexcore.infrastructure.uow.helpers.get_repository provides type-safe access to a named repository on a UoW instance.
The active Unit of Work instance.
The attribute name under which the repository is attached to the UoW (e.g.
"user_repository").The repository interface or class used for type casting. Not used for runtime lookup — only for static type checking.
get_repository raises an AttributeError with a helpful message listing every IBaseRepository instance it found, making debugging easy:
Usage patterns
- Basic transaction
- Explicit rollback
- Event dispatch
The most common pattern — open a UoW, perform operations, commit.
Repository discovery and v2 behaviour
The UoW scans every module listed inrepository_discovery_paths at startup and instantiates the repository classes it finds, attaching them to the UoW instance by a conventional attribute name. If a module is missing from the list, its repositories are invisible to get_repository().