Skip to content

Instantly share code, notes, and snippets.

@cgalvist
Last active October 26, 2023 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgalvist/c9c2c79fb7129cfb7895413658ba234c to your computer and use it in GitHub Desktop.
Save cgalvist/c9c2c79fb7129cfb7895413658ba234c to your computer and use it in GitHub Desktop.
dotnet clean architecture template

Basic .NET template generator (clean architecture) and common files

# PARAMETERS
COMPANY=CompanyName
SYSTEM=SystemName
PROJECT=ProjectName
SOLUTION_NAME=$COMPANY.$SYSTEM.$PROJECT
FRAMEWORK=net6.0

# GENERATE GIT FILES
wget https://www.toptal.com/developers/gitignore/api/linux,macos,windows,aspnetcore,visualstudio,visualstudiocode -O .gitignore
wget https://gitattributes.io/api/common%2Ccsharp%2Cvisualstudio%2Cweb -O .gitattributes
## Add gitignore custom rules
echo '
### Custom properties ###

# Local environment variables file
.env' >> .gitignore

# ADD VSCODE FILES
mkdir .vscode
echo '{
    "recommendations": [
        "ms-dotnettools.csharp",
        "mhutchie.git-graph"
    ]
}' > .vscode/extensions.json
echo '{
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "editor.wordWrap": "on"
}' > .vscode/settings.json

# BUILD .NET PROJECT

## Make code projects
dotnet new classlib -o src/Application/Core --framework $FRAMEWORK
dotnet new classlib -o src/Application/ApplicationCore --framework $FRAMEWORK
dotnet new classlib -o src/Infrastructure/Infrastructure --framework $FRAMEWORK
dotnet new classlib -o src/Infrastructure/Data --framework $FRAMEWORK
dotnet new webapi -o src/Presentation/WebApi --framework $FRAMEWORK --use-program-main --no-https
dotnet new nunit -o src/Tests/UnitTests --framework $FRAMEWORK

## Make solution
dotnet new sln -n $SOLUTION_NAME
dotnet sln add src/Application/Core/
dotnet sln add src/Application/ApplicationCore/
dotnet sln add src/Infrastructure/Infrastructure/
dotnet sln add src/Infrastructure/Data/
dotnet sln add src/Presentation/WebApi/
dotnet sln add src/Tests/UnitTests/

## Add project references
### Core project
dotnet add src/Application/ApplicationCore reference src/Application/Core
dotnet add src/Infrastructure/Infrastructure reference src/Application/Core
dotnet add src/Infrastructure/Data reference src/Application/Core
dotnet add src/Presentation/WebApi reference src/Application/Core
### ApplicationCore project
dotnet add src/Infrastructure/Infrastructure reference src/Application/ApplicationCore
dotnet add src/Infrastructure/Data reference src/Application/ApplicationCore
dotnet add src/Presentation/WebApi reference src/Application/ApplicationCore
### Infrastructure project
dotnet add src/Presentation/WebApi reference src/Infrastructure/Infrastructure
### Data project
dotnet add src/Presentation/WebApi reference src/Infrastructure/Data
### WebApi project
dotnet add src/Tests/UnitTests reference src/Presentation/WebApi

## Delete garbage
find . -type f \( -name "Class1.cs" -o -name "WeatherForecast.cs" -o -name "WeatherForecastController.cs" -o -name "UnitTest1.cs" \) -delete

## Change default values
find . -type f -name "*.csproj" -print0 | xargs -0 sed -i 's/<ImplicitUsings>enable/<ImplicitUsings>disable/g'
find . -type f -name "*.csproj" -print0 | xargs -0 sed -i 's/<Nullable>enable/<Nullable>disable/g'

## Add custom namespaces
sed -i 's/namespace WebApi;/namespace '"$SOLUTION_NAME"'.Presentation.WebApi;/g' src/Presentation/WebApi/Program.cs

sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Application.Core<\/RootNamespace>/g' src/Application/Core/Core.csproj
sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Application.ApplicationCore<\/RootNamespace>/g' src/Application/ApplicationCore/ApplicationCore.csproj
sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Infrastructure.Data<\/RootNamespace>/g' src/Infrastructure/Data/Data.csproj
sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Infrastructure.Infrastructure<\/RootNamespace>/g' src/Infrastructure/Infrastructure/Infrastructure.csproj
sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Presentation.WebApi<\/RootNamespace>/g' src/Presentation/WebApi/WebApi.csproj
sed -i 's/<PropertyGroup>/<PropertyGroup>\n\    <RootNamespace>'"$SOLUTION_NAME"'.Tests.UnitTests<\/RootNamespace>/g' src/Tests/UnitTests/UnitTests.csproj

## Add explicit usings
sed -i '1s/^/using Microsoft.AspNetCore.Builder;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\n/' src/Presentation/WebApi/Program.cs

## Add environment empty files
touch src/Presentation/WebApi/.env
touch src/Presentation/WebApi/sample.env

## Add README
echo "# $SYSTEM $PROJECT" > README.md

## Add Core project folders
mkdir -p src/Application/Core/Constants
mkdir -p src/Application/Core/DTOs
mkdir -p src/Application/Core/Entities
mkdir -p src/Application/Core/Enums
mkdir -p src/Application/Core/Helpers

## Add ApplicationCore project folders
mkdir -p src/Application/ApplicationCore/Config/Validators
mkdir -p src/Application/ApplicationCore/Extensions
mkdir -p src/Application/ApplicationCore/Interfaces/Services
mkdir -p src/Application/ApplicationCore/Interfaces/ExternalServices
mkdir -p src/Application/ApplicationCore/Interfaces/Repositories
mkdir -p src/Application/ApplicationCore/Services
mkdir -p src/Application/ApplicationCore/Specifications

## Add Data project folders
mkdir -p src/Infrastructure/Data/Config
mkdir -p src/Infrastructure/Data/Repositories

## Add WebApi project folders
mkdir -p src/Presentation/WebApi/Config
mkdir -p src/Presentation/WebApi/Controllers/Rest
mkdir -p src/Presentation/WebApi/Controllers/Tools
mkdir -p src/Presentation/WebApi/Features
mkdir -p src/Presentation/WebApi/Helpers/Swagger

Generate vscode assets

In Visual Studio Code, press Ctrl + Shift + p command and execute the .NET: Generate Assets for Build and Debug command for launch.json and tasks.json files generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment