A clean architecture-based Human Resources Leave Management system, built with .NET 8, MediatR, and AutoMapper, demonstrating best practices in API development and domain-driven design.
This project implements a comprehensive HR Leave Management system following a Clean Architecture approach. It showcases a well-structured, maintainable, and testable application built with .NET 8. The system handles various aspects of leave management, including leave types, leave requests, and leave allocations, providing a robust foundation for enterprise-level HR solutions.
- β¨ Features
- π Getting Started
- π Project Structure
- βοΈ Configuration
- π§ͺ Testing
- π API Documentation
- π Performance Optimization
- π Observability
- π¦ Design Patterns
- π οΈ Contributing
- π License
- π Additional Resources
- Clean Architecture β Organized into Domain, Application, Infrastructure, and API layers for clear separation of concerns.
- MediatR Implementation β Utilizes MediatR for handling commands, queries, and events, promoting a decoupled and testable codebase.
- AutoMapper Integration β Seamless object-to-object mapping to reduce boilerplate code and improve maintainability.
- Centralized Exception Handling β Robust error management through custom middleware for consistent API responses.
- CRUD Operations for Leave Management β Comprehensive functionalities for managing Leave Types, Leave Requests, and Leave Allocations.
- API Development with .NET 8 β Modern RESTful API built on .NET 6, leveraging its latest features and performance improvements.
- Unit and Integration Testing β Dedicated test projects ensuring code quality and reliability.
- Swagger/OpenAPI Documentation β Automatically generated API documentation for easy consumption and testing.
Ensure you have the following installed:
- .NET 8 SDK
- SQL Server (or any compatible database) β For persistence layer.
- Visual Studio 2022 (or VS Code with C# extensions) β Recommended IDE.
-
Clone the repository:
git clone https://github.com/ahmadabdelbary2001/HR.LeaveManagement.Clean.git cd HR.LeaveManagement.Clean -
Restore dependencies:
dotnet restore
-
Configure Database: Update the connection string in
HR.LeaveManagement.Api/appsettings.jsonandHR.LeaveManagement.Api/appsettings.Development.jsonto point to your SQL Server instance. -
Apply Migrations: Navigate to the
HR.LeaveManagement.Persistenceproject and apply database migrations:dotnet ef database update --project HR.LeaveManagement.Persistence
-
Build and run the API project:
dotnet run --project HR.LeaveManagement.Api
The API will typically run on
https://localhost:5296(or a similar port).
HR.LeaveManagement.Clean/
βββ HR.LeaveManagement.Api/ # ASP.NET Core Web API project
β βββ Controllers/ # API Endpoints
β βββ Middleware/ # Custom Middleware (e.g., Exception Handling)
β βββ Models/ # API-specific models (e.g., Email settings)
β βββ Program.cs # Application startup and service registration
β βββ appsettings.json # Configuration files
βββ HR.LeaveManagement.Application/ # Application layer (Core business logic)
β βββ Contracts/ # Interfaces for services and repositories
β βββ Exceptions/ # Custom exceptions
β βββ Features/ # MediatR handlers (Commands, Queries, Events)
β βββ MappingProfiles/ # AutoMapper profiles
β βββ Models/ # Application-specific models
β βββ ApplicationServiceRegistration.cs # Dependency Injection for Application services
βββ HR.LeaveManagement.Domain/ # Domain layer (Enterprise business rules and entities)
β βββ Common/ # Base entities and common domain logic
β βββ Entities/ # Core domain entities (LeaveType, LeaveRequest, LeaveAllocation)
β βββ HR.LeaveManagement.Domain.csproj # Project file
βββ HR.LeaveManagement.Infrastructure/ # Infrastructure layer (External services, e.g., Email, Logging)
β βββ EmailService/ # Email sending implementation
βββ HR.LeaveManagement.Persistence/ # Persistence layer (Data access, e.g., Entity Framework Core)
β βββ Configurations/ # Entity Framework Core configurations
β βββ Repositories/ # Concrete implementations of domain repositories
β βββ DatabaseContext/ # DbContext for Entity Framework Core
β βββ PersistenceServiceRegistration.cs # Dependency Injection for Persistence services
βββ HR.LeaveManagement.Application.UnitTests/ # Unit tests for the Application layer
βββ HR.LeaveManagement.Persistence.IntegrationTests/ # Integration tests for the Persistence layer
βββ HR.LeaveManagement.Clean.sln # Solution file
Connection strings and other sensitive configurations should be managed securely. For local development, you can use appsettings.Development.json or user secrets.
- Connection Strings β Located in
appsettings.jsonandappsettings.Development.jsonfor database connectivity. - CORS Policy β Configured in
Program.csto allow all origins, headers, and methods for development purposes. Adjust as needed for production environments. - Swagger/OpenAPI β Enabled in
Program.csfor API documentation and testing, typically only in development environments.
dotnet test- Unit Tests:
HR.LeaveManagement.Application.UnitTests/β - Integration Tests:
HR.LeaveManagement.Persistence.IntegrationTests/π
The API documentation is generated using Swagger/OpenAPI and can be accessed when the application is running (typically at https://localhost:5296/swagger).
This project currently does not implement explicit authentication/authorization mechanisms. All API endpoints are publicly accessible. For production use, consider integrating IdentityServer4 or ASP.NET Core Identity.
- Endpoint:
GET /api/LeaveType - Description: Retrieves a list of all leave types.
- Response Codes:
200 OKβ Success β500 Internal Server Errorβ Server error π₯
- Asynchronous Processing β Utilizes
async/awaitfor non-blocking I/O operations, improving responsiveness. - Caching β While not explicitly implemented in the provided code, a caching mechanism (e.g., Redis, in-memory cache) can be integrated into the Infrastructure layer to reduce database load.
- Connection Pooling β Managed by Entity Framework Core for efficient database connection reuse.
- Minimal API Surface β The API is designed to expose only necessary endpoints, reducing overhead.
- Centralized Exception Handling β Custom middleware (
ExceptionMiddleware) in the API layer captures and handles exceptions consistently. - Logging β Can be integrated using popular logging frameworks like Serilog or NLog for structured logging across different environments.
- Health Checks β ASP.NET Core Health Checks can be added to monitor the health of various components (database, external services).
- Clean Architecture β Separates concerns into distinct layers (Domain, Application, Infrastructure, Presentation) to improve maintainability, testability, and scalability.
- Mediator Pattern (MediatR) β Decouples senders from receivers, allowing for a more flexible and testable command/query/event handling system.
- Repository Pattern β Abstracts the data access layer, providing a clean API for interacting with data sources.
- Unit of Work Pattern β Ensures atomicity of transactions by coordinating multiple repository operations within a single transaction.
- Dependency Injection β Manages dependencies between components, promoting loose coupling and testability.
- AutoMapper β Simplifies object-to-object mapping, reducing manual mapping code.
- Fork the repository π΄
- Create your feature branch:
git checkout -b feature/my-feature - Commit changes:
git commit -m 'Add my feature' - Push to branch:
git push origin feature/my-feature - Open a Pull Request
Please adhere to the existing coding style and run tests before submitting.
This project is licensed under the MIT License. See the LICENSE file for details.
- Project Wiki π
- Microsoft .NET Microservices Architecture Guide πΊοΈ
- MediatR GitHub Repository π¬
- AutoMapper Documentation
Made with β€οΈ by @Ahmad Abdelbary