Omnia.AutoCodeGenerator
1.1.0
See the version list below for details.
dotnet tool install --global Omnia.AutoCodeGenerator --version 1.1.0
dotnet new tool-manifest
dotnet tool install --local Omnia.AutoCodeGenerator --version 1.1.0
#tool dotnet:?package=Omnia.AutoCodeGenerator&version=1.1.0
nuke :add-package Omnia.AutoCodeGenerator --version 1.1.0
ControllerGenerator
Generatore automatico di codice per creare Controller, Service, Repository e classi correlate basate su modelli Entity Framework.
Installazione
Da NuGet (Raccomandato)
# Installa come CLI tool globale
dotnet tool install --global Omnia.AutoCodeGenerator
# Usa il comando
autocodegen -f "path/to/config.json"
Da Sorgente
# Clona il repository
git clone <repository-url>
cd ControllerGenerator
# Esegui direttamente
dotnet run -- -f "path/to/config.json"
Prerequisiti
- .NET 8.0 o superiore
- Modelli Entity Framework già generati
Utilizzo
Con CLI Tool (NuGet)
autocodegen -f <path-to-config.json>
Con Sorgente
dotnet run -- -f <path-to-config.json>
Esempio di Utilizzo
# Con CLI tool
autocodegen -f "C:\path\to\your\config.json"
autocodegen -f "..\TestAPI\CodeGenerator.json"
# Con sorgente
dotnet run -- -f "C:\path\to\your\config.json"
dotnet run -- -f "..\TestAPI\CodeGenerator.json"
Configurazione JSON
Il generatore utilizza un file di configurazione JSON per specificare i percorsi di input e output e le opzioni di generazione.
Esempio di Configurazione Completa
{
"NamespaceModels": "YourProject.Models",
"OverwriteExistingFiles": true,
"ServiceGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputImplementationFolder": "C:\\path\\to\\Services\\Implementation\\",
"OutputInterfaceFolder": "C:\\path\\to\\Services\\Interfaces\\",
"Namespace": "YourProject.Services"
},
"ServiceBeforeGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputImplementationFolder": "C:\\path\\to\\Services\\Before\\",
"Namespace": "YourProject.Services.Before"
},
"ServiceAfterGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputImplementationFolder": "C:\\path\\to\\Services\\After\\",
"Namespace": "YourProject.Services.After"
},
"RepositoryGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputImplementationFolder": "C:\\path\\to\\Repositories\\Implementation\\",
"OutputInterfaceFolder": "C:\\path\\to\\Repositories\\Interfaces\\",
"Namespace": "YourProject.Repositories",
"Context": "YourDbContext"
},
"UpperServiceGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputImplementationFolder": "C:\\path\\to\\UpperServices\\Implementation\\",
"OutputInterfaceFolder": "C:\\path\\to\\UpperServices\\Interfaces\\",
"Namespace": "YourProject.UpperServices"
},
"ControllerGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputFolder": "C:\\path\\to\\Controllers\\",
"Namespace": "YourProject.Controllers"
},
"DependencyInjectionGen": {
"InputModelsFolder": "C:\\path\\to\\Models\\",
"OutputFolder": "C:\\path\\to\\DI\\",
"Namespace": "YourProject.DI",
"TypeDependency": "Scoped"
},
"SwaggerAuthenticationGen": {
"OutputFolder": "C:\\path\\to\\Extensions\\",
"Namespace": "YourProject.Extensions"
}
}
Parametri di Configurazione
Parametri Globali
| Campo | Tipo | Obbligatorio | Descrizione |
|---|---|---|---|
NamespaceModels |
string | ✅ | Namespace dei modelli Entity Framework |
OverwriteExistingFiles |
bool | ❌ | Se true, sovrascrive i file esistenti. Default: false |
ServiceGen (Obbligatorio)
Genera servizi di base con interfacce.
| Campo | Descrizione |
|---|---|
InputModelsFolder |
Cartella contenente i modelli EF |
OutputImplementationFolder |
Cartella di output per le implementazioni |
OutputInterfaceFolder |
Cartella di output per le interfacce |
Namespace |
Namespace per i servizi generati |
ServiceBeforeGen (Obbligatorio)
Genera servizi "Before" per logica pre-elaborazione.
ServiceAfterGen (Obbligatorio)
Genera servizi "After" per logica post-elaborazione.
RepositoryGen (Obbligatorio)
Genera repository con pattern Repository.
| Campo Speciale | Descrizione |
|---|---|
Context |
Nome della classe DbContext |
UpperServiceGen (Obbligatorio)
Genera servizi di livello superiore che orchestrano Before/After.
ControllerGen (Opzionale)
Genera controller API REST.
DependencyInjectionGen (Opzionale)
Genera configurazione per la dependency injection.
| Campo Speciale | Descrizione |
|---|---|
TypeDependency |
Tipo di dependency: "Scoped", "Singleton", "Transient" |
SwaggerAuthenticationGen (Opzionale)
Genera extension methods per Swagger/OpenAPI con supporto autenticazione JWT Bearer.
| Campo | Descrizione |
|---|---|
OutputFolder |
Cartella di output per le estensioni |
Namespace |
Namespace per le estensioni generate |
Note: Non richiede InputModelsFolder poiché genera utility indipendenti dai modelli.
Gestione Sovrascrittura File
Il generatore supporta due modalità per la gestione dei file esistenti:
Modalità Default (Sicura)
{
"NamespaceModels": "YourProject.Models",
// OverwriteExistingFiles assente o false
}
- ✅ I file esistenti NON vengono sovrascritti
- ✅ Mostra
[SKIP] filename.cs esiste già. - ✅ Sicuro per le modifiche manuali
Modalità Sovrascrittura
{
"NamespaceModels": "YourProject.Models",
"OverwriteExistingFiles": true
}
- ⚠️ I file esistenti VENGONO sovrascritti
- ⚠️ Mostra
[OVERWRITE] filename.cs sovrascritto. - ⚠️ Le modifiche manuali verranno perse
Messaggi di Output
Durante l'esecuzione, il generatore mostra messaggi informativi:
[CREATE] filename.cs creato.- Nuovo file creato[SKIP] filename.cs esiste già.- File esistente non sovrascritto[OVERWRITE] filename.cs sovrascritto.- File esistente sovrascritto
Struttura File Generati
Per ogni modello Customer, il generatore crea:
Controllers
CustomerController.cs
Services
// Interfaces
ICustomerService.cs
ICustomerServiceBefore.cs
ICustomerServiceAfter.cs
ICustomerUpperService.cs
// Implementations
CustomerService.cs
CustomerUpperService.cs
Repositories
// Interface
ICustomerRepository.cs
// Implementation
CustomerRepository.cs
Extensions (SwaggerAuthenticationGen)
SwaggerAuthenticationExtensions.cs
Contiene i seguenti extension methods:
AddSwaggerWithAuthentication()- Configura Swagger/OpenAPI con JWT BearerAddPermissiveAuthentication()- Autenticazione permissiva per sviluppoUseSwaggerWithUI()- Configura Swagger UI personalizzabile
Esempio di utilizzo in Program.cs:
// Aggiungi servizi
builder.Services.AddSwaggerWithAuthentication(
title: "My API",
version: "v1",
description: "API Documentation"
);
builder.Services.AddPermissiveAuthentication();
// Configura pipeline
app.UseSwaggerWithUI(
title: "My API Documentation",
version: "v1",
routePrefix: "" // Swagger alla radice
);
app.UseAuthentication();
app.UseAuthorization();
Esempio Pratico
- Preparare i modelli EF in una cartella (es.
Models/) - Creare il file di configurazione
config.json - Eseguire il generatore:
dotnet run -- -f config.json - Verificare i file generati nelle cartelle specificate
Note Importanti
- Il generatore esclude automaticamente i file che contengono "Context" nel nome
- Tutti i percorsi devono essere assoluti
- Le cartelle di output vengono create automaticamente se non esistono
- La compatibilità è garantita con file JSON esistenti (retrocompatibile)
Risoluzione Problemi
Errore: "File non trovato"
Verificare che il percorso del file JSON sia corretto e accessibile.
Errore: "Campo XXX non può essere nullo"
Verificare che tutti i campi obbligatori siano presenti nel JSON.
File non generati
Verificare che:
- La cartella dei modelli contenga file .cs
- I percorsi di output siano scrivibili
- Non ci siano conflitti di permessi
Help
Per visualizzare l'help:
dotnet run -- --help
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
Added SwaggerAuthenticationGen for automatic Swagger/JWT setup. See SwaggerAuthenticationGen-README.md for details.