dddmisc-unit-of-work
d3m.uow
ILocker
Bases: Generic[TLock]
, ABC
A generic abstract class representing a locker. This class defines the interface for creating a context manager that uses a lock key.
Attributes:
Name | Type | Description |
---|---|---|
TLock |
generic type, returned on entering to async context manager building by locker instance. |
Methods:
Name | Description |
---|---|
__call__ |
Create a context manager that creates a lock on entering the context and releases it on exiting the context. |
Examples:
To create a custom locker, subclass ILocker and implement the call method.
>>> class RedLocker(ILocker):
>>> def __call__(self, __lock_key: TLockKey = None, /) -> AsyncContextManager[TLock]:
>>> # implementation goes here
__call__(__lock_key=None)
abstractmethod
Create context manager make lock use lock key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__lock_key |
str | None
|
Optional lock key |
None
|
Returns:
Type | Description |
---|---|
AsyncContextManager[TLock]
|
Async context manager create lock object on enter to context and release lock on exit from context |
IRepository
Bases: ABC
An abstract base class for repositories.
Methods:
Name | Description |
---|---|
commit |
Commits the current state of the repository to the persistence storage. |
commit()
abstractmethod
async
Commit all changes to storage
IRepositoryBuilder
Bases: Generic[TRepo]
, ABC
Interface for repository builder using by unit of work context manager.
Attributes:
Name | Type | Description |
---|---|---|
TRepo |
interface of repository to be built. |
__call__(__uow_context_manager)
abstractmethod
async
Build repository instance use unit of work context manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__uow_context_manager |
IUnitOfWorkCtxMgr
|
unit of work context manager. |
required |
Returns:
Type | Description |
---|---|
IRepository | TRepo
|
repository class |
IUnitOfWork
Bases: Generic[TRepo]
, ABC
An interface representing a unit of work for a repository.
Attributes:
Name | Type | Description |
---|---|---|
TRepo |
A generic type representing the repository. |
|
repository |
TRepo
|
Returns the repository instance. |
Methods:
Name | Description |
---|---|
apply |
Commits all changes in the repository. |
repository: TRepo
abstractmethod
property
Returns:
Type | Description |
---|---|
TRepo
|
Repository instance |
apply()
abstractmethod
async
Commit all changes in repository
IUnitOfWorkCtxMgr
Bases: Generic[TRepo, TLock]
, ABC
Abstract base class for context managers implementing unit of work pattern.
This class allows for the creation of context managers that encapsulate a unit of work, providing methods for entering and exiting the context, as well as accessing a lock object.
Attributes:
Name | Type | Description |
---|---|---|
TLock |
A generic type representing the lock object |
|
TRepo |
A generic type representing the repository |
|
lock |
TLock
|
Returns the lock object returned from the locker. |
Methods:
Name | Description |
---|---|
__aenter__ |
Called when entering the context. Returns an instance of IUnitOfWork. |
__aexit__ |
Called when exiting the context. |
lock: TLock
abstractmethod
property
Returns:
Type | Description |
---|---|
TLock
|
Lock object returned from locker |
IUnitOfWorkBuilder
Bases: Generic[TRepo]
, ABC
Build unit of work context manager with an optional lock key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lock_key |
The lock key used for locking the unit of work. |
required |
Returns:
Type | Description |
---|---|
The unit of work context manager. |
__call__(lock_key=None)
abstractmethod
Build unit of work context manager and with lock used lock key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lock_key |
str | None
|
lock key used for lock |
None
|
Returns:
Type | Description |
---|---|
IUnitOfWorkCtxMgr[TRepo, TLock]
|
Unit of work context manager |
UnitOfWorkBuilder
Bases: IUnitOfWorkBuilder
, Generic[TRepo]
Implementation of the IUnitOfWorkBuilder.
This class is responsible for constructing and providing instances of Unit of Work context managers.
Attributes:
Name | Type | Description |
---|---|---|
repository_builder |
IRepositoryBuilder[TRepo]
|
An instance of IRepositoryBuilder[TRepo] that is used to construct repositories for the Unit of Work. |
locker |
ILocker[TLock]
|
An instance of ILocker that is used to manage locks for the Unit of Work. |
Methods call: Returns a new instance of the Unit of Work context manager.
Examples:
>>> class ICustomRepository:
... async def get_aggregate(self, reference):
... ...
>>> uow_builder = UnitOfWorkBuilder[ICustomRepository](RepositoryBuilder(), Locker())
>>> async with uow_builder('lock-key') as uow:
... aggregate = await uow.repository.get_aggregate(...)
... ... # change state of the aggregate.
... await uow.apply()
>>> uow_builder = UnitOfWorkBuilder[ICustomRepository](RepositoryBuilder(), Locker())
>>> uow_context_manager = uow_builder()
>>> assert isinstance(uow_context_manager, IUnitOfWorkCtxMgr)
>>> uow = await uow_context_manager.__aenter__()
>>> assert isinstance(uow, IUnitOfWork)
__call__(__lock_key=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__lock_key |
`TLockKey`
|
The lock key used for locking the resources. Defaults to |
None
|
Returns:
Type | Description |
---|---|
IUnitOfWorkCtxMgr[TRepo, TLock]
|
|