This project provides an example of implementing Clean Architecture principles in a .NET-based Gym Management application. It covers core architectural concepts and patterns, aiming to separate concerns across multiple layers and improve modularity, testability, and maintainability.
This solution follows a Clean Architecture approach, using .NET technologies and a layered architecture that separates responsibilities across the Presentation, Application, Domain, and Infrastructure layers.
The code structure showcases key design patterns, including CQRS, Mediator, Repository, and Unit of Work, which facilitate a scalable and decoupled architecture. It also illustrates how domain events and eventual consistency can be integrated into a project following Clean Architecture, as well as how to apply authorization, authentication, and testing techniques. This README provides a comprehensive guide to the project structure and its design.
GymManagement.sln: The main solution file.requests/: Contains.httpfiles to test API endpoints.src/: Source code for each layer, organized by functionality.tests/: Test projects for validation and functionality assurance.
GymManagement.Api: API layer responsible for handling HTTP requests, routing them to appropriate services in the Application layer, and configuring dependencies.- Controllers: Defines controllers for managing endpoints, acting as entry points for API requests.
- Services: Contains utilities like
CurrentUserProvider.csfor user context GymManagement.Contracts: Defines the data contracts. It includes request and response models for gyms, rooms, subscriptions etc.
GymManagement.Application: Contains business logic, including commands, queries, and service interfaces. Organized by feature (e.g.Gyms,Rooms,Subscriptions).- Commands and Queries: Implements CQRS for managing and retrieving data with the Mediator pattern, using the
MediatRlibrary. - Behaviors: Includes validation using FluentValidation and custom pipeline behaviors for cross-cutting concerns.
- Dependency Injection: Facilitates loose coupling by providing interfaces for dependencies.
GymManagement.Domain: Encapsulates core domain logic and entities.- Entities: Defines domain models, errors, and custom types specific to gyms, rooms, and subscriptions.
- Domain-Driven Design (DDD): Follows DDD principles, ensuring domain models represent business rules and policies.
- Domain Events: Implements events and event handlers to propagate changes. Eventual Consistency is used.
GymManagement.Infrastructure: Handles persistence, external dependencies, and middleware.- Repositories and UoW: Implements the Repository and Unit of Work patterns for database operations.
- Authentication: Provides services for authorization, claims, and token generation.
- Migrations: Manages database schema with migrations, enabling version control for the database structure.
- Middleware: Includes
EventualConsistencyMiddleware.csfor handling eventual consistency.
GymManagement.Application.IntegrationTests: Verifies that modules interact correctly in an end-to-end setup.GymManagement.Application.SubcutaneousTests: Subcutaneous tests focusing on application boundary interactions.GymManagement.Application.UnitTests: Unit tests for business logic in the Application layer.GymManagement.Domain.UnitTests: Unit tests for domain logic and rules.TestCommon: Shared utilities, factories, and constants to support test projects.
- .NET 8 SDK
- A supported database for data persistence. This project uses SQLite by default, but you can configure it to use SQL Server or other supported databases if desired.
-
Clone the repository.
git clone https://github.com/Gregapos/Gym-Management.git
-
Navigate to the project directory.
cd Gym-Management -
Restore dependencies.
dotnet restore
-
Run the database migrations.
dotnet ef database update -p .\src\GymManagement.Infrastructure\ -s .\src\GymManagement.Api\
-
Run the API project.
dotnet run --project src/GymManagement.Api
The .http files in the requests folder allow you to test each API endpoint:
- Open an
.httpfile (e.g.,ListGyms.http). - Send requests directly from your editor (e.g., Visual Studio Code) to test endpoint responses.
To run all tests, navigate to the root folder and run:
dotnet test