Migrazione gestione risorse in obj separati (Repository/Service)

This commit is contained in:
Samuele Locatelli
2026-03-17 15:23:05 +01:00
parent 3ec1511aec
commit 870cf85e3f
15 changed files with 231 additions and 180 deletions
@@ -2674,105 +2674,6 @@ namespace EgwCoreLib.Lux.Data.Controllers
return dbRestults;
}
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="rec2del"></param>
/// <returns></returns>
internal async Task<bool> ResourcesDeleteAsync(ResourceModel rec2del)
{
bool answ = false;
//using (DataLayerContext dbCtx = new DataLayerContext(_config))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
var currRec = dbCtx
.DbSetResource
.Where(x => rec2del.ResourceID > 0 && x.ResourceID == rec2del.ResourceID)
.FirstOrDefault();
// se trovato --> elimino
if (currRec != null)
{
dbCtx.DbSetResource.Remove(rec2del);
}
// salvo...
int numAct = await dbCtx.SaveChangesAsync();
answ = numAct > 0;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ResourcesDeleteAsync{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Elenco record risorse da DB
/// </summary>
/// <returns></returns>
internal async Task<List<ResourceModel>> ResourcesGetAllAsync()
{
List<ResourceModel> dbResult = new List<ResourceModel>();
//using (DataLayerContext dbCtx = new DataLayerContext(_config))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
dbResult = await dbCtx
.DbSetResource
.Include(d => d.DriverNav)
.Include(j => j.JobStepNav)
.ToListAsync();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ResourcesGetAllAsync{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Add record
/// </summary>
/// <param name="upsRec"></param>
/// <returns></returns>
internal async Task<bool> ResourcesUpsertAsync(ResourceModel upsRec)
{
bool answ = false;
//using (DataLayerContext dbCtx = new DataLayerContext(_config))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
var currRec = dbCtx
.DbSetResource
.Where(x => upsRec.ResourceID > 0 && x.ResourceID == upsRec.ResourceID)
.FirstOrDefault();
// se trovato --> aggiorno
if (currRec != null)
{
dbCtx.Entry(currRec).CurrentValues.SetValues(upsRec);
}
// se mancasse --> aggiungo
else
{
dbCtx.DbSetResource.Add(upsRec);
}
// salvo...
int numAct = await dbCtx.SaveChangesAsync();
answ = numAct > 0;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ResourcesUpsertAsync{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Esegue merge dei dati nella tab profili del DB con le info accessorie...
/// </summary>
@@ -0,0 +1,21 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
namespace EgwCoreLib.Lux.Data.Repository.Cost
{
public interface IResourceRepository : IBaseRepository
{
#region Public Methods
Task<bool> AddAsync(ResourceModel entity);
Task<bool> DeleteAsync(ResourceModel entity);
Task<List<ResourceModel>> GetAllAsync();
Task<ResourceModel?> GetByIdAsync(int recId);
Task<bool> UpdateAsync(ResourceModel entity);
#endregion Public Methods
}
}
@@ -0,0 +1,68 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
using Microsoft.EntityFrameworkCore;
namespace EgwCoreLib.Lux.Data.Repository.Cost
{
public class ResourceRepository : BaseRepository, IResourceRepository
{
#region Public Constructors
public ResourceRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Public Methods
public async Task<bool> AddAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
await dbCtx.DbSetResource.AddAsync(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
dbCtx.DbSetResource.Remove(entity);
return await dbCtx.SaveChangesAsync() > 0;
}
public async Task<List<ResourceModel>> GetAllAsync()
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetResource
.Include(d => d.DriverNav)
.Include(j => j.JobStepNav)
.AsNoTracking()
.ToListAsync();
}
public async Task<ResourceModel?> GetByIdAsync(int recId)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == recId);
}
public async Task<bool> UpdateAsync(ResourceModel entity)
{
await using var dbCtx = await CreateContextAsync();
// Recuperiamo l'entità tracciata dal context
var trackedEntity = dbCtx.DbSetResource.Local.FirstOrDefault(x => x.ResourceID == entity.ResourceID);
if (trackedEntity != null)
{
// Aggiorna i valori dell'entità tracciata con quelli della nuova
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
}
else
{
dbCtx.DbSetResource.Update(entity);
}
return await dbCtx.SaveChangesAsync() > 0;
}
#endregion Public Methods
}
}
@@ -0,0 +1,17 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
namespace EgwCoreLib.Lux.Data.Services.Cost
{
public interface IResourceService
{
#region Public Methods
Task<bool> DeleteAsync(ResourceModel entity);
Task<List<ResourceModel>> GetAllAsync();
Task<bool> UpsertAsync(ResourceModel upsRec);
#endregion Public Methods
}
}
@@ -0,0 +1,94 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
using EgwCoreLib.Lux.Data.Repository.Cost;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
namespace EgwCoreLib.Lux.Data.Services.Cost
{
public class ResourceService : BaseServ, IResourceService
{
#region Public Constructors
public ResourceService(
IConfiguration config,
IConnectionMultiplexer redis,
IResourceRepository repo) : base(config, redis)
{
_className = "Resource";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
public async Task<bool> DeleteAsync(ResourceModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByIdAsync(rec2del.ResourceID);
if (dbResult == null) return false;
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
public async Task<List<ResourceModel>> GetAllAsync()
{
return await TraceAsync($"{_className}.GetAll", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ALL",
async () => await _repo.GetAllAsync(),
UltraLongCache
);
});
}
public async Task<bool> UpsertAsync(ResourceModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(upsRec.ResourceID);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
success = await _repo.UpdateAsync(upsRec);
}
else
{
operation = "INSERT";
success = await _repo.AddAsync(upsRec);
}
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IResourceRepository _repo;
#endregion Private Fields
}
}
@@ -1615,70 +1615,6 @@ namespace EgwCoreLib.Lux.Data.Services
return dbResult;
}
/// Esegue Delete del record ricevuto
/// </summary>
/// <param name="upsRec"></param>
/// <returns></returns>
public async Task<bool> ResourcesDeleteAsync(ResourceModel upsRec)
{
using var activity = StartActivity();
string source = "DB+REDIS";
bool result = await dbController.ResourcesDeleteAsync(upsRec);
await ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:Resources:*");
activity?.SetTag("data.source", source);
LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Elenco completo Risorse
/// </summary>
/// <returns></returns>
public async Task<List<ResourceModel>> ResourcesGetAllAsync()
{
using var activity = StartActivity();
string source = "DB";
List<ResourceModel>? result = new List<ResourceModel>();
// cerco in redis...
string currKey = $"{redisBaseKey}:Resources:ALL";
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
if (rawData.HasValue)
{
result = JsonConvert.DeserializeObject<List<ResourceModel>>($"{rawData}");
source = "REDIS";
}
else
{
result = await dbController.ResourcesGetAllAsync();
// serializzo e salvo con config x evitare loop...
rawData = JsonConvert.SerializeObject(result, JSSettings);
await _redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (result == null)
{
result = new List<ResourceModel>();
}
activity?.SetTag("data.source", source);
LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Esegue Add del record ricevuto
/// </summary>
/// <param name="upsRec"></param>
/// <returns></returns>
public async Task<bool> ResourcesUpsertAsync(ResourceModel upsRec)
{
using var activity = StartActivity();
string source = "DB+REDIS";
bool result = await dbController.ResourcesUpsertAsync(upsRec);
await ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:Resources:*");
activity?.SetTag("data.source", source);
LogTrace($"{source} | trace: {activity?.TraceId} | {activity?.Duration.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Esegue salvataggio BOM sul DB
/// </summary>
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.1.2603.1714</Version>
<Version>1.1.2603.1715</Version>
</PropertyGroup>
<ItemGroup>
+4
View File
@@ -1,10 +1,12 @@
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.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.Utils;
@@ -178,6 +180,7 @@ builder.Services.AddScoped<IGenValRepository, GenValRepository>();
builder.Services.AddScoped<IItemRepository, ItemRepository>();
builder.Services.AddScoped<IJobStepRepository, JobStepRepository>();
builder.Services.AddScoped<IJobTaskRepository, JobTaskRepository>();
builder.Services.AddScoped<IResourceRepository, ResourceRepository>();
builder.Services.AddScoped<ISellingItemRepository, SellingItemRepository>();
builder.Services.AddScoped<ITemplateRepository, TemplateRepository>();
builder.Services.AddScoped<ITemplateRowRepository, TemplateRowRepository>();
@@ -192,6 +195,7 @@ builder.Services.AddScoped<IGenValService, GenValService>();
builder.Services.AddScoped<IItemService, ItemService>();
builder.Services.AddScoped<IJobStepService, JobStepService>();
builder.Services.AddScoped<IJobTaskService, JobTaskService>();
builder.Services.AddScoped<IResourceService, ResourceService>();
builder.Services.AddScoped<ISellingItemService, SellingItemService>();
builder.Services.AddScoped<ITemplateService, TemplateService>();
builder.Services.AddScoped<ITemplateRowService, TemplateRowService>();
@@ -1,8 +1,7 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
using EgwCoreLib.Lux.Data.DbModel.Job;
using EgwCoreLib.Lux.Data.Services;
using EgwCoreLib.Lux.Data.Services.Cost;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.JSInterop;
namespace Lux.UI.Components.Compo.JobTask
@@ -30,6 +29,9 @@ namespace Lux.UI.Components.Compo.JobTask
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
[Inject]
protected IResourceService ResService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
@@ -63,7 +65,7 @@ namespace Lux.UI.Components.Compo.JobTask
isLoading = true;
// elimino e ricarico...
await DLService.ResourcesDeleteAsync(rec2del);
await ResService.DeleteAsync(rec2del);
await ReloadBaseData();
ReloadData();
}
@@ -92,7 +94,7 @@ namespace Lux.UI.Components.Compo.JobTask
CostDriverID = 1
};
await DLService.ResourcesUpsertAsync(newRecord);
await ResService.UpsertAsync(newRecord);
AllRecords = new List<ResourceModel>();
await Task.Delay(100);
await ReloadBaseData();
@@ -156,7 +158,7 @@ namespace Lux.UI.Components.Compo.JobTask
// se non nullo salvo!
if (updRec != null)
{
await DLService.ResourcesUpsertAsync(updRec);
await ResService.UpsertAsync(updRec);
}
await ReloadBaseData();
ReloadData();
@@ -165,7 +167,7 @@ namespace Lux.UI.Components.Compo.JobTask
private async Task ReloadBaseData()
{
AllRecords = await DLService.ResourcesGetAllAsync();
AllRecords = await ResService.GetAllAsync();
ListCostDriver = await DLService.CostDriverGetAllAsync();
}
+10 -6
View File
@@ -1,6 +1,7 @@
using EgwCoreLib.Lux.Data.DbModel.Cost;
using EgwCoreLib.Lux.Data.DbModel.Job;
using EgwCoreLib.Lux.Data.Services;
using EgwCoreLib.Lux.Data.Services.Cost;
using EgwCoreLib.Lux.Data.Services.Job;
using Microsoft.AspNetCore.Components;
@@ -14,10 +15,13 @@ namespace Lux.UI.Components.Pages
protected DataLayerServices DLService { get; set; } = null!;
[Inject]
protected IJobStepService JSService { get; set; } = null!;
protected IJobStepService JStService { get; set; } = null!;
[Inject]
protected IJobTaskService JTService { get; set; } = null!;
protected IJobTaskService JTaService { get; set; } = null!;
[Inject]
protected IResourceService ResService { get; set; } = null!;
#endregion Protected Properties
@@ -74,8 +78,8 @@ namespace Lux.UI.Components.Pages
ListTagsAvailable = rawTags.Select(x => x.CodTag).ToList();
ListCostDrivers = await DLService.CostDriverGetAllAsync();
ListPhases = await DLService.PhasesGetAllAsync();
ListResources = await DLService.ResourcesGetAllAsync();
ListJobTask = await JTService.GetAllAsync();
ListResources = await ResService.GetAllAsync();
ListJobTask = await JTaService.GetAllAsync();
ListStep = null;
isLoading = false;
}
@@ -89,7 +93,7 @@ namespace Lux.UI.Components.Pages
{
if (selRecord != null)
{
ListStep = await JSService.GetByParentAsync(selRecord.JobID);
ListStep = await JStService.GetByParentAsync(selRecord.JobID);
}
else
{
@@ -107,7 +111,7 @@ namespace Lux.UI.Components.Pages
// ricarico dal DB
if (selRec != null)
{
ListStep = await JSService.GetByParentAsync(selRec.JobID);
ListStep = await JStService.GetByParentAsync(selRec.JobID);
}
else
{
+1 -1
View File
@@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-Lux.UI-a758c101-a2f4-4e38-977d-1c4887dbbd50</UserSecretsId>
<Version>1.1.2603.1714</Version>
<Version>1.1.2603.1715</Version>
</PropertyGroup>
<ItemGroup>
+4
View File
@@ -1,10 +1,12 @@
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.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.Utils;
@@ -216,6 +218,7 @@ builder.Services.AddScoped<IGenValRepository, GenValRepository>();
builder.Services.AddScoped<IItemRepository, ItemRepository>();
builder.Services.AddScoped<IJobStepRepository, JobStepRepository>();
builder.Services.AddScoped<IJobTaskRepository, JobTaskRepository>();
builder.Services.AddScoped<IResourceRepository, ResourceRepository>();
builder.Services.AddScoped<ISellingItemRepository, SellingItemRepository>();
builder.Services.AddScoped<ITemplateRepository, TemplateRepository>();
builder.Services.AddScoped<ITemplateRowRepository, TemplateRowRepository>();
@@ -230,6 +233,7 @@ builder.Services.AddScoped<IGenValService, GenValService>();
builder.Services.AddScoped<IItemService, ItemService>();
builder.Services.AddScoped<IJobStepService, JobStepService>();
builder.Services.AddScoped<IJobTaskService, JobTaskService>();
builder.Services.AddScoped<IResourceService, ResourceService>();
builder.Services.AddScoped<ISellingItemService, SellingItemService>();
builder.Services.AddScoped<ITemplateService, TemplateService>();
builder.Services.AddScoped<ITemplateRowService, TemplateRowService>();
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>LUX - Web Windows MES</i>
<h4>Versione: 1.1.2603.1714</h4>
<h4>Versione: 1.1.2603.1715</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.1.2603.1714
1.1.2603.1715
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.1.2603.1714</version>
<version>1.1.2603.1715</version>
<url>http://nexus.steamware.net/repository/SWS/GPW/stable/GPW.UI.zip</url>
<changelog>http://nexus.steamware.net/repository/SWS/GPW/stable/ChangeLog.html</changelog>
<mandatory>false</mandatory>