In this article, we are going to learn how to implement Clean Architecture on .NET. We are going to create 3 projects: Application Core, Infrastructure, and Web API.
Prerequisites
- Visual Studio 2019 with .NET 5 SDK
- SQL Server Database
1. Create Application Core project
Create a blank solution named "StoreCleanArchitecture" and add a solution folder named "src", inside create a Class library project (create the src folder the directory project as well) with .NET Standard 2.1
Create the following folders,
- DTOs
- Entities
- Exceptions
- Interfaces
- Mappings
- Utils
Install AutoMapper.Extensions.Microsoft.DependencyInjection.
Create DependencyInjection class.
In Entities folder, create Product class.
In DTOs folder, create Product class to specify the requests and response.
In Mappings folder, create GeneralProfile class. This is useful to map automatically from the Request to the Entity and from the Entity to the Response.
In Interfaces folder, create IProductRepository interface. Here we create the methods for the CRUD.
In Exceptions folder, create NotFoundException class.
In Utils folder, create DateUtil class.
2. Create Infrastructure project
Create a Class library project with .NET 5, named Store.Infrastructure.
Create Persistence folder, and inside create Contexts and Repositories folders.
Install Microsoft.EntityFrameworkCore.SqlServer.
Right click on Store.Infrastucture project / Add / Project Reference ... / Check Store.ApplicationCore / OK.
In Contexts folder, create StoreContext class. Here we add Product entity to the DbSets in order to communicate with the database to the Products table.
In Repositories folder, create ProductRepository class.
Create DependencyInjection class, add the following:
There we are configuring the db context and adding IProductRepository to the services collection as Scoped.
3. Create Web API project
Create a "Web API project" with .NET 5, named Store.WebApi.
Right click on Store.WebApi / Set as Startup project.
At the top, click on Debug / Start Without Debugging.
Remove WeatherForecast and WeatherForecastController files.
Add the references to the Store.ApplicationCore and Store.Infrastructure projects.
Add the connection string to SQL Server in appsettings.json.
In Startup class, in ConfigureServices method add the extensions for Application Core and Infrastructure.
Open Package Manager Console and select Store.Infrastructure project as default. Execute Add-Migration InitialCreate -Context StoreContext
.
In Store.Infrastructure project, a Migrations folder with 2 files inside were created.
Then, from the Package Manager Console, execute Update-Database
. From Controllers, add a controller named ProductsController.
Now, you can test the API.
Thanks for reading
Thank you very much for reading, I hope you found this article interesting and may be useful in the future. If you have any questions or ideas that you need to discuss, it will be a pleasure to be able to collaborate and exchange knowledge together.
No comments:
Post a Comment