11using System . Text . Json ;
2+ using Microsoft . EntityFrameworkCore ;
23using Microsoft . Extensions . DependencyInjection ;
34using Microsoft . Extensions . Logging ;
45using Microsoft . Extensions . Options ;
@@ -8,29 +9,42 @@ namespace SIL.Harmony;
89
910public static class CrdtKernel
1011{
12+ public static IServiceCollection AddCrdtDataDbFactory < TContext > ( this IServiceCollection services ,
13+ Action < CrdtConfig > configureCrdt ) where TContext : DbContext , ICrdtDbContext
14+ {
15+
16+ services . AddCrdtDataCore ( configureCrdt ) ;
17+ services . AddScoped < ICrdtDbContextFactory , CrdtDbContextFactory < TContext > > ( ) ;
18+ return services ;
19+ }
20+
1121 public static IServiceCollection AddCrdtData < TContext > ( this IServiceCollection services ,
12- Action < CrdtConfig > configureCrdt ) where TContext : ICrdtDbContext
22+ Action < CrdtConfig > configureCrdt ) where TContext : DbContext , ICrdtDbContext
23+ {
24+ services . AddCrdtDataCore ( configureCrdt ) ;
25+ services . AddScoped < ICrdtDbContextFactory , CrdtDbContextNoDisposeFactory < TContext > > ( ) ;
26+ return services ;
27+ }
28+ public static IServiceCollection AddCrdtDataCore ( this IServiceCollection services , Action < CrdtConfig > configureCrdt )
1329 {
1430 services . AddLogging ( ) ;
15- services . AddOptions < CrdtConfig > ( ) . Configure ( configureCrdt ) . PostConfigure ( crdtConfig => crdtConfig . ObjectTypeListBuilder . Freeze ( ) ) ;
31+ services . AddOptions < CrdtConfig > ( ) . Configure ( configureCrdt )
32+ . PostConfigure ( crdtConfig => crdtConfig . ObjectTypeListBuilder . Freeze ( ) ) ;
1633 services . AddSingleton ( sp => sp . GetRequiredService < IOptions < CrdtConfig > > ( ) . Value . JsonSerializerOptions ) ;
1734 services . AddSingleton ( TimeProvider . System ) ;
1835 services . AddScoped < IHybridDateTimeProvider > ( NewTimeProvider ) ;
19- //must use factory, otherwise one context will be created for this registration, and one for the application.
20- //we want to have one context per application
21- services . AddScoped < ICrdtDbContext > ( p => p . GetRequiredService < TContext > ( ) ) ;
22- services . AddScoped < CrdtRepository > ( ) ;
36+ services . AddScoped < CrdtRepositoryFactory > ( ) ;
2337 //must use factory method because DataModel constructor is internal
2438 services . AddScoped < DataModel > ( provider => new DataModel (
25- provider . GetRequiredService < CrdtRepository > ( ) ,
39+ provider . GetRequiredService < CrdtRepositoryFactory > ( ) ,
2640 provider . GetRequiredService < JsonSerializerOptions > ( ) ,
2741 provider . GetRequiredService < IHybridDateTimeProvider > ( ) ,
2842 provider . GetRequiredService < IOptions < CrdtConfig > > ( ) ,
2943 provider . GetRequiredService < ILogger < DataModel > > ( )
3044 ) ) ;
3145 //must use factory method because ResourceService constructor is internal
3246 services . AddScoped < ResourceService > ( provider => new ResourceService (
33- provider . GetRequiredService < CrdtRepository > ( ) ,
47+ provider . GetRequiredService < CrdtRepositoryFactory > ( ) ,
3448 provider . GetRequiredService < IOptions < CrdtConfig > > ( ) ,
3549 provider . GetRequiredService < DataModel > ( ) ,
3650 provider . GetRequiredService < ILogger < ResourceService > > ( )
@@ -43,8 +57,11 @@ public static HybridDateTimeProvider NewTimeProvider(IServiceProvider servicePro
4357 //todo, if this causes issues getting the order correct, we can update the last date time after the db is created
4458 //as long as it's before we get a date time from the provider
4559 //todo use IMemoryCache to store the last date time, possibly based on the current project
46- var hybridDateTime = serviceProvider . GetRequiredService < CrdtRepository > ( ) . GetLatestDateTime ( ) ;
60+ using var repo = serviceProvider . GetRequiredService < CrdtRepositoryFactory > ( ) . CreateRepositorySync ( ) ;
61+ var hybridDateTime = repo . GetLatestDateTime ( ) ;
4762 hybridDateTime ??= HybridDateTimeProvider . DefaultLastDateTime ;
4863 return ActivatorUtilities . CreateInstance < HybridDateTimeProvider > ( serviceProvider , hybridDateTime ) ;
4964 }
5065}
66+
67+
0 commit comments