Repository¶
Generic repository interfaces for Domain-Driven Design.
This module defines repository interfaces used to persist and retrieve
aggregate roots. Repositories abstract infrastructure concerns and offer
type-safe contracts for both command-side and query-side data access in
CQRS or traditional architectures.
Responsibilities:
- Persist and retrieve aggregates.
- Abstract away storage mechanisms.
Non-Responsibilities:
- Implement business invariants (aggregate enforces them).
- Expose ORM models or database APIs directly.
repository
¶
Generic repository interfaces for Domain-Driven Design.
This module defines repository interfaces used to persist and retrieve
aggregate roots. Repositories abstract infrastructure concerns and offer
type-safe contracts for both command-side and query-side data access in
CQRS or traditional architectures.
Responsibilities
- Persist and retrieve aggregates.
- Abstract away storage mechanisms.
Non-Responsibilities
- Implement business invariants (aggregate enforces them).
- Expose ORM models or database APIs directly.
ReadOnlyRepository
¶
Bases: Generic[TReadAggregateRoot, TId], Protocol
Read-only repository abstraction for query operations.
This interface is optimized for query-side usage in CQRS architectures.
It provides type-safe retrieval of aggregates or read models.
Source code in src/forging_blocks/application/ports/outbound/repository.py
get_by_id(id: TId) -> TReadAggregateRoot | None
async
¶
Retrieve an aggregate or read model by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
TId
|
Unique identifier of the resource. |
required |
Returns:
| Type | Description |
|---|---|
TReadAggregateRoot | None
|
The retrieved instance or None if not found. |
Notes
Implementations may read from:
- read replicas,
- projections,
- cached read models.
Source code in src/forging_blocks/application/ports/outbound/repository.py
list_all() -> Sequence[TReadAggregateRoot]
async
¶
Retrieve all resources.
Returns:
| Type | Description |
|---|---|
Sequence[TReadAggregateRoot]
|
A sequence of aggregate or read model instances. |
Notes
Data sources may differ from command-side storage.
Source code in src/forging_blocks/application/ports/outbound/repository.py
WriteOnlyRepository
¶
Bases: Protocol, Generic[TWriteAggregateRoot, TWriteId]
Write-only repository abstraction for command operations.
This interface supports command-side operations where writes are applied
independently from read-side storage.
Source code in src/forging_blocks/application/ports/outbound/repository.py
delete_by_id(id: TWriteId) -> None
async
¶
Delete an aggregate by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
TWriteId
|
Unique identifier of the aggregate. |
required |
Raises:
| Type | Description |
|---|---|
RepositoryError
|
If deletion fails. |
save(aggregate: TWriteAggregateRoot) -> None
async
¶
Persist an aggregate instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregate
|
TWriteAggregateRoot
|
The aggregate to save. |
required |
Repository
¶
Bases: ReadOnlyRepository[TAggregateRoot, TId], WriteOnlyRepository[TAggregateRoot, TId], Protocol
Full CRUD repository abstraction.
Combines read and write capabilities into a single repository interface.
Suitable for non-CQRS applications or simplified contexts.