Skip to content

Commit

Permalink
Update repositories using transactions (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored Jan 29, 2024
1 parent a2c7c49 commit 1fc5d18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions TournamentManager/TournamentManager/Data/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public GenericRepository(MultiTenancy.IDbContext dbContext)

public virtual async Task<bool> SaveEntityAsync<T>(T entityToSave, bool refetchAfterSave, bool recurse, CancellationToken cancellationToken) where T : IEntity2
{
var transactionName = Guid.NewGuid().ToString("N");
var transactionName = string.Concat(nameof(GenericRepository), nameof(SaveEntityAsync), Guid.NewGuid().ToString("N"));
using var da = _dbContext.GetNewAdapter();
try
{
await da.StartTransactionAsync(IsolationLevel.ReadCommitted, transactionName, cancellationToken);
var success = await da.SaveEntityAsync(entityToSave, refetchAfterSave, recurse, cancellationToken);
await da.SaveEntityAsync(entityToSave, refetchAfterSave, recurse, cancellationToken);
await da.CommitAsync(cancellationToken);
return success;
return true;
}
catch (Exception e)
{
Expand Down
20 changes: 10 additions & 10 deletions TournamentManager/TournamentManager/Data/MatchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public virtual async Task<bool> SaveMatchResultAsync(MatchEntity matchEntity, Ca
try
{
await da.StartTransactionAsync(IsolationLevel.ReadCommitted,
string.Concat(nameof(MatchRepository), nameof(SaveMatchResultAsync), Guid.NewGuid().ToString()), cancellationToken);
string.Concat(nameof(MatchRepository), nameof(SaveMatchResultAsync), Guid.NewGuid().ToString("N")), cancellationToken);

if (matchEntity.Sets.RemovedEntitiesTracker != null)
{
Expand All @@ -348,9 +348,9 @@ await da.StartTransactionAsync(IsolationLevel.ReadCommitted,
matchEntity.Sets.RemovedEntitiesTracker = null;
}

var success = await da.SaveEntityAsync(matchEntity, false, true, cancellationToken);
await da.SaveEntityAsync(matchEntity, false, true, cancellationToken);
await da.CommitAsync(cancellationToken);
return success;
return true;
}
catch (Exception e)
{
Expand Down Expand Up @@ -378,20 +378,20 @@ public virtual async Task<bool> DeleteMatchResultAsync(long matchId, Cancellatio
?? throw new ArgumentException(@"No match found", nameof(matchId));

matchEntity.HomePoints = matchEntity.GuestPoints = null;
matchEntity.RealStart = matchEntity.RealEnd = null;
matchEntity.Remarks = string.Empty;
matchEntity.IsOverruled = false;
matchEntity.IsComplete = false;

// Track the removed sets
matchEntity.Sets.RemovedEntitiesTracker = new EntityCollection<SetEntity>();
matchEntity.Sets.Clear();

using var da = _dbContext.GetNewAdapter();

try
{
await da.StartTransactionAsync(IsolationLevel.ReadCommitted,
string.Concat(nameof(MatchRepository), nameof(DeleteMatchResultAsync), Guid.NewGuid().ToString()), cancellationToken);
string.Concat(nameof(MatchRepository), nameof(DeleteMatchResultAsync), Guid.NewGuid().ToString("N")), cancellationToken);

// Track the removed sets
matchEntity.Sets.RemovedEntitiesTracker = matchEntity.Sets;

// Delete the sets
if (matchEntity.Sets.RemovedEntitiesTracker != null)
Expand All @@ -402,9 +402,9 @@ await da.StartTransactionAsync(IsolationLevel.ReadCommitted,
}

// Save the changes to the match
var success = await da.SaveEntityAsync(matchEntity, false, true, cancellationToken);
await da.SaveEntityAsync(matchEntity, false, false, cancellationToken);
await da.CommitAsync(cancellationToken);
return success;
return true;
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public virtual async Task ReplaceAsync(RankingList rankingList, long roundId, Ca
var rankingColl = new EntityCollection<RankingEntity>(new RankingEntityFactory());
using var da = _dbContext.GetNewAdapter();

var transactionName = nameof(ReplaceAsync) + Guid.NewGuid().ToString("N");
var transactionName =
string.Concat(nameof(RankingRepository), nameof(ReplaceAsync), Guid.NewGuid().ToString("N"));

try
{
Expand Down
5 changes: 3 additions & 2 deletions TournamentManager/TournamentManager/TournamentCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SD.LLBLGen.Pro.ORMSupportClasses;
using TournamentManager.DAL.EntityClasses;
using TournamentManager.DAL.HelperClasses;
using TournamentManager.Data;
using TournamentManager.MultiTenancy;

namespace TournamentManager;
Expand Down Expand Up @@ -86,7 +87,7 @@ await _appDb.TournamentRepository.GetTournamentAsync(
/// <returns>True, if creation was successful, false otherwise.</returns>
public async Task<bool> CopyRound(long fromTournamentId, long toTournamentId, IList<long> excludeRoundId, CancellationToken cancellationToken)
{
const string transactionName = "CopyRounds";
var transactionName = string.Concat(nameof(TournamentCreator), nameof(CopyRound), Guid.NewGuid().ToString("N"));
var now = DateTime.UtcNow;

// get the rounds of SOURCE tournament
Expand Down Expand Up @@ -165,7 +166,7 @@ public async Task<bool> CopyRound(long fromTournamentId, long toTournamentId, IL

public async Task<bool> SetLegDates(IEnumerable<RoundEntity> rounds , int sequenceNo, DateTime start, DateTime end, CancellationToken cancellationToken)
{
const string transactionName = "SetLegDates";
var transactionName = string.Concat(nameof(RankingRepository), nameof(SetLegDates), Guid.NewGuid().ToString("N"));
var now = DateTime.UtcNow;

var roundEntities = (rounds as RoundEntity[] ?? rounds.ToArray()).ToList();
Expand Down

0 comments on commit 1fc5d18

Please sign in to comment.