0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-30 10:33:43 +00:00
Sample .NET 9 API project including Clean Architecture principles, Onion Architecture, MediatR, and Entity Framework with unit and integration tests using xUnit
Go to file
2023-08-14 16:52:19 +02:00
.github/workflows fix: Add ref for workflow 2023-08-14 16:52:19 +02:00
CleanArchitecture.Api test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Application test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Application.Tests test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Domain chore: Cleanup 2023-08-09 20:25:38 +02:00
CleanArchitecture.Domain.Tests chore: Cleanup 2023-08-09 20:25:38 +02:00
CleanArchitecture.gRPC Code cleanup and dependency updates 2023-07-01 13:26:10 +02:00
CleanArchitecture.gRPC.Tests test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Infrastructure test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Infrastructure.Tests chore: Cleanup 2023-08-09 20:25:38 +02:00
CleanArchitecture.IntegrationTests chore: Cleanup 2023-08-09 20:25:38 +02:00
CleanArchitecture.Proto test: Replace Moq with NSubstitute 2023-08-09 19:20:44 +02:00
CleanArchitecture.Shared Remake the gRPC implementatin properly 2023-03-23 18:37:24 +01:00
.dockerignore Add Docker support 2023-03-09 18:04:37 +01:00
.gitignore Add query handler tests 2023-03-07 23:26:47 +01:00
CleanArchitecture.sln Remake the gRPC implementatin properly 2023-03-23 18:37:24 +01:00
docker-compose.yml Add health checks 2023-07-15 13:33:11 +02:00
Dockerfile Add health checks 2023-07-15 13:33:11 +02:00
k8s-deployment.yml Use my image for the k8s file 2023-07-15 14:23:29 +02:00
LICENSE Create LICENSE 2023-07-15 13:44:21 +02:00
Readme.md Add correct connection string informations 2023-07-15 15:14:43 +02:00

Clean Architecture Dotnet 7 API Project

GitHub Workflow Status

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.

Project Structure

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.

Dependencies

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.

Running the Project

To run the project, follow these steps:

  1. Clone the repository to your local machine.
  2. Open the solution in your IDE of choice.
  3. Build the solution to restore the dependencies.
  4. Update the connection string in the appsettings.json file to point to your database.
  5. Start the API project
  6. The database migrations will be automatically applied on start-up. If the database does not exist, it will be created.
  7. 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.

Using docker

  1. Build the Dockerfile: docker build -t clean-architecture .
  2. Run the Container: docker run -p 80:80 clean-architecture

Using docker-compose

  1. Change the ConnectionString in the appsettings.json to Server=db;Database=clean-architecture;Trusted_Connection=False;MultipleActiveResultSets=true;TrustServerCertificate=True;User Id=SA;Password=Password123!#
  2. Build the Dockerfile: docker build -t clean-architecture .
  3. Running the docker compose: docker-compose up -d (Delete: docker-compose down)

Using Kubernetes

  1. Change the ConnectionString in the appsettings.json to Server=clean-architecture-db-service;Database=clean-architecture;Trusted_Connection=False;MultipleActiveResultSets=true;TrustServerCertificate=True;User Id=SA;Password=Password123!#
  2. 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)

Running the Tests

To run the tests, follow these steps:

  1. Open the solution in your IDE of choice.
  2. Build the solution to restore the dependencies.
  3. Open the Test Explorer window
  4. Run all the tests by clicking the Run All button in the Test Explorer.

Continuous Integration

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.

Conclusion

This project is a sample implementation of the Clean Architecture principles, Onion Architecture, MediatR, and Entity Framework. It demonstrates how to organize a .NET 7 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.