In Memory Read Repository¶
in_memory_read_repository
¶
In-memory read-only repository backed by a dictionary.
Provides a concrete implementation of ReadOnlyRepositoryPort for
query-side operations in CQRS architectures. Storage is a plain
dictionary keyed by entity identifier.
InMemoryReadRepository
¶
Bases: ReadOnlyRepositoryPort[TEntity, TId]
In-memory read-only repository backed by a dictionary.
Stores entities in a dictionary keyed by their identifier.
The storage mapping is injected via the constructor and copied on init
to ensure independence from external mutation.
Example
class ExpressionSpecification:
def __init__(self, predicate):
self._predicate = predicate
def is_satisfied_by(self, entity):
return self._predicate(entity)
storage = {1: {"id": 1, "name": "alpha"}, 2: {"id": 2, "name": "beta"}}
repo = InMemoryReadRepository[dict, int](storage=storage)
entity = await repo.get_by_id(1)
active = ExpressionSpecification(lambda e: e["name"].startswith("a"))
results = await repo.find_matching(active)
Source code in src/forging_blocks/infrastructure/repositories/in_memory_read_repository.py
__init__(storage: Mapping[TId, TEntity] | None = None) -> None
¶
Initialize the read repository with optional external storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
Mapping[TId, TEntity] | None
|
An optional mapping to use as backing storage. |
None
|
Source code in src/forging_blocks/infrastructure/repositories/in_memory_read_repository.py
get_by_id(entity_id: TId) -> TEntity | None
async
¶
Retrieve an entity by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
TId
|
Unique identifier of the entity. |
required |
Returns:
| Type | Description |
|---|---|
TEntity | None
|
The entity if found, otherwise None. |
Source code in src/forging_blocks/infrastructure/repositories/in_memory_read_repository.py
list_all() -> Sequence[TEntity]
async
¶
Retrieve all resources in the repository.
Returns:
| Type | Description |
|---|---|
Sequence[TEntity]
|
A sequence of all stored entities. |
find_matching(spec: Specification[TEntity]) -> Sequence[TEntity]
async
¶
Return all stored entities that satisfy the given specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
Specification predicate to filter entities. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[TEntity]
|
A list of matching entities. |
Source code in src/forging_blocks/infrastructure/repositories/in_memory_read_repository.py
count_matching(spec: Specification[TEntity]) -> int
async
¶
Return the count of entities satisfying the specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
Specification predicate to filter entities. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of matching entities. |
Source code in src/forging_blocks/infrastructure/repositories/in_memory_read_repository.py
exists_matching(spec: Specification[TEntity]) -> bool
async
¶
Return True if at least one entity satisfies the specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
Specification predicate to filter entities. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if at least one entity matches, False otherwise. |