From 40193db94f587f84b85e04df96a3047e3677f7b8 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 19 Mar 2026 14:36:03 +0100 Subject: [PATCH] Semplificazione init Repo/Servizi x DI --- .../DataServiceCollectionExtensions.cs | 95 +++++++++++++++ Lux.API/Program.cs | 96 +--------------- Lux.UI/Program.cs | 108 +----------------- 3 files changed, 103 insertions(+), 196 deletions(-) create mode 100644 EgwCoreLib.Lux.Data/DataServiceCollectionExtensions.cs diff --git a/EgwCoreLib.Lux.Data/DataServiceCollectionExtensions.cs b/EgwCoreLib.Lux.Data/DataServiceCollectionExtensions.cs new file mode 100644 index 0000000..574d898 --- /dev/null +++ b/EgwCoreLib.Lux.Data/DataServiceCollectionExtensions.cs @@ -0,0 +1,95 @@ +using EgwCoreLib.Lux.Data.Repository.Config; +using EgwCoreLib.Lux.Data.Repository.Cost; +using EgwCoreLib.Lux.Data.Repository.Items; +using EgwCoreLib.Lux.Data.Repository.Job; +using EgwCoreLib.Lux.Data.Repository.Production; +using EgwCoreLib.Lux.Data.Repository.Sales; +using EgwCoreLib.Lux.Data.Repository.Utils; +using EgwCoreLib.Lux.Data.Services; +using EgwCoreLib.Lux.Data.Services.Config; +using EgwCoreLib.Lux.Data.Services.Cost; +using EgwCoreLib.Lux.Data.Services.Items; +using EgwCoreLib.Lux.Data.Services.Job; +using EgwCoreLib.Lux.Data.Services.Production; +using EgwCoreLib.Lux.Data.Services.Sales; +using EgwCoreLib.Lux.Data.Services.Utils; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace EgwCoreLib.Lux.Data +{ + public static class DataServiceCollectionExtensions + { + public static IServiceCollection AddLuxData(this IServiceCollection services, string connectionString) + { + //// DbContextFactory: preferibile in Blazor Server e scenari concorrenti + //services.AddDbContextFactory(options => + // options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))); + + // Repository con interfaccia + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + + // Servizi dati con interfaccia + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + + // Facade / DataLayerService + //builder.Services.AddSingleton(); + services.TryAddScoped(); + //services.TryAddScoped(); + + return services; + } + } +} diff --git a/Lux.API/Program.cs b/Lux.API/Program.cs index 50ec9dd..3888bb2 100644 --- a/Lux.API/Program.cs +++ b/Lux.API/Program.cs @@ -1,19 +1,5 @@ using EgwCoreLib.Lux.Data; -using EgwCoreLib.Lux.Data.Repository.Config; -using EgwCoreLib.Lux.Data.Repository.Cost; -using EgwCoreLib.Lux.Data.Repository.Items; -using EgwCoreLib.Lux.Data.Repository.Job; -using EgwCoreLib.Lux.Data.Repository.Production; -using EgwCoreLib.Lux.Data.Repository.Sales; -using EgwCoreLib.Lux.Data.Repository.Utils; using EgwCoreLib.Lux.Data.Services; -using EgwCoreLib.Lux.Data.Services.Config; -using EgwCoreLib.Lux.Data.Services.Cost; -using EgwCoreLib.Lux.Data.Services.Items; -using EgwCoreLib.Lux.Data.Services.Job; -using EgwCoreLib.Lux.Data.Services.Production; -using EgwCoreLib.Lux.Data.Services.Sales; -using EgwCoreLib.Lux.Data.Services.Utils; using Lux.API.Services; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; @@ -169,86 +155,8 @@ builder.Services.AddDbContextFactory(options => .LogTo(_ => { }); // disabilita EF logging; }); -// servizi Scoped (usano DB) - -//builder.Services.AddSingleton(); -builder.Services.AddScoped(); - -// Repository con interfaccia -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -// Servizi dati con interfaccia -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -// Da rivedere!!! -#if false -// registrazione automatica Repository e Servizi con Scrutor -builder.Services.Scan(scan => scan - .FromAssemblyOf() // Cerca nell'assembly dove si trova BaseServ - - // Registra tutti i Repository - .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Repository"))) - .AsImplementedInterfaces() // Es: associa GenClassRepository a IGenClassRepository - .WithScopedLifetime() - - // Registra tutti i Servizi - .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Service"))) - .AsImplementedInterfaces() - .WithScopedLifetime() -); -#endif +// registrazione in blocco servizi con metodo extension custom +builder.Services.AddLuxData(connectionString); // servizi Singleton (no DB) builder.Services.AddSingleton(); diff --git a/Lux.UI/Program.cs b/Lux.UI/Program.cs index a87b0ad..f355edf 100644 --- a/Lux.UI/Program.cs +++ b/Lux.UI/Program.cs @@ -1,19 +1,5 @@ using EgwCoreLib.Lux.Data; -using EgwCoreLib.Lux.Data.Repository.Config; -using EgwCoreLib.Lux.Data.Repository.Cost; -using EgwCoreLib.Lux.Data.Repository.Items; -using EgwCoreLib.Lux.Data.Repository.Job; -using EgwCoreLib.Lux.Data.Repository.Production; -using EgwCoreLib.Lux.Data.Repository.Sales; -using EgwCoreLib.Lux.Data.Repository.Utils; using EgwCoreLib.Lux.Data.Services; -using EgwCoreLib.Lux.Data.Services.Config; -using EgwCoreLib.Lux.Data.Services.Cost; -using EgwCoreLib.Lux.Data.Services.Items; -using EgwCoreLib.Lux.Data.Services.Job; -using EgwCoreLib.Lux.Data.Services.Production; -using EgwCoreLib.Lux.Data.Services.Sales; -using EgwCoreLib.Lux.Data.Services.Utils; using Lux.UI.Components; using Lux.UI.Components.Account; using Lux.UI.Data; @@ -154,27 +140,23 @@ builder.Services.AddAuthentication(options => }) .AddIdentityCookies(); -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); -#if false -builder.Services.AddDbContext(options => -options.UseSqlServer(connectionString)); -#endif +var conn = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); // ApplicationDbContext (già presente) builder.Services.AddDbContext(options => { options.UseMySql( - builder.Configuration.GetConnectionString("DefaultConnection"), - ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection"))) + conn, + ServerVersion.AutoDetect(conn)) .EnableSensitiveDataLogging(false) .EnableDetailedErrors(true); //.LogTo(_ => { }); // disabilita EF logging }); // DataLayerContext (manca!) +conn = builder.Configuration.GetConnectionString("Lux.All") ?? ""; builder.Services.AddDbContextFactory(options => { - var conn = builder.Configuration.GetConnectionString("Lux.All"); options.UseMySql(conn, ServerVersion.AutoDetect(conn), mySqlOptions => { mySqlOptions.EnableStringComparisonTranslations(); @@ -184,9 +166,6 @@ builder.Services.AddDbContextFactory(options => //.LogTo(_ => { }, Microsoft.Extensions.Logging.LogLevel.None); // disabilita EF logging }); - - - builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddIdentityCore(options => options.SignIn.RequireConfirmedAccount = true) @@ -209,84 +188,9 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -//builder.Services.AddSingleton(); -builder.Services.AddScoped(); -// Repository con interfaccia -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -// Servizi dati con interfaccia -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -// Da rivedere!!! -#if false -// registrazione automatica Service e Servizi con Scrutor -builder.Services.Scan(scan => scan - .FromAssemblyOf() // Cerca nell'assembly dove si trova BaseServ - - // Registra tutti i Repository - .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Repository"))) - .AsImplementedInterfaces() // Es: associa GenClassRepository a IGenClassRepository - .WithScopedLifetime() - - // Registra tutti i Servizi - .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Service"))) - .AsImplementedInterfaces() - .WithScopedLifetime() -); -#endif +// registrazione in blocco servizi con metodo extension custom +builder.Services.AddLuxData(conn); // init servizio gestone ReqIndex string cleanupDayTTL = configuration.GetValue("ServerConf:CleanupDayTTL") ?? "360";