Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions EstateReportingAPI.BusinessLogic/QueryTimingInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Shared.EntityFramework;
using Shared.Logger;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Text;
using Shared.General;

namespace EstateReportingAPI.BusinessLogic;

public class QueryTimingInterceptor : DbCommandInterceptor {

internal void LogIfRequired(DbCommand command,
CommandExecutedEventData eventData) {

Int32 threshold = ConfigurationReader.GetValueOrDefault<int>("AppSettings", "EFQueryPerformanceThresholdMs", 500);

if (eventData.Duration.TotalMilliseconds < threshold)
return;
Logger.LogWarning($"PERFORMANCE - EF Query took {eventData.Duration.TotalMilliseconds} ms\n{command.CommandText}\n");
}

public override DbDataReader ReaderExecuted(DbCommand command,
CommandExecutedEventData eventData,
DbDataReader result) {
LogIfRequired(command, eventData);
return result;
}

public override async ValueTask<DbDataReader> ReaderExecutedAsync(DbCommand command,
CommandExecutedEventData eventData,
DbDataReader result,
CancellationToken cancellationToken = new CancellationToken()) {
LogIfRequired(command, eventData);

return result;
}
}


public class DbContextResolverX<TContext> : IDbContextResolver<TContext> where TContext : DbContext
{
private readonly IServiceProvider _rootProvider;
private readonly IConfiguration _config;
private readonly DbCommandInterceptor Interceptor;

public DbContextResolverX(IServiceProvider rootProvider,
IConfiguration config,
DbCommandInterceptor interceptor)
{
_rootProvider = rootProvider;
_config = config;
this.Interceptor = interceptor;
}

public ResolvedDbContext<TContext> Resolve(String connectionStringKey)
{
return this.Resolve(connectionStringKey, String.Empty);
}

public ResolvedDbContext<TContext> Resolve(String connectionStringKey,
String databaseNameSuffix)
{
IServiceScope scope = _rootProvider.CreateScope();
String connectionString = _config.GetConnectionString(connectionStringKey);
if (String.IsNullOrWhiteSpace(connectionString))
throw new InvalidOperationException($"Connection string for '{connectionStringKey}' not found.");

// Update the connection string with the identifier if needed
if (!String.IsNullOrWhiteSpace(databaseNameSuffix))
{
SqlConnectionStringBuilder builder = new(connectionString);
builder.InitialCatalog = $"{builder.InitialCatalog}-{databaseNameSuffix}";
connectionString = builder.ConnectionString;


// Create an isolated service collection and provider
ServiceCollection services = new();
services.AddDbContext<TContext>(options => {
options.UseSqlServer(connectionString);
options.AddInterceptors(Interceptor); // attach here
});

ServiceProvider provider = services.BuildServiceProvider();
scope = provider.CreateScope();
// Standard resolution using DI container
}

return new ResolvedDbContext<TContext>(scope);
}
}
19 changes: 12 additions & 7 deletions EstateReportingAPI/Bootstrapper/RepositoryRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using TransactionProcessor.Database.Contexts;
using Microsoft.EntityFrameworkCore.Diagnostics;
using TransactionProcessor.Database.Contexts;

namespace EstateReportingAPI.Bootstrapper;

using BusinessLogic;
using Common;
using Lamar;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Shared.EntityFramework;
using Shared.General;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -18,16 +20,19 @@ public RepositoryRegistry(){
{
this.AddSingleton<IReportingManager, ReportingManager>();
}

this.AddSingleton(typeof(IDbContextResolver<>), typeof(DbContextResolver<>));
this.AddSingleton<DbCommandInterceptor, QueryTimingInterceptor>();
this.AddSingleton(typeof(IDbContextResolver<>), typeof(DbContextResolverX<>));
if (Startup.WebHostEnvironment.IsEnvironment("IntegrationTest") || Startup.Configuration.GetValue<Boolean>("ServiceOptions:UseInMemoryDatabase") == true)
{
this.AddDbContext<EstateManagementContext>(builder => builder.UseInMemoryDatabase("TransactionProcessorReadModel"));
}
else
{
this.AddDbContext<EstateManagementContext>(options =>
options.UseSqlServer(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel")));
else {
this.AddSingleton<QueryTimingInterceptor>();
this.AddDbContext<EstateManagementContext>((sp, options) =>
{
options.UseSqlServer(ConfigurationReader.GetConnectionString("TransactionProcessorReadModel"));
options.AddInterceptors(sp.GetRequiredService<QueryTimingInterceptor>());
});
}
}
}
Loading