Skip to content

Commit

Permalink
feat(ApiDbContext): update DataTypeMapper configuration
Browse files Browse the repository at this point in the history
- Change ApiDbContext class to be sealed
- Remove static constructor in ApiDbContext class
- Move DataTypeMapper configuration to a new extension method ConfigureApiDbDataSourceBuilder in ApiDbContextExtensions class
- Update Startup.cs to use the new ConfigureApiDbDataSourceBuilder extension method for configuring NpgsqlDataSource
  • Loading branch information
SakuraIsayeki committed Jan 11, 2024
1 parent 5942226 commit b9eddf4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 19 additions & 8 deletions WowsKarma.Api/Data/ApiDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace WowsKarma.Api.Data;


public class ApiDbContext : DbContext
public sealed class ApiDbContext : DbContext
{
public DbSet<Clan> Clans { get; init; }
public DbSet<ClanMember> ClanMembers { get; init; }
Expand All @@ -30,16 +30,11 @@ public class ApiDbContext : DbContext
public DbSet<PostModDeletedNotification> PostModDeletedNotifications { get; init; }
#endregion


static ApiDbContext()
public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options)
{
NpgsqlConnection.GlobalTypeMapper.MapEnum<ModActionType>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<NotificationType>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<ClanRole>();

}

public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (Type type in modelBuilder.Model.GetEntityTypes().Where(t => t.ClrType.ImplementsInterface(typeof(ITimestamped))).Select(t => t.ClrType))
Expand Down Expand Up @@ -132,3 +127,19 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
#endregion
}
}


public static class ApiDbContextExtensions
{
public static NpgsqlDataSourceBuilder ConfigureApiDbDataSourceBuilder(this NpgsqlDataSourceBuilder dataSourceBuilder)
{
dataSourceBuilder
.MapEnum<ModActionType>()
.MapEnum<NotificationType>()
.MapEnum<ClanRole>();

dataSourceBuilder.EnableDynamicJson();

return dataSourceBuilder;
}
}
7 changes: 6 additions & 1 deletion WowsKarma.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Nodsoft.Wargaming.Api.Client.Clients.Wows;
using Nodsoft.Wargaming.Api.Common;
using Nodsoft.WowsReplaysUnpack.ExtendedData;
using Npgsql;
using WowsKarma.Api.Data;
using WowsKarma.Api.Hubs;
using WowsKarma.Api.Infrastructure.Authorization;
Expand Down Expand Up @@ -186,8 +187,12 @@ public void ConfigureServices(IServiceCollection services)
string dbConnectionString = $"ApiDbConnectionString:{ApiRegion.ToRegionString()}";
int dbPoolSize = Configuration.GetValue<int>("Database:PoolSize");

NpgsqlDataSource apiDbDataSourceBuilder = new NpgsqlDataSourceBuilder(Configuration.GetConnectionString(dbConnectionString))
.ConfigureApiDbDataSourceBuilder()
.Build();

services.AddDbContextPool<ApiDbContext>(
o => o.UseNpgsql(Configuration.GetConnectionString(dbConnectionString),
o => o.UseNpgsql(apiDbDataSourceBuilder,
p =>
{
p.EnableRetryOnFailure();
Expand Down

0 comments on commit b9eddf4

Please sign in to comment.