CleanArchitectureProgramGenerator 8.1.2
dotnet new install CleanArchitectureProgramGenerator::8.1.2
Clean Architecture Style Code Generator
This is a .Net8.0 Clean Architecture style code generator which will generate Domain project containing entities; Application project containing repository interfaces, service interfaces, service implementations; Persistence project containg ApplicationDbcontext, repository implementations; it will also generate controllers for each entity in the WebAPI or WEBMVC project.
A working WEBAPI and WEBMVC project pointing a database with multiple tables can be generated and smoke tested under 4 minutes.
Version 8.1.0 and above supports Generating a FrontEnd in Angular 18 this make use of the WebAPI backend, all of these are generated with this program generator.
Table of Contents
- Getting Started
- Github Link
- Installing template from Nuget Package Manager
- How to check if this really works
- Adding new entities
- Some useful commands
Getting Started
This is a NuGet package that should be installed as a template in dotnet cli.
You can browse this package using manage NuGet packages by searching for "Thomson Mathews" or "CleanArchitectureProgramGenerator"
This will install four template files in your project.
Clean Architecture Angular Generator.tt
Clean Architecture Database Access Generator.tt
Clean Architecture WebAPI Generator.tt
Clean Architecture WebMVC Generator.tt
Once these template files are installed.
Run Clean Architecture Database Access Generator.tt template by right clicking on the template and select "Run Custom Tool".
This will generate the following projects.
Now define entities in the Domain Project entities folder using code first approach or database first approach and ApplicationDbContext in Persistence project context folder.
Run Clean Architecture Database Access Generator.tt again to generate all repositories, services for the each entity that was added in the previous step.
Run Clean Architecture WebAPI Generator.tt to generate API controllers in WebAPI project.
Run Clean Architecture WebMVC Generator.tt to generate MVC controllers in WebMVC project.
Run Clean Architecture Angular Generator.tt to generate Agular 18 frontend project.
set WebAPI or WebMVC or AngularProject1 as a startup project.
Voila! a working WebAPI or WebMVC or AngularProject1 is ready for you.
if more entities are added, just run all these three transformation again, that's it.
Clean Architecture Database Access Generator.tt
This template will generate 4 projects:
Domain, Application, Persistence, Startup.
Domain Project:
Define all entities in Entities folder, either using code first approach or database approach, Exceptions folder defines some basic useful exceptions that can be used in global error handling. This project also refers to the latest Microsoft EntityFramework
Application Project
Defines Interface for ServiceManager, Services in the ServiceInterfaces folder, implementation of ServiceManager and Services in Services folder, interfaces for RepositoryManager, GenericRepository, Repositories and UnitOfWork in RepositoryInterface folder.
Persistence Project:
ApplicationDbContext should be defined or generated using Entity Framework in the context folder, Repositories folder contains GenericRepository, Repositories, RepositoryManager and UnitOfWork implementation.
Startup Project:
This is a console application that will create a Generic Host which will help in dependency injection, here ApplicationDbContext is added to the ServiceCollection, this project will help you to run Entity Framework commands to do entity migrations to a database or generate entities and ApplicationDbContext from existing database. It also has a appsettings.json file containing the connection string.
Startup project is only used to run Entity Framework commands with out a WebAPI project and Startup project is not required if WebAPI project is generated.
Once you add more entities, you can run this template again to generate repository interfaces for the new Entities in the Domain Project, repository implementation for the new Entities in Persistence Project and Services and its Service interfaces for the new Entities in the Application Project.
Clean Architecture WebAPI Generator.tt
This template will open a dialog box, select "ASP.NET Core Web API" project template to generate a initial skelton of Web API project which will be modified by the "Clean Architecture WebAPI Generator template"
This template will generate open a dialog box, select ASP.NET Core WebAPI project and it will create a project named WebAPI
WebAPI Project:
This contains controllers and other required classes.
Controller Folder:
Contains generated controller for each entities defined in Domain project.
Extensions Folder:
Contains extension method to add BasicAuthMiddleware to the HTTP request pipeline.
MiddleWare Folder:
Contains implementation for BasicAuthMiddleware and ExceptionHandlingMiddleware.
Appsetting.json:
Contains configuration for Serilog logging, connection string and basic authentication details.
Appsetting Folder:
Contains classes structures to read from appsettings.json file.
Program.cs
This connect all the wiring between the projects.
Once you add more entities, you can run this template again to generate controllers for the new Entities in the WebAPI Project.
Clean Architecture WebMVC Generator.tt
This template will open a dialog box, select "ASP.NET Core Web app (Mode-View-Controller)" project template to generate a initial skelton of MVC project which will be modified by the "Clean Architecture WebMVC Generator template" it will create a project named WebAPI
WebAPI Project:
This contains controllers and other required classes.
Controller Folder:
Contains generated controller for each entities defined in Domain project.
Extensions Folder:
Contains extension method to add BasicAuthMiddleware to the HTTP request pipeline.
MiddleWare Folder:
Contains implementation for BasicAuthMiddleware and ExceptionHandlingMiddleware.
Appsetting.json:
Contains configuration for Serilog logging, connection string and basic authentication details.
Appsetting Folder:
Contains classes structures to read from appsettings.json file.
Program.cs This connect all the wiring between the projects.
Once you add more entities, you can run this template again to generate controllers for the new Entities in the WebAPI Project.
Clean Architecture Angular Generator.tt
To make use of this generator, you need to have Angular CLI installed in your system.
npm install –g @angular/cli
Next step is to create a Standalone TypeScript Angular Project in Visual Studio
This will create a project named AngularProject1, (Important: Leave the project name as AngularProject1).
this will create the following structure in the solution explorer.
Go to your Developer PowerShell and change directory to the AngularProject1 folder.
Now install these packages
npm install bootstrap
npm install ngx-pagination
Right click on the Clean Architecture Angular Generator.tt and select "Run Custom Tool"
This will generate module and its component for each entity defined in the Domain project.
T4Helper.ttinclude
This file contains function used by the above T4Template files.
Github Link
https://github.com/tcj2001/CleanArchitectureProgramGenerator
Installing template from Nuget Package Manager
From your package manager console.
dotnet new install CleanArchitectureProgramGenerator
if you already have a older version installed
dotnet new uninstall CleanArchitectureProgramGenerator
dotnet new install CleanArchitectureProgramGenerator
This will add a new template in your dotnet cli environment.
dotnet new list
you can use the above command to check if the template was installed.
CAPG is shortname for the template
There are two way to use the template.
First Method.
Use command Prompt to a directory where you want to make use create this template project.
dotnet new CAPG -o yourProjectSolution
Double click on the Project, this will open Visual Studio with a Solution containing the projects CleanArchitectureProgramGenerator.
Rename the solution to your solution name.
Second Method.
When you select new project in visual studio you will see a template named CleanArchitectureProgramGenerator, if you don't see filter the project type and select Clean Architecture, select this template and create your project.
This will create a ClassLibrary project named CleanArchitectureProgramGenerator within Solution.
How to check if this really works
Once all the Data related projects are generated using the "Clean Architecture Database Access Generator.tt" template.
Domain project by default will be provided one entity named Sample and it's Repository and Service.
When you add new entities in the Domain project under Entities folder:
The classes highlighted in green will get generated for each entity defined in the Domain project entities folder.
The Classes highlighted in red gets re-generated every time with entity details.
Startup project adds ApplicationDbContext to the service collection for dependency injection and points to a database, change the the connection string as needed, before you proceed to next step.
Code First approach
Since we already have a sample entity defined for you by the template, let's just make use of it.
In the Startup project modify appsettings.json to edit the connection string.
dotnet ef migrations add "Add Sample entity to Database" --project Persistence --startup-project Startup
dotnet ef database update --project Persistence --startup-project Startup
These commands will add Sample table to the database.
Note: Here Startup project is used to run Entity Framework commands.
Now use Clean Architecture WebAPI Generator.tt to generate WebAPI project. This will also create a controller for the Sample entity in the project.
Update the WebAPI projects appsetting.json to point to the correct database.
Set WebAPI as a startup project and run it.
Database First approach
Here we will make use of a BookStoresDB database in a Sql Server setup. This database was grabbed from internet, credit goes to the person who made it available for community.
In the Startup project modify appsettings.json to edit the connection string.
Run this command to generate entities in the domain\Entities folder and ApplicationDbContect in Persitence\Context folder.
scaffold-DbContext -Connection "Name=SqlServerDB" -Provider Microsoft.EntityFrameworkCore.SqlServer -Project Persistence -StartupProject Startup -OutputDir ..\Domain\Entities -Context ApplicationDbContext -ContextDir ..\Persistence\Context -Namespace Domain.Entities -ContextNamespace Persistence.Context -DataAnnotations -force
*note: if scaffold-DbContext uses a Named connection string, you won't be able to add controllers using visual studio in-built scaffolded mechanism, you can avoid that by providing connection string directly in the scaffold-DbContext command.
This will generate all entities in domain project
Run template:
Clean Architecture Database Access Generator.tt
To generate repositories and Services for the new entities
Run template:
Clean Architecture WebAPI Generator.tt
To generate controllers for WebAPI project.
Update the WebAPI projects appsetting.json to point to the correct database.
Set WebAPI as a startup project and run it.
By default the WebAPI uses BasicAuthentication the userid and password is defined in the appsettings.json file
Run template:
Clean Architecture WebMVC Generator.tt
To generate controllers for WebMVC project.
Run template:
Clean Architecture Angular Generator.tt
This will generate module and its component for each entity defined in the Domain project.
This is going to make use of the WebAPI project to get data from the database, so make sure you have the WebAPI project running.
Now edit the proxy.conf.json file to point to the WebAPI url
Set AngularProject1 as a startup project and run it and you will see the Angular project handling the CRUD operation of each entities.
Some useful commands
Code First Migrations
dotnet ef migrations add "initialmigration1" --project Persistence --startup-project Startup
dotnet ef migrations add "initialmigration2" --project Persistence --startup-project WebAPI
dotnet ef database update --project Persistence --startup-project Startup
dotnet ef database update --project Persistence --startup-project WebAPI
Database First Migrations
scaffold-DbContext -Connection "Server=DESKTOP-GBANT4V; Database=BookStoresDB; Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -Project Persistence -StartupProject Startup -OutputDir ..\Domain\Entities -Context ApplicationDbContext -ContextDir ..\Persistence\Context -Namespace Domain.Entities -ContextNamespace Persistence.Context -DataAnnotations -Force
If possible do not use connection string directly in the above command.
if you have an appsettings.json define connection strings as shown
"ConnectionStrings": {
"SqlServerDB": "Server=DESKTOP-GBANT4V; Database=BookStoresDB; Trusted_Connection=True;"
}
then use "Name=SqlServerDB" in the scaffold-DbContext -Connection parameter
Remove migration
dotnet ef migrations remove
List migrations
dotnet ef migrations list
Install Entity Framework
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef
Create a solution with the current director name
dotnet new solution
Create a startup project if webapi or mvc not used
dotnet new worker -n Startup
Create class library
dotnet new classlib -o projectName
Add solution file
dotnet sln add SomeFolder/SomeProject.csproj AnotherFolder/AnotherProject.csproj
Add project dependency
dotnet add SomeFolder/SomeProject.csproj reference AnotherFolder/AnotherProject.csproj
Add package dependencies
dotnet add SomeFolder/SomeProject.csproj package microsoft.entityframeworkcore
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.