DEV Community

Cover image for From TDD to DDD: Building a .NET Core Web API - Part 1
LUCIANO DE SOUSA PEREIRA
LUCIANO DE SOUSA PEREIRA

Posted on

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.

Oldest comments (3)

Collapse
 
smartcodinghub profile image
Oscar

Tip: Next time put your Visual Studio in English at least for the screenshots :)

Collapse
 
oscarcentenor profile image
Oscar Centeno

Please use your IDE in English. That may help you reach more people.

Collapse
 
wariored profile image
Cheikh Tidjane Konteye

This is not really a DDD architecture. And it's better to have 3 layers (3 projects) for your API:

  • projectName.Api (Application Layer)
  • projectName.Domain (Domain Model Layer)
  • projectName.Infrastructure (Infrastructure Layer).

Then create a test project for each of your layer. For instance:

  • projectName.Api.Tests
  • projectName.Domain.Tests
  • projectName.Infrastructure.Tests

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