From TDD to DDD: Building a .NET Core Web API - Part 1
Tags: tdd, ddd, netcore, xunit
Published: 2019-12-16 12:22:10

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](https://github.com/lucianopereira86/CRUD-NetCore-TDD)

## Technologies

- [Visual Studio 2019](https://visualstudio.microsoft.com/pt-br/downloads/)
- [.NET Core 3.1.0](https://dotnet.microsoft.com/download/dotnet-core/3.1)
- [xUnit 2.4.0](https://www.nuget.org/packages/xunit/2.4.0)
- [Microsoft.EntityFrameworkCore 3.1.0](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/3.1.0)
- [FluentValidation 8.6.0](https://www.nuget.org/packages/fluentvalidation)

## Topics

- [TDD](#tdd)
- [Project Structure](#project-structure)

## TDD

What's TDD?  
It means [Test Driven Development](https://en.wikipedia.org/wiki/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](https://thepracticaldev.s3.amazonaws.com/i/8c9pvcdrk9p9o5a1t0jj.JPG)

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](https://thepracticaldev.s3.amazonaws.com/i/6ewkl1vmtz1tvfelsj4o.JPG)

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](https://thepracticaldev.s3.amazonaws.com/i/56g6349ao58maunwy1zp.JPG)

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](https://thepracticaldev.s3.amazonaws.com/i/hjn0nknd646ima520ebp.JPG)

## Next

Let's build the Test project.