Documentation 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 defines the transactional boundary for every write operation in your application. It acts as an async context manager: entering the context opens a transaction, a successful exit commits it, and any unhandled exception automatically triggers a rollback. The UoW also owns the event-dispatch lifecycle — it collects domain events from tracked entities and publishes them after a successful commit. Concrete implementations live in the infrastructure layer (e.g., SQLAlchemy, Django ORM).
Class definition
Constructor attributes
A mapping of repository names to repository instances. Populated by the concrete implementation’s
__init__. Used by get_repository() for type-safe access.An optional
EventBus (or IEventBus) instance. When set, dispatch_events() uses it to publish collected domain events.Async context manager
__aenter__
self. Override in your concrete implementation to open a database session or begin a transaction.
The same Unit of Work instance, ready for use inside the
async with block.__aexit__
exc_type is set (an exception was raised inside the block), rollback() is called automatically. Successful exits do not commit automatically — you must call commit() explicitly before the async with block ends.
The design choice of requiring an explicit
commit() rather than auto-committing on success makes the commit intent visible in application code and prevents accidental commits when control flow is complex.Abstract methods
commit
async with block exits.
rollback
__aexit__ on exception; you may also call it manually inside the async with block to programmatically abort.
collect_entity
BaseEntity for event tracking. Concrete implementations typically store the entity in an internal set and later call entity.pull_domain_events() inside collect_domain_events().
The domain entity whose events should be harvested on commit.
collect_domain_events
pull_domain_events() on each, and returns a flat list of all accumulated DomainEvent instances.
All domain events collected from tracked entities since the last call.
dispatch_events
event_bus. Should be called after a successful commit() so events are only published when the transaction has been durably persisted.
clear_tracked_entities
dispatch_events() or inside rollback() to ensure no stale entity references are held between transactions.
get_repository helper
uow.users directly (which loses type information), get_repository casts the attribute to the expected repository type and provides a helpful error message if the attribute does not exist.
The Unit of Work instance to inspect.
The attribute name of the repository on the UoW (e.g.,
"users", "products").The expected repository class. Used for the cast and the error message only — no runtime isinstance check is performed.
The repository instance cast to
repo_type.AttributeError — if repo_name is not an attribute on uow. The error message lists all repositories detected on the UoW instance (attributes whose values are IBaseRepository instances), making debugging straightforward.