Hello, everyone!
It's being a while.
Are you ready for another epic saga?
This time we will make a complete .NET Core web API by following the TDD (Test Driven Development) steps with xUnit until the DDD (Domain Driven Design) structure.
The complete project can be found here: https://github.com/lucianopereira86/CRUD-NetCore-TDD
Technologies
- Visual Studio 2019
- .NET Core 3.1.0
- xUnit 2.4.0
- Microsoft.EntityFrameworkCore 3.1.0
- FluentValidation 8.6.0
Topics
TDD
What's TDD?
It means Test Driven Development and it consists on programming unit tests for core functionalities of an application before creating classes, projects, validations and other layers.
During the development, there will be a cycle of three steps: Red, Green and Refactor.
The Red step needs the code to fail when executed, even by not compiling.
The Green step consists of a successful compilation and exacly what the unit test was expecting as result.
The Refactor step will be a refactoring of the code to become a new functionality like a new class or module.
With this cycle you will have a clear understanding of all the failures and certainties that your program may have. The problem is that it requires a good amount of time to develop.
Project Structure
Initially, our solution will have 3 layers: web API, Infra and Test.
The web API layer will contain the controllers.
The Infra layer will be responsible for the repositories and table models.
The Test layer will have the unit tests for each funcionality to be built in the other layers.
Now, let's create a solution!
Open the Visual Studio 2019 and create a new .NET Core web application project and name it "CRUD-NETCore-TDD". Choose the API template and uncheck the HTTPS option:
The default .NET Core web API will be created.
Delete the "Controllers/WeatherForecastController.cs" and "WeatherForecast.cs" files. Add a C# .NET Core class library project to the solution named "CRUD-NETCore-TDD.Infra". Delete the "Class1.cs" file as well.
The solution will look like this:
Add a .NET Core xUnit Test Project to the solution and name it "CRUD-NETCore-TDD.Test". Delete the "UnitTest1.cs" file.
The solution will look like this:
Add a reference from the Infra project to the web API project and a reference to the Test project from the other ones.
The solution will look like this:
Next
Let's build the Test project.
Top comments (3)
Tip: Next time put your Visual Studio in English at least for the screenshots :)
This is not really a DDD architecture. And it's better to have 3 layers (3 projects) for your API:
Then create a test project for each of your layer. For instance:
By doing so, you can use your Domain for another projects. The Domain Layer is responsible for representing concepts of the business, information about the business situation, and business rules. It's independent to other Layers
Please use your IDE in English. That may help you reach more people.