Core Concepts¶
This section provides detailed explanations of the core architectural concepts and patterns used within the castlecraft-engineer library. Understanding these concepts will help you effectively build applications and leverage the framework's capabilities.
Table of Contents¶
-
Aggregates
- Explains the Domain-Driven Design (DDD) Aggregate pattern, the
Aggregatebase class, its role in managing consistency boundaries, and howAggregateRepositoryhandles persistence and optimistic concurrency.
- Explains the Domain-Driven Design (DDD) Aggregate pattern, the
-
Authentication
- Covers the authentication mechanisms, focusing on the
AuthenticationServicefor token verification (JWKS, introspection, userinfo) and its caching strategies.
- Covers the authentication mechanisms, focusing on the
-
Authorization
- Details the authorization framework, including the
AuthorizationService,Permissiondefinitions, the@ctxdecorator for handlers, and how to implement permission checks.
- Details the authorization framework, including the
-
Caching
- Covers caching capabilities using Redis, including configuration, helper utilities, and examples of using synchronous and asynchronous cache clients. Also mentions internal uses like JWKS caching.
-
Command Query Responsibility Segregation (CQRS)
- Provides an overview of the CQRS pattern, explaining the separation of read and write operations and the roles of Commands, Queries, and Events in this architecture.
-
Commands
- Focuses on the write-side of CQRS: the
Commandbase class,CommandHandlerfor business logic, and theCommandBusfor dispatching commands.
- Focuses on the write-side of CQRS: the
-
Custom Repositories
- Explains how to create custom repository methods beyond the generic CRUD operations, often by inheriting from the base repository classes and adding specific query logic.
-
Dependency Injection
- Explains how Dependency Injection (DI) is managed using
punqand theContainerBuilderutility for registering and resolving components like handlers, repositories, and services.
- Explains how Dependency Injection (DI) is managed using
-
Events
- Describes domain events, the
Eventbase class,EventHandlerfor reacting to events, the in-processEventBus, and abstractions for external event publishing/consumption (ExternalEventPublisher,EventStreamConsumer).
- Describes domain events, the
-
Event Store
- Details the
EventStoreabstraction for persisting and retrieving event streams, crucial for Event Sourcing. Covers its API and concepts like optimistic concurrency.
- Details the
-
Event Sourced Aggregates
- Explains how
EventSourcedAggregateextends the baseAggregatefor event sourcing, managing state through events, and integrating with snapshotting.
- Explains how
-
Projectors and Read Models
- Describes how Projectors consume event streams to build and maintain denormalized read models optimized for querying. Covers the
Projectorabstraction andProjectionStorefor state management.
- Describes how Projectors consume event streams to build and maintain denormalized read models optimized for querying. Covers the
-
Queries
- Focuses on the read-side of CQRS: the
Querybase class,QueryHandlerfor data retrieval logic, and theQueryBusfor dispatching queries.
- Focuses on the read-side of CQRS: the
-
Specifications
- Explains the Specification pattern for encapsulating business rules for selecting objects, including the
SpecificationABC and composite specifications (AndSpecification,OrSpecification,NotSpecification).
- Explains the Specification pattern for encapsulating business rules for selecting objects, including the
-
Translators
- Describes the Translator (or Mapper/Converter) pattern for transforming data from one object representation to another, using the
Translator[Source, Target]ABC.
- Describes the Translator (or Mapper/Converter) pattern for transforming data from one object representation to another, using the
-
Repositories
- Details the generic repository pattern for
SQLModelentities (ModelRepository,AsyncModelRepository), distinct fromAggregateRepository, often used in Query Handlers or for simpler data operations.
- Details the generic repository pattern for
-
Sagas
- Describes the Saga pattern for managing distributed transactions or long-running business processes that span multiple services or aggregates, including the
Sagabase class andSagaManager.
- Describes the Saga pattern for managing distributed transactions or long-running business processes that span multiple services or aggregates, including the
-
Snapshots and SnapshotStore
- Details the concept of aggregate snapshots for performance optimization, the
Snapshotdata structure, and theSnapshotStoreabstraction for persistence.
- Details the concept of aggregate snapshots for performance optimization, the
-
Security
- Covers security-related aspects, including data encryption utilities (
encrypt_data,decrypt_datausing AES-GCM), OIDC token verification viaAuthenticationService, and a reference to the authorization framework.
- Covers security-related aspects, including data encryption utilities (
-
Unit of Work
- Explains the Unit of Work (UoW) pattern for managing database transactions, ensuring that a series of operations are committed or rolled back atomically. Covers
SQLAlchemyUnitOfWorkandAsyncSQLAlchemyUnitOfWork.
- Explains the Unit of Work (UoW) pattern for managing database transactions, ensuring that a series of operations are committed or rolled back atomically. Covers
Each document provides in-depth explanations and code examples to illustrate the concepts.