Skip to content

Commit 03bbe57

Browse files
committed
feat: add db context factory
1 parent ed12902 commit 03bbe57

11 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/Audit/AuditModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avolutions.Baf.Core.Audit.Services;
44
using Avolutions.Baf.Core.Module.Abstractions;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
67

78
namespace Avolutions.Baf.Core.Audit;
89

@@ -11,6 +12,6 @@ public class AuditModule : IFeatureModule
1112
public void Register(IServiceCollection services)
1213
{
1314
services.AddScoped(typeof(IAuditLogService<>), typeof(AuditLogService<>));
14-
services.AddScoped<AuditSaveChangesInterceptor>();
15+
services.TryAddSingleton<AuditSaveChangesInterceptor>();
1516
}
1617
}

src/Entity/EntityModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avolutions.Baf.Core.Entity.Services;
44
using Avolutions.Baf.Core.Module.Abstractions;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
67

78
namespace Avolutions.Baf.Core.Entity;
89

@@ -11,7 +12,7 @@ public class EntityModule : IFeatureModule
1112
public void Register(IServiceCollection services)
1213
{
1314
services.AddScoped(typeof(IEntityService<>), typeof(EntityService<>));
14-
services.AddScoped<TrackableSaveChangesInterceptor>();
15+
services.TryAddSingleton<TrackableSaveChangesInterceptor>();
1516
services.AddSingleton(typeof(IEntityRouteProvider<>), typeof(EntityRouteProvider<>));
1617
}
1718
}

src/Identity/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avolutions.Baf.Core.Identity.Models;
22
using Avolutions.Baf.Core.Identity.Services;
3+
using Avolutions.Baf.Core.Persistence;
34
using Microsoft.AspNetCore.Components.Authorization;
45
using Microsoft.AspNetCore.Identity;
56
using Microsoft.Extensions.DependencyInjection;

src/Jobs/Infrastructure/JobService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avolutions.Baf.Core.Identity.Models;
44
using Avolutions.Baf.Core.Jobs.Abstractions;
55
using Avolutions.Baf.Core.Jobs.Models;
6+
using Avolutions.Baf.Core.Persistence;
67
using Microsoft.EntityFrameworkCore;
78

89
namespace Avolutions.Baf.Core.Jobs.Infrastructure;

src/Jobs/Infrastructure/JobWorker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Channels;
33
using Avolutions.Baf.Core.Jobs.Abstractions;
44
using Avolutions.Baf.Core.Jobs.Models;
5+
using Avolutions.Baf.Core.Persistence;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Hosting;
78

src/Module/Extensions/ServiceCollectionExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avolutions.Baf.Core.Entity.Interceptors;
44
using Avolutions.Baf.Core.Lookups.Interceptors;
55
using Avolutions.Baf.Core.Module.Abstractions;
6+
using Avolutions.Baf.Core.Persistence;
67
using Microsoft.EntityFrameworkCore;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -33,6 +34,15 @@ public static class ServiceCollectionExtensions
3334
public static IServiceCollection AddBafCore<TContext>(this IServiceCollection services, params Assembly[] assemblies)
3435
where TContext : BafDbContext
3536
{
37+
// Register a factory alias so framework services (e.g., EntityService<T>) can depend on
38+
// IDbContextFactory<BafDbContext> without knowing the concrete context type.
39+
// This is the factory equivalent of the scoped DbContext alias below.
40+
services.TryAddScoped<IDbContextFactory<BafDbContext>>(sp =>
41+
new BafDbContextFactoryWrapper<TContext>(sp.GetRequiredService<IDbContextFactory<TContext>>()));
42+
43+
// Keep scoped aliases for backward compatibility.
44+
// AddDbContextFactory already registers TContext as scoped, so API controllers
45+
// and other non-Blazor code that injects DbContext/BafDbContext directly still works.
3646
services.TryAddScoped<DbContext>(sp => sp.GetRequiredService<TContext>());
3747
services.TryAddScoped<BafDbContext>(sp => sp.GetRequiredService<TContext>());
3848

@@ -48,7 +58,7 @@ public static IServiceCollection AddBafCore<TContext>(this IServiceCollection se
4858
services.AddSingleton(new BafRegistry(modules, moduleAssemblies));
4959

5060
// Add database context interceptors
51-
services.AddDbContext<TContext>((sp, options) =>
61+
services.AddDbContextFactory<TContext>((sp, options) =>
5262
{
5363
options.AddInterceptors(
5464
sp.GetRequiredService<AuditSaveChangesInterceptor>(),

src/NumberSequences/Services/NumberSequenceService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avolutions.Baf.Core.NumberSequences.Models;
2+
using Avolutions.Baf.Core.Persistence;
23
using Microsoft.EntityFrameworkCore;
34

45
namespace Avolutions.Baf.Core.NumberSequences.Services;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Microsoft.EntityFrameworkCore;
1010
using Microsoft.EntityFrameworkCore.Infrastructure;
1111

12-
namespace Avolutions.Baf.Core;
12+
namespace Avolutions.Baf.Core.Persistence;
1313

1414
/// <summary>
1515
/// Base DbContext for the BAF framework.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace Avolutions.Baf.Core.Persistence;
4+
5+
internal class BafDbContextFactoryWrapper<TContext> : IDbContextFactory<BafDbContext>
6+
where TContext : BafDbContext
7+
{
8+
private readonly IDbContextFactory<TContext> _inner;
9+
10+
public BafDbContextFactoryWrapper(IDbContextFactory<TContext> inner)
11+
{
12+
_inner = inner;
13+
}
14+
15+
public BafDbContext CreateDbContext()
16+
{
17+
return _inner.CreateDbContext();
18+
}
19+
}

src/Settings/Infrastructure/SettingsStore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Concurrent;
22
using System.Reflection;
33
using System.Text.Json;
4+
using Avolutions.Baf.Core.Persistence;
45
using Avolutions.Baf.Core.Settings.Abstractions;
56
using Avolutions.Baf.Core.Settings.Attributes;
67
using Avolutions.Baf.Core.Settings.Models;

0 commit comments

Comments
 (0)