From TDD to DDD: Building a .NET Core Web API - Part 1

lucianopereira86

LUCIANO DE SOUSA PEREIRA

Posted on December 16, 2019

From TDD to DDD: Building a .NET Core Web API - Part 1

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

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:

print02

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:

print03

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:

print04

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:

print05

Next

Let's build the Test project.

💖 💪 🙅 🚩
lucianopereira86
LUCIANO DE SOUSA PEREIRA

Posted on December 16, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related