refactor(ServicesCollectionExtensions): replace hardcoded string with constant for Testing environment
This commit is contained in:
@@ -22,6 +22,7 @@ internal static class ServicesCollectionExtensions
|
|||||||
private const int DbCommandTimeout = 30;
|
private const int DbCommandTimeout = 30;
|
||||||
private const string DbMigrationAssemblyName = "Shop.PublicApi";
|
private const string DbMigrationAssemblyName = "Shop.PublicApi";
|
||||||
private const string RedisInstanceName = "master";
|
private const string RedisInstanceName = "master";
|
||||||
|
private const string TestingEnvironmentName = "Testing";
|
||||||
|
|
||||||
private static readonly string[] DbRelationalTags = ["database", "ef-core", "sql-server", "relational"];
|
private static readonly string[] DbRelationalTags = ["database", "ef-core", "sql-server", "relational"];
|
||||||
private static readonly string[] DbNoSqlTags = ["database", "mongodb", "no-sql"];
|
private static readonly string[] DbNoSqlTags = ["database", "mongodb", "no-sql"];
|
||||||
@@ -44,7 +45,7 @@ internal static class ServicesCollectionExtensions
|
|||||||
|
|
||||||
public static IServiceCollection AddWriteDbContext(this IServiceCollection services, IWebHostEnvironment environment)
|
public static IServiceCollection AddWriteDbContext(this IServiceCollection services, IWebHostEnvironment environment)
|
||||||
{
|
{
|
||||||
if (!environment.IsEnvironment("Testing"))
|
if (!environment.IsEnvironment(TestingEnvironmentName))
|
||||||
{
|
{
|
||||||
services.AddDbContextPool<WriteDbContext>((serviceProvider, optionsBuilder) =>
|
services.AddDbContextPool<WriteDbContext>((serviceProvider, optionsBuilder) =>
|
||||||
ConfigureDbContext<WriteDbContext>(serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll));
|
ConfigureDbContext<WriteDbContext>(serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll));
|
||||||
@@ -77,12 +78,12 @@ internal static class ServicesCollectionExtensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConfigureDbContext<TContext>(
|
private static void ConfigureDbContext<TDbContext>(
|
||||||
IServiceProvider serviceProvider,
|
IServiceProvider serviceProvider,
|
||||||
DbContextOptionsBuilder optionsBuilder,
|
DbContextOptionsBuilder optionsBuilder,
|
||||||
QueryTrackingBehavior queryTrackingBehavior) where TContext : DbContext
|
QueryTrackingBehavior queryTrackingBehavior) where TDbContext : DbContext
|
||||||
{
|
{
|
||||||
var logger = serviceProvider.GetRequiredService<ILogger<TContext>>();
|
var logger = serviceProvider.GetRequiredService<ILogger<TDbContext>>();
|
||||||
var options = serviceProvider.GetOptions<ConnectionOptions>();
|
var options = serviceProvider.GetOptions<ConnectionOptions>();
|
||||||
var environment = serviceProvider.GetRequiredService<IHostEnvironment>();
|
var environment = serviceProvider.GetRequiredService<IHostEnvironment>();
|
||||||
var envIsDevelopment = environment.IsDevelopment();
|
var envIsDevelopment = environment.IsDevelopment();
|
||||||
@@ -111,5 +112,8 @@ internal static class ServicesCollectionExtensions
|
|||||||
retryEventData.Delay,
|
retryEventData.Delay,
|
||||||
exceptions[^1].Message);
|
exceptions[^1].Message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (envIsDevelopment)
|
||||||
|
optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user