Skip to content

Commit

Permalink
Made report-repositories disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
szv committed Jun 28, 2023
1 parent b1290e4 commit c943df2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/Services/Report/src/Data/Repositories/GameRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace CodeBreaker.Services.Report.Data.Repositories;

public class GameRepository(ReportDbContext dbContext) : IGameRepository
public class GameRepository(ReportDbContext dbContext) : IGameRepository, IDisposable
{
public IAsyncEnumerable<Game> GetAsync(GetGamesArgs args, CancellationToken cancellationToken = default) =>
dbContext.Games
Expand Down Expand Up @@ -92,4 +92,9 @@ private static void SyncPositions(IEnumerable<KeyPeg> keyPegs)
foreach (var keyPeg in keyPegs)
keyPeg.Position = i++;
}

public void Dispose()
{
dbContext.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using CodeBreaker.Services.Report.Data.Repositories.Args;

namespace CodeBreaker.Services.Report.Data.Repositories;
public interface IGameRepository
public interface IGameRepository : IDisposable
{
Task<Game> CreateAsync(Game game, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid gameId, CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CodeBreaker.Services.Report.Data.Models;

namespace CodeBreaker.Services.Report.Data.Repositories;
public interface IStatisticsRepository
public interface IStatisticsRepository : IDisposable
{
Task<Statistics> GetStatisticsAsync(GetStatisticsArgs args, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CodeBreaker.Services.Report.Data.Repositories;

public class StatisticsRepository(ReportDbContext dbContext) : IStatisticsRepository
public class StatisticsRepository(ReportDbContext dbContext) : IStatisticsRepository, IDisposable
{
private class StatisticsResult
{
Expand Down Expand Up @@ -37,6 +37,11 @@ public async Task<Statistics> GetStatisticsAsync(GetStatisticsArgs args, Cancell
})
.SingleAsync(cancellationToken);
}

public void Dispose()
{
dbContext.Dispose();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
private async Task OnGameCreatedCallbackAsync(GameCreatedPayload payload, CancellationToken cancellationToken)
{
_logger.LogInformation("OnGameCreated");
var repository = _services.GetRequiredService<IGameRepository>();
using var repository = _services.GetRequiredService<IGameRepository>();
await repository.CreateAsync(payload.ToModel(), cancellationToken);
}

private async Task OnMoveCreatedCallbackAsync(MoveCreatedPayload payload, CancellationToken cancellationToken)
{
_logger.LogInformation("OnMoveCreated");
var repository = _services.GetRequiredService<IGameRepository>();
using var repository = _services.GetRequiredService<IGameRepository>();
await repository.AddMoveAsync(payload.GameId, payload.ToMoveModel(), cancellationToken);
}

private async Task OnGameEndedCallbackAsync(GameEndedPayload payload, CancellationToken cancellationToken)
{
_logger.LogInformation("OnGameEnded");
var repository = _services.GetRequiredService<IGameRepository>();
using var repository = _services.GetRequiredService<IGameRepository>();
var game = await repository.GetAsync(payload.Id, cancellationToken);
payload.ToModel(game);
await repository.UpdateAsync(payload.Id, game, cancellationToken);
Expand Down

0 comments on commit c943df2

Please sign in to comment.