Rimozione transazione in gestione EvList / DDB

This commit is contained in:
Samuele Locatelli
2026-04-30 15:54:05 +02:00
parent 492648a592
commit 1ecef8e09f
4 changed files with 57 additions and 11 deletions
+3 -3
View File
@@ -139,7 +139,7 @@ namespace MP.Data.Controllers
public async Task<bool> CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet)
{
await using var dbCtx = new MoonProContext(options);
await using var tx = await dbCtx.Database.BeginTransactionAsync();
//await using var tx = await dbCtx.Database.BeginTransactionAsync();
try
{
@@ -200,12 +200,12 @@ namespace MP.Data.Controllers
}
// Nessuna eccezione = successo
await tx.CommitAsync();
//await tx.CommitAsync();
return true;
}
catch (Exception ex)
{
await tx.RollbackAsync();
//await tx.RollbackAsync();
// Log dettagliato errore
Log.Error(ex, $"Errore in CheckCambiaStatoBatchAsync: {IdxMacchina} | {tipoInput}");
throw; // O return false; se il chiamante gestisce fallimenti
+11 -1
View File
@@ -7,6 +7,7 @@ namespace MP.Data.Services.IOC
{
public interface IIocService
{
#region Public Methods
/// <summary>
/// Processing intera catena eventi verifica cambio stato in singola transazione e con unico DbContext
@@ -29,5 +30,14 @@ namespace MP.Data.Services.IOC
/// <param name="newRecEv"></param>
/// <returns></returns>
Task<bool> EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv);
/// <summary>
/// Aggiornamento record Microstato macchina
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
Task<bool> MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec);
#endregion Public Methods
}
}
}
+31 -5
View File
@@ -10,9 +10,7 @@ namespace MP.Data.Services.IOC
{
public class IocService : BaseServ, IIocService
{
private readonly string _className;
private readonly IIocRepository _repo;
#region Public Constructors
public IocService(
IConfiguration config,
@@ -23,6 +21,10 @@ namespace MP.Data.Services.IOC
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet)
{
@@ -39,7 +41,6 @@ namespace MP.Data.Services.IOC
});
}
/// <inheritdoc />
public async Task<bool> EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv)
{
@@ -55,5 +56,30 @@ namespace MP.Data.Services.IOC
return success;
});
}
/// <inheritdoc />
public async Task<bool> MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec)
{
return await TraceAsync($"{_className}.MicroStatoMacchinaUpsert", async (activity) =>
{
// eseguo su repostory (DB)
string operation = "UPDATE";
bool success = await _repo.MicroStatoMacchinaUpsertAsync(newRec);
activity?.SetTag("db.operation", operation);
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IIocRepository _repo;
#endregion Private Fields
}
}
}
+12 -2
View File
@@ -1033,8 +1033,18 @@ namespace MP.IOC.Data
}
else
{
// solo microstato
await IocDbController.MicroStatoMacchinaUpsertAsync(newRecMsm);
if (useFactory)
{
await using var scope = _scopeFactory.CreateAsyncScope();
var iocService = scope.ServiceProvider.GetRequiredService<IIocService>();
// solo microstato
await iocService.MicroStatoMacchinaUpsertAsync(newRecMsm);
}
else
{
// solo microstato
await IocDbController.MicroStatoMacchinaUpsertAsync(newRecMsm);
}
}
return answ;
}