How kazuma0606/thread_safe_clean_architechture works
Main Components
• Domain Layer: Represents the heart of the application containing business rules and entities (e.g., the User entity and the repository interface in src/domain).
• Application Layer: Contains use cases and Data Transfer Objects (DTOs) which process input from the CLI and orchestrate domain operations (located in src/application).
• Adapter Layer: Implements interfaces like repositories and utilities that interact with external systems (for example, the JSON repository in src/adapter and UUID generator in src/adapter/utils).
• Infrastructure/Framework Layer: Provides the CLI interface (src/infrastructure/cli) and the entry point (src/main.rs) that ties everything together.
File Structure and Relationships
• The directories (adapter, application, domain, state, and infrastructure) reflect a separation of concerns according to Clean Architecture.
• The adapter layer contains subdirectories for controllers (although labeled “contoroller”) and repository implementations (like json_user_repository).
• The application layer contains DTOs (for both user creation requests and responses) and use case implementations (specifically create_user_usecase).
• The domain layer holds business entities (user) and repository and utility interfaces.
• The infrastructure includes CLI-related modules which serve as the interface for user commands.
• There are configuration files (Cargo.toml, Cargo.lock) that set the Rust environment and dependencies.
Incorporate Architectural Patterns and Principles
• Clean Architecture: Emphasize the separation between the inner domain/application layers and the outer adapter/infrastructure layers.
• Dependency Injection: Show how use cases accept repository and id-generator interfaces, making it possible to inject different implementations (mock or production).
• DTO Pattern: Represent the transformation of input data into domain objects and then into output DTOs.
• Thread Safety: Illustrate how shared resource handling is done via Arc<Mutex<>> in the repository for concurrent access.
• Asynchronous Processing: Indicate async operations where non-blocking I/O (e.g., working with JSON files) is used in use cases.
Detailing Components and Their Interactions
• Use Case Component: Represent the CreateUserUseCase in the application layer, which receives input DTOs, interacts with the domain by creating User entities, and uses the repository interface to persist data.
• Domain Entities: Show the User entity with its immutable characteristics, populated through domain logic.
• Utility Components: Include an IdGenerator interface in the domain and its UUID generator adapter. Indicate dependency injection by showing the use case receiving both repository and id-generator components through generic parameters.
• Concurrency Aspects: Optionally annotate the JSON repository with a note to highlight the use of Arc<Mutex<>> for thread-safe operations.