How huuquangg/clean_architecture-case_study works
Project Type and Purpose
• The project is a web API application built around managing parking slots. It is structured as a layered (or clean) architecture where you have clearly separated components: APIs (presentation layer), Entities (domain models), Usecase (business logic) and Infrastructure (data access, repositories, and database management).
File Structure
• The top-level solution file (ParkingSlot.sln) and the separate projects indicate a modular approach.
• Key directories are:
• ParkingSlot.APIs: Contains controllers (SlotController.cs), the main Program.cs file, and configuration files (appsettings.json). It acts as the presentation layer exposing endpoints.
• ParkingSlot.Entities: Holds the core domain model (Slot.cs) representing the fundamental data structure.
• ParkingSlot.Usecase: Includes business logic (SlotManager.cs) and interface definitions (ISlotRepository.cs) that drive the application's operations.
• ParkingSlot.Infrastructure: Contains the implementations supporting the domain – such as the database context (Models/DatabaseContext.cs), an in-memory repository (InMemISlotRepository.cs), the database file (Database/parkingslot.db) and database migration scripts (Version/1_0_0/up.sql and down.sql). This layer is responsible for data persistence and integration with external storage.
• This structure clearly follows a separation of concerns, ensuring that each layer has distinct responsibilities.
README for Additional Insights
• Although the README content provided is empty, the naming and structure indicate this is a modular application where each project supports a different layer of the overall system. Therefore, focus on representing interactions between these layers.
a. Main Components:
• Presentation Layer: ParkingSlot.APIs (with SlotController handling HTTP requests).
• Business Logic/Usecase Layer: ParkingSlot.Usecase (SlotManager, ISlotRepository interface).
• Domain Layer: ParkingSlot.Entities (Slot.cs, the domain model).
• Data Access/Infrastructure Layer: ParkingSlot.Infrastructure (DatabaseContext, InMemISlotRepository, migration scripts, and the actual database file).
b. Relationships and Interactions:
• The API layer receives client requests and directs them to controllers (e.g., SlotController).
• Controllers invoke the Usecase layer (via SlotManager) to apply business rules.
• The Usecase layer interacts with the Repository interface (ISlotRepository) that is implemented in the Infrastructure layer.
• The Infrastructure layer handles data persistence using the in-memory repository or database (parkingslot.db) along with migration support managed through versioning.
• Data flows from the Presentation layer to the Business layer, then to the Infrastructure layer and back, ensuring separation of responsibilities.
c. Important Architectural Patterns:
• Layered (or Clean) Architecture: Clear separation between presentation, business logic, domain, and infrastructure.
• Dependency Inversion: The domain and business logic depend on abstractions (like ISlotRepository) rather than concrete implementations.
• Data Migration and Versioning are managed in a dedicated subdirectory, ensuring controlled database evolution.
d. Technologies, Frameworks, or Libraries:
• ASP.NET Core is likely used for the API (Controller, Program.cs, appsettings.json).
• Entity Framework or an alternative ORM might be suspected given the database context, although the presence of an in-memory repository file implies a potential for testing or alternative storage strategies.
• SQLite could be inferred from the database file (parkingslot.db).