WebApis Using DDD

The DDD architecture separates your core business logic (the “Domain”) from technical details like databases (the “Infrastructure”) or web frameworks (the “API”).

Unlike using a flat architecture using a DDD architecture requires your code to have a central entry point which will be your API this will be your base project and with contain the Program.cs. This project will be of the type webapi. We will have other projects which will be of the type classlib. These will complement your base code with their respective functions.

Creating your solution

Let’s assume your solution is named Project1 so this will how you shall create the various projects within your solution. Best practices is to capitalize the solution name and the projectname.

Here is how you can do it using the dotnet CLI

  • Creating your base solution
    1
    
    dotnet new sln -n Project1
    
  • Creating your base project
    1
    
    dotnet new webapi -n Project1.Api
    
  • Creating the complementing solutions
    1
    2
    3
    4
    
    dotnet new classlib -n Project1.Domain
    dotnet new classlib -n Project1.Application
    dotnet new classlib -n Project1.Infrastructure
    dotnet new classlib -n Project1.Client
    
  • Adding your various projects to the solution
    • Doing it manually
      1
      2
      3
      4
      5
      
      dotnet sln add MyProject.Domain/MyProject.Domain.csproj
      dotnet sln add MyProject.Application/MyProject.Application.csproj
      dotnet sln add MyProject.Infrastructure/MyProject.Infrastructure.csproj
      dotnet sln add MyProject.Api/MyProject.Api.csproj
      dotnet sln add MyProject.Client/MyProject.Client.csproj
      
    • Recursively adding the files in linux based systems
      1
      
      dotnet sln add **/*.csproj
      
    • Recursively adding the files in windows based systems (powershell)
      1
      
      Get-ChildItem -Recurse *.csproj | ForEach-Object { dotnet sln add $_.FullName }
      

You can also do it via an IDE if doing from a GUI seems better.