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 ConfGlassRepository(DataLayerContext db) : base(db)
|
||||
public ConfGlassRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -15,38 +15,48 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Add(GlassModel entity) => _dbCtx.DbSetConfGlass.Add(entity);
|
||||
public async Task<bool> AddAsync(GlassModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
await dbCtx.DbSetConfGlass.AddAsync(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public void Delete(GlassModel entity) => _dbCtx.DbSetConfGlass.Remove(entity);
|
||||
public async Task<bool> DeleteAsync(GlassModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
dbCtx.DbSetConfGlass.Remove(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<List<GlassModel>> GetAllAsync()
|
||||
{
|
||||
return await _dbCtx.DbSetConfGlass
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfGlass.AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<GlassModel?> GetByIdAsync(int recId)
|
||||
{
|
||||
return await _dbCtx.DbSetConfGlass
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfGlass
|
||||
.Where(x => x.GlassID == recId)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public void Update(GlassModel entity)
|
||||
public async Task<bool> UpdateAsync(GlassModel entity)
|
||||
{
|
||||
// Recuperiamo l'entità tracciata dal context
|
||||
var trackedEntity = _dbCtx.DbSetConfGlass.Local.FirstOrDefault(x => x.GlassID == entity.GlassID);
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
var trackedEntity = dbCtx.DbSetConfGlass.Local.FirstOrDefault(x => x.GlassID == entity.GlassID);
|
||||
|
||||
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.DbSetConfGlass.Update(entity);
|
||||
dbCtx.DbSetConfGlass.Update(entity);
|
||||
}
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public ConfWoodRepository(DataLayerContext db) : base(db)
|
||||
public ConfWoodRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -15,38 +15,51 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Add(WoodModel entity) => _dbCtx.DbSetConfWood.Add(entity);
|
||||
public async Task<bool> AddAsync(WoodModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
await dbCtx.DbSetConfWood.AddAsync(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public void Delete(WoodModel entity) => _dbCtx.DbSetConfWood.Remove(entity);
|
||||
public async Task<bool> DeleteAsync(WoodModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
dbCtx.DbSetConfWood.Remove(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<List<WoodModel>> GetAllAsync()
|
||||
{
|
||||
return await _dbCtx.DbSetConfWood
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfWood
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<WoodModel?> GetByIdAsync(int recId)
|
||||
{
|
||||
return await _dbCtx.DbSetConfWood
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetConfWood
|
||||
.Where(x => x.WoodID == recId)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public void Update(WoodModel entity)
|
||||
public async Task<bool> UpdateAsync(WoodModel entity)
|
||||
{
|
||||
// Recuperiamo l'entità tracciata dal context
|
||||
var trackedEntity = _dbCtx.DbSetConfWood.Local.FirstOrDefault(x => x.WoodID == entity.WoodID);
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
var trackedEntity = dbCtx.DbSetConfWood.Local.FirstOrDefault(x => x.WoodID == entity.WoodID);
|
||||
|
||||
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.DbSetConfWood.Update(entity);
|
||||
dbCtx.DbSetConfWood.Update(entity);
|
||||
}
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public EnvirParamRepository(DataLayerContext db) : base(db)
|
||||
public EnvirParamRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
|
||||
public async Task<List<EnvirParamModel>> GetAllAsync()
|
||||
{
|
||||
return await _dbCtx.DbSetEnvirPar
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetEnvirPar
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
void Add(GlassModel entity);
|
||||
Task<bool> AddAsync(GlassModel entity);
|
||||
|
||||
void Delete(GlassModel entity);
|
||||
Task<bool> DeleteAsync(GlassModel entity);
|
||||
|
||||
Task<List<GlassModel>> GetAllAsync();
|
||||
|
||||
Task<GlassModel?> GetByIdAsync(int recId);
|
||||
|
||||
void Update(GlassModel entity);
|
||||
Task<bool> UpdateAsync(GlassModel entity);
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
public interface IConfProfileRepository : IBaseRepository
|
||||
{
|
||||
void Add(ProfileModel entity);
|
||||
Task<bool> AddAsync(ProfileModel entity);
|
||||
|
||||
|
||||
void Delete(ProfileModel entity);
|
||||
Task<bool> DeleteAsync(ProfileModel entity);
|
||||
|
||||
Task<List<ProfileModel>> GetAllAsync();
|
||||
|
||||
Task<ProfileModel?> GetByIdAsync(int recId);
|
||||
|
||||
void Update(ProfileModel entity);
|
||||
Task<bool> UpdateAsync(ProfileModel entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ namespace EgwCoreLib.Lux.Data.Repository.Config
|
||||
{
|
||||
public interface IConfWoodRepository : IBaseRepository
|
||||
{
|
||||
void Add(WoodModel entity);
|
||||
Task<bool> AddAsync(WoodModel entity);
|
||||
|
||||
|
||||
void Delete(WoodModel entity);
|
||||
Task<bool> DeleteAsync(WoodModel entity);
|
||||
|
||||
Task<List<WoodModel>> GetAllAsync();
|
||||
|
||||
Task<WoodModel?> GetByIdAsync(int recId);
|
||||
|
||||
void Update(WoodModel entity);
|
||||
Task<bool> UpdateAsync(WoodModel entity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user