Skip to content

Commit 5233eec

Browse files
Refactor Cosmos DB context configuration and add proxy
Refactored the `ApplicationServices` class to simplify Cosmos DB context configuration by replacing `AddDbContext` with `AddCosmosDbContext` and introducing a scoped service registration for `IGamesRepository` using `DataContextProxy<GamesCosmosContext>`. Removed `EnrichCosmosDbContext` and commented out old configuration code for reference. Added a new `DataContextProxy<TContext>` class to act as a proxy for `IGamesRepository`, delegating repository operations to the underlying `DbContext`. This improves flexibility and decouples the repository interface from specific database context implementations.
1 parent 7483695 commit 5233eec

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

src/services/gameapis/Codebreaker.GameAPIs/ApplicationServices.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ static void ConfigureSqlServer(IHostApplicationBuilder builder)
4343

4444
static void ConfigureCosmos(IHostApplicationBuilder builder)
4545
{
46-
builder.Services.AddDbContext<IGamesRepository, GamesCosmosContext>(options =>
47-
{
48-
string connectionString = builder.Configuration.GetConnectionString("codebreaker") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
49-
options.UseCosmos(connectionString, "codebreaker");
46+
builder.AddCosmosDbContext<GamesCosmosContext>("codebreaker", "codebreaker");
5047

51-
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
52-
});
53-
builder.EnrichCosmosDbContext<GamesCosmosContext>(settings =>
54-
{
55-
});
48+
builder.Services.AddScoped<IGamesRepository, DataContextProxy<GamesCosmosContext>>();
49+
50+
51+
//builder.Services.AddDbContext<IGamesRepository, GamesCosmosContext>(options =>
52+
//{
53+
// //string connectionString = builder.Configuration.GetConnectionString("codebreaker") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
54+
// //options.UseCosmos(connectionString, "codebreaker");
55+
56+
// options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
57+
//});
58+
//builder.AddCosmosDbContext<GamesCosmosContext>("codebreaker");
59+
////builder.EnrichCosmosDbContext<GamesCosmosContext>(settings =>
60+
////{
61+
////});
5662
}
5763

5864
static void ConfigureInMemory(IHostApplicationBuilder builder)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Codebreaker.GameAPIs.Data;
2+
3+
public class DataContextProxy<TContext>(TContext context) : IGamesRepository
4+
where TContext : DbContext, IGamesRepository
5+
{
6+
public Task AddGameAsync(Game game, CancellationToken cancellationToken = default) => context.AddGameAsync(game, cancellationToken);
7+
public Task AddMoveAsync(Game game, Move move, CancellationToken cancellationToken = default) => context.AddMoveAsync(game, move, cancellationToken);
8+
public Task<bool> DeleteGameAsync(Guid id, CancellationToken cancellationToken = default) => context.DeleteGameAsync(id, cancellationToken);
9+
public Task<Game?> GetGameAsync(Guid id, CancellationToken cancellationToken = default) => context.GetGameAsync(id, cancellationToken);
10+
public Task<IEnumerable<Game>> GetGamesAsync(GamesQuery gamesQuery, CancellationToken cancellationToken = default) => context.GetGamesAsync(gamesQuery, cancellationToken);
11+
public Task<Game> UpdateGameAsync(Game game, CancellationToken cancellationToken = default) => context.UpdateGameAsync(game, cancellationToken);
12+
13+
//public async Task UpdateDatabaseAsync(ILogger logger)
14+
//{
15+
// if (context is GamesSqlServerContext gamesSqlContext)
16+
// {
17+
// await gamesSqlContext.Database.MigrateAsync();
18+
// logger.LogInformation("Sql Server database migration applied");
19+
// }
20+
//}
21+
}

0 commit comments

Comments
 (0)