Skip to content

Commit

Permalink
Fix DI lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorstenThiel committed Oct 22, 2023
1 parent 101b938 commit 051928f
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Fluss.HotChocolate/Fluss.HotChocolate.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0; net7.0; net6.0</TargetFrameworks>
<TargetFrameworks>net8.0; net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fluss.PostgreSQL/Fluss.PostgreSQL.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0; net7.0; net6.0</TargetFrameworks>
<TargetFrameworks>net8.0; net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
37 changes: 12 additions & 25 deletions src/Fluss.PostgreSQL/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddPostgresEventSourcingRepository(this IServiceCollection services,
string connectionString, Assembly? upcasterSourceAssembly = null)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services);

if (upcasterSourceAssembly is not null)
{
Expand All @@ -27,7 +24,7 @@ public static IServiceCollection AddPostgresEventSourcingRepository(this IServic
}

return services
.AddScoped<IBaseEventRepository, Fluss.PostgreSQL.PostgreSQLEventRepository>()
.AddBaseEventRepository<PostgreSQLEventRepository>()
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddPostgres()
Expand All @@ -43,15 +40,15 @@ public static IServiceCollection AddPostgresEventSourcingRepository(this IServic
public class Migrator : BackgroundService
{
private readonly ILogger<Migrator> _logger;
private readonly IMigrationRunner _migrationRunner;
private readonly IServiceProvider _serviceProvider;
private bool _didFinish;

private readonly SemaphoreSlim _didFinishChanged = new(0, 1);

public Migrator(IMigrationRunner migrationRunner, ILogger<Migrator> logger)
public Migrator(ILogger<Migrator> logger, IServiceProvider serviceProvider)
{
_migrationRunner = migrationRunner;
_logger = logger;
_serviceProvider = serviceProvider;
}

public async Task WaitForFinish()
Expand All @@ -77,30 +74,20 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
Migrate();
var scope = _serviceProvider.CreateScope();
var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>()
migrationRunner.MigrateUp();

_didFinish = true;
_didFinishChanged.Release();
}
catch (Exception e)
{
_logger.LogError(e, "Error while migrating");
Environment.Exit(-1);
}
}, stoppingToken);
}

public void Migrate()
{
//_migrationRunner.ListMigrations();
try
{
_migrationRunner.MigrateUp();
}
catch
{
Environment.Exit(-1);
}

_didFinish = true;
_didFinishChanged.Release();
}
}

public class Upcaster : BackgroundService
Expand Down
2 changes: 1 addition & 1 deletion src/Fluss.Testing/Fluss.Testing.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0; net7.0; net6.0</TargetFrameworks>
<TargetFrameworks>net8.0; net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fluss.UnitTest/Fluss.UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0; net7.0; net6.0</TargetFrameworks>
<TargetFrameworks>net8.0; net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
6 changes: 3 additions & 3 deletions src/Fluss/Authentication/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static IServiceCollection AddPoliciesFrom(this IServiceCollection service

foreach (var policyType in policyTypes)
{
services = services
.AddScoped(policyType)
.AddScoped(sp => (Policy)sp.GetRequiredService(policyType));
services
.AddSingleton(policyType)
.AddSingleton(sp => (Policy)sp.GetRequiredService(policyType));
}

return services;
Expand Down
2 changes: 1 addition & 1 deletion src/Fluss/Fluss.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0; net7.0; net6.0</TargetFrameworks>
<TargetFrameworks>net8.0; net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Fluss</PackageId>
Expand Down
2 changes: 1 addition & 1 deletion src/Fluss/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static IServiceCollection AddUpcasters(this IServiceCollection services,

foreach (var upcaster in upcasters)
{
services.AddScoped(upcasterType, upcaster);
services.AddSingleton(upcasterType, upcaster);
}

return services.AddSingleton<EventUpcasterService>().AddSingleton<UpcasterSorter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IServiceCollection RegisterSideEffects(this IServiceCollection ser

foreach (var @class in implementingClasses)
{
services.AddScoped(typeof(SideEffect), @class);
services.AddSingleton(typeof(SideEffect), @class);
}

services.AddHostedService<SideEffectDispatcher>();
Expand Down
4 changes: 2 additions & 2 deletions src/Fluss/Validation/ValidationServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public static IServiceCollection AddValidationFrom(this IServiceCollection servi
sourceAssembly.GetTypes().Where(t => t.IsAssignableTo(aggregateValidatorType)).ToList();
foreach (var aggregateValidator in aggregateValidators)
{
services.AddScoped(aggregateValidatorType, aggregateValidator);
services.AddSingleton(aggregateValidatorType, aggregateValidator);
}

var eventValidatorType = typeof(EventValidator);
var eventValidators =
sourceAssembly.GetTypes().Where(t => t.IsAssignableTo(eventValidatorType)).ToList();
foreach (var eventValidator in eventValidators)
{
services.AddScoped(eventValidatorType, eventValidator);
services.AddSingleton(eventValidatorType, eventValidator);
}

services.AddSingleton<IRootValidator, RootValidator>();
Expand Down

0 comments on commit 051928f

Please sign in to comment.