Omnia.AutoCodeGenerator
1.1.1
dotnet tool install --global Omnia.AutoCodeGenerator --version 1.1.1
dotnet new tool-manifest
dotnet tool install --local Omnia.AutoCodeGenerator --version 1.1.1
#tool dotnet:?package=Omnia.AutoCodeGenerator&version=1.1.1
nuke :add-package Omnia.AutoCodeGenerator --version 1.1.1
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": "Test.Models",
"OverwriteExistingFiles": true,
"RepositoryInterfaceGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\Interfaces\\Repository\\",
"Namespace": "TestGenerated",
"Usings": ["InterfaceUtility.GenericInterfaces"]
},
"ServiceInterfaceGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\Interfaces\\Service\\",
"Namespace": "TestGenerated",
"Usings": ["InterfaceUtility.GenericInterfaces"]
},
"UpperServiceInterfaceGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\Interfaces\\UpperService\\",
"Namespace": "TestGenerated",
"Usings": ["InterfaceUtility.GenericInterfaces"]
},
"ServiceGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\",
"Namespace": "TestGenerated",
"Usings": ["Utility.ServiceRepository"]
},
"ServiceBeforeGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\",
"Namespace": "TestGenerated",
"Usings": ["InterfaceUtility.GenericInterfaces"]
},
"ServiceAfterGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\",
"Namespace": "TestGenerated",
"Usings": ["InterfaceUtility.GenericInterfaces"]
},
"RepositoryGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\",
"Namespace": "TestGenerated",
"Context": "TestContext",
"RepositoryBaseClass": "GenericRepository",
"Usings": ["GenericImplementation.Service"]
},
"UpperServiceGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\Test\\Generated\\",
"Namespace": "TestGenerated",
"Usings": ["GenericImplementation.Service"]
},
"ControllerGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\TestAPI\\Controllers\\",
"Namespace": "TestGenerated",
"Usings": [
"InterfaceUtility.GenericInterfaces",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc",
"Utility.GenericControllerImplementation"
]
},
"DependencyInjectionGen": {
"InputModelsFolder": "C:\\...\\Test\\Models\\",
"OutputFolder": "C:\\...\\TestAPI\\",
"Namespace": "TestGenerated",
"TypeDependency": "Scoped"
},
"SwaggerAuthenticationGen": {
"OutputFolder": "C:\\...\\TestAPI\\Extensions\\",
"Namespace": "TestGenerated"
}
}
Parametri di Configurazione
Parametri Globali
Parametri Globali
| Campo | Tipo | Obbligatorio | Descrizione |
|---|---|---|---|
NamespaceModels |
string | ✅ | Namespace dei modelli Entity Framework |
OverwriteExistingFiles |
bool | ❌ | Se true, sovrascrive i file esistenti. Default: false |
Blocchi Generatori
Ogni blocco (es. ServiceGen, RepositoryGen, ControllerGen, ecc.) accetta:
| Campo | Tipo | Obbligatorio | Descrizione |
|---|---|---|---|
InputModelsFolder |
string | ✅ | Cartella contenente i modelli EF |
OutputFolder |
string | ✅ | Cartella di output per i file generati |
Namespace |
string | ✅ | Namespace per i file generati |
Usings |
string[] | ❌ | Array di usings da includere nei file |
Context |
string | Solo RepositoryGen | Nome della classe DbContext |
RepositoryBaseClass |
string | Solo RepositoryGen | Base class per i repository |
TypeDependency |
string | Solo DependencyInjectionGen | Tipo di dependency injection |
Note: I generatori di interfaccia (RepositoryInterfaceGen, ServiceInterfaceGen, UpperServiceInterfaceGen) sono indipendenti e accettano solo i parametri sopra.
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
Interfacce Generiche
// RepositoryInterfaceGen
ICustomerRepository.cs
// ServiceInterfaceGen
ICustomerService.cs
// UpperServiceInterfaceGen
ICustomerUpperService.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.
README aggiornato e verificato, esempi CLI e JSON completi, documentazione SwaggerAuthenticationGen inclusa. Versione 1.1.1.