Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 9
.NET CLI & Project Structure
The .NET CLI is the primary tool for creating, building, and publishing ASP.NET Core applications in CI/CD pipelines. Understanding project structure and the solution model is the foundation for Clean Architecture.
CLI Commands & Clean Architecture Layout
# ━━ Create ASP.NET Core Web API project ━━
dotnet new webapi -n MyApp.Api --use-controllers
dotnet new classlib -n MyApp.Application
dotnet new classlib -n MyApp.Domain
dotnet new classlib -n MyApp.Infrastructure
# ━━ Wire up solution ━━
dotnet new sln -n MyApp
dotnet sln add MyApp.Api/MyApp.Api.csproj
dotnet sln add MyApp.Application/MyApp.Application.csproj
dotnet sln add MyApp.Domain/MyApp.Domain.csproj
dotnet sln add MyApp.Infrastructure/MyApp.Infrastructure.csproj
# ━━ Add project references (dependency direction: inward) ━━
dotnet add MyApp.Api/MyApp.Api.csproj reference MyApp.Application/
dotnet add MyApp.Application/MyApp.Application.csproj reference MyApp.Domain/
dotnet add MyApp.Infrastructure/MyApp.Infrastructure.csproj reference MyApp.Application/
# ━━ Add NuGet packages ━━
dotnet add MyApp.Infrastructure/ package Microsoft.EntityFrameworkCore.SqlServer
dotnet add MyApp.Infrastructure/ package Microsoft.EntityFrameworkCore.Tools
dotnet add MyApp.Api/ package Serilog.AspNetCore
dotnet add MyApp.Api/ package Swashbuckle.AspNetCore
# ━━ Clean Architecture folder layout ━━
# MyApp.Domain/ ← zero dependencies - only C# classes
# Entities/
# Interfaces/
# Enums/
# ValueObjects/
# MyApp.Application/ ← depends on Domain only
# Features/Products/ ← CQRS feature folders
# Queries/
# Commands/
# Interfaces/ ← IRepository, IEmailService (abstractions)
# MyApp.Infrastructure/ ← depends on Application
# Persistence/ ← EF Core DbContext, migrations
# Repositories/
# Services/ ← Email, file storage, etc.
# MyApp.Api/ ← depends on Application (not Infrastructure!)
# Controllers/
# Middleware/
# Program.cs
# ━━ Publish self-contained executable ━━
dotnet publish MyApp.Api -c Release -r linux-x64 --self-contained true -o publish/
# ━━ Run EF Core migrations ━━
dotnet ef migrations add InitialCreate --project MyApp.Infrastructure --startup-project MyApp.Api
dotnet ef database update --project MyApp.Infrastructure --startup-project MyApp.ApiTip
Tip
Practice NET CLI Project Structure in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
.NET provides a unified platform for building various application types
Practice Task
Note
Practice Task - (1) Write a working example of NET CLI Project Structure from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with NET CLI Project Structure is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.
Finished this lesson?