This repository contains a sample API project built using the Clean Architecture principles, Onion Architecture, MediatR, and Entity Framework. The project also includes unit tests for all layers and integration tests using xUnit and Nsubstitute.
The purpose of this project is to create a clean boilerplate for an API and to show how to implement specific features.
The project follows the Onion Architecture, which means that the codebase is organized into layers, with the domain model at the center and the outer layers dependent on the inner layers.
The project has the following structure:
- Domain: Contains the domain model, which represents the core business logic of the application. It includes entities, value objects, domain services, and domain events.
- Application: Contains the application layer, which implements the use cases of the system. It includes commands, queries, handlers, and DTOs.
- Infrastructure: Contains the infrastructure layer, which implements the technical details of the system. It includes database access, logging, configuration, and external services.
- API: Contains the presentation layer, which exposes the functionality of the system to the outside world. It includes controllers, action results, and view models.
The project uses the following dependencies:
- MediatR: A lightweight library that provides a mediator pattern implementation for .NET.
- Entity Framework Core: A modern object-relational mapper for .NET that provides data access to the application.
- FluentValidation: A validation library that provides a fluent API for validating objects.
- gRPC: gRPC is an open-source remote procedure call framework that enables efficient communication between distributed systems using a variety of programming languages and protocols.
To run the project, follow these steps:
- Clone the repository to your local machine.
- Open the solution in your IDE of choice.
- Build the solution to restore the dependencies.
- Update the connection string in the appsettings.json file to point to your database.
- Start the API project
- The database migrations will be automatically applied on start-up. If the database does not exist, it will be created.
- The API should be accessible at
https://localhost:<port>/api/<controller>
where<port>
is the port number specified in the project properties and<controller>
is the name of the API controller.
Requirements
This is only needed if running the API locally or only the docker image
- Redis:
docker run --name redis -d -p 6379:6379 -e ALLOW_EMPTY_PASSWORD=yes redis:latest
- Add this to the redis configuration in the Program.cs
options.ConfigurationOptions = new ConfigurationOptions
{
AbortOnConnectFail = false,
EndPoints = { "localhost", "6379" }
};
- RabbitMq:
docker run --name rabbitmq -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Running the container
- Build the Dockerfile:
docker build -t clean-architecture .
- Run the Container:
docker run -p 80:80 clean-architecture
- Build the Dockerfile:
docker build -t clean-architecture .
- Running the docker compose:
docker-compose up -d
(Delete:docker-compose down
)
- Build the docker image and push it to the docker hub (Change the image name in the
k8s-deployment.yml
to your own) - Apply the deployment file:
kubectl apply -f k8s-deployment.yml
(Delete:kubectl delete -f k8s-deployment.yml
)
To run the tests, follow these steps:
- Open the solution in your IDE of choice.
- Build the solution to restore the dependencies.
- Open the Test Explorer window
- Run all the tests by clicking the Run All button in the Test Explorer.
This project uses GitHub Actions to build and test the project on every commit to the main branch. The workflow consists of several steps, including restoring packages, building the project and running tests.
This project is a sample implementation of the Clean Architecture principles, Onion Architecture, MediatR, and Entity Framework. It demonstrates how to organize a .NET 9 API project into layers, how to use the MediatR library to implement the mediator pattern, and how to use Entity Framework to access data. It also includes unit tests for all layers and integration tests using xUnit.