Review schema gestione repository x evitare concorrezza contesto DbContext
This commit is contained in:
@@ -7,7 +7,7 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public ConfProfileRepository(DataLayerContext db) : base(db)
|
||||
public ConfProfileRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -15,38 +15,49 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Add(ProfileModel entity) => _dbCtx.DbSetConfProfile.Add(entity);
|
||||
|
||||
public void Delete(ProfileModel entity) => _dbCtx.DbSetConfProfile.Remove(entity);
|
||||
public async Task<bool> AddAsync(ProfileModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
await dbCtx.DbSetConfProfile.AddAsync(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(ProfileModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
dbCtx.DbSetConfProfile.Remove(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
public async Task<List<ProfileModel>> GetAllAsync()
|
||||
{
|
||||
return await _dbCtx.DbSetConfProfile
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfProfile.AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ProfileModel?> GetByIdAsync(int recId)
|
||||
{
|
||||
return await _dbCtx.DbSetConfProfile
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfProfile
|
||||
.Where(x => x.ProfileID == recId)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public void Update(ProfileModel entity)
|
||||
|
||||
public async Task<bool> UpdateAsync(ProfileModel entity)
|
||||
{
|
||||
// Recuperiamo l'entità tracciata dal context
|
||||
var trackedEntity = _dbCtx.DbSetConfProfile.Local.FirstOrDefault(x => x.ProfileID == entity.ProfileID);
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
var trackedEntity = dbCtx.DbSetConfProfile.Local.FirstOrDefault(x => x.ProfileID == entity.ProfileID);
|
||||
|
||||
if (trackedEntity != null)
|
||||
{
|
||||
// Aggiorna i valori dell'entità tracciata con quelli della nuova
|
||||
_dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
||||
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dbCtx.DbSetConfProfile.Update(entity);
|
||||
dbCtx.DbSetConfProfile.Update(entity);
|
||||
}
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
Reference in New Issue
Block a user