Skip to content

Commit

Permalink
Merge branch 'refactor/use_domain_events_in_tests' of https://github.…
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilbaczek committed Mar 29, 2024
2 parents 08b4671 + 2864e45 commit 1cfbed2
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void OffersShouldCommunicateWithPassesViaEvents()
var offersModule = Solution.Types
.That()
.ResideInNamespace(Modules.Offers);

var shouldModule = Solution.Types
.That()
.ResideInNamespace(Modules.Passes)
Expand Down
2 changes: 1 addition & 1 deletion Chapter-1-initial-architecture/Src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ services:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 10s
timeout: 5s
retries: 5
retries: 5
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Modules;

using Microsoft.FeatureManagement;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

public static class ModuleAvailabilityChecker
public sealed class ModuleAvailabilityChecker : IDisposable
{
public static bool IsModuleEnabled(this IServiceCollection services, string module)
private readonly IFeatureManager _featureManager;
private readonly ServiceProvider _serviceProvider;

private ModuleAvailabilityChecker(IFeatureManager featureManager, ServiceProvider serviceProvider)
{
_featureManager = featureManager;
_serviceProvider = serviceProvider;
}

public static ModuleAvailabilityChecker Create(IConfiguration configuration)
{
var buildServiceProvider = services.BuildServiceProvider();
var featureManager = buildServiceProvider.GetRequiredService<IFeatureManager>();
var featureFlagServiceProvider = BuildIsolatedFeatureFlagServiceProvider(configuration);
var featureManager = featureFlagServiceProvider.GetRequiredService<IFeatureManager>();

return featureManager.IsEnabledAsync(module).GetAwaiter().GetResult();
return new ModuleAvailabilityChecker(featureManager, featureFlagServiceProvider);
}

public static bool IsModuleEnabled(this IApplicationBuilder applicationBuilder, string module)
private static ServiceProvider BuildIsolatedFeatureFlagServiceProvider(IConfiguration configuration) => new ServiceCollection()
.AddSingleton(configuration)
.AddFeatureManagement()
.Services
.BuildServiceProvider();

public bool IsModuleEnabled(string module) =>
_featureManager.IsEnabledAsync(module).GetAwaiter().GetResult();

private void Dispose(bool disposing)
{
var buildServiceProvider = applicationBuilder.ApplicationServices;
var featureManager = buildServiceProvider.GetRequiredService<IFeatureManager>();
if (disposing)
{
_serviceProvider.Dispose();
}
}

return featureManager.IsEnabledAsync(module).GetAwaiter().GetResult();
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Modules;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;

public static class ModuleAvailabilityCheckerExtensions
{
public static bool IsModuleEnabled(this IApplicationBuilder applicationBuilder, string module)
{
using var scope = applicationBuilder.ApplicationServices.CreateScope();
var featureManager = scope.ServiceProvider.GetRequiredService<IFeatureManager>();

var enabled = featureManager.IsEnabledAsync(module).GetAwaiter().GetResult();

return enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public static void RegisterContracts(this WebApplication app, string module)
app.MapContracts();
}

public static IServiceCollection AddContracts(this IServiceCollection services, IConfiguration configuration,
string module)
public static IServiceCollection AddContracts(this IServiceCollection services,
string module, IConfiguration configuration, ModuleAvailabilityChecker checker)
{
if (!services.IsModuleEnabled(module))
if (!checker.IsModuleEnabled(module))
{
return services;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EvolutionaryArchitecture.Fitnet;
namespace EvolutionaryArchitecture.Fitnet.Modules;

internal record Module(string Value)
{
Expand All @@ -8,4 +8,4 @@ internal record Module(string Value)
internal static readonly Module Reports = new("Reports");

public static implicit operator string(Module module) => module.Value;
}
}
27 changes: 27 additions & 0 deletions Chapter-2-modules-separation/Src/Fitnet/Modules/ModulesRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace EvolutionaryArchitecture.Fitnet.Modules;

using EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Modules;
using Contracts.Api;
using Offers.Api;
using Passes.Api;
using Reports;

internal static class ModulesRegistry
{
internal static void AddModules(this IServiceCollection services, IConfiguration configuration)
{
using var moduleAvailabilityChecker = ModuleAvailabilityChecker.Create(configuration);
services.AddContracts(Module.Contracts, configuration, moduleAvailabilityChecker);
services.AddPasses(Module.Passes, configuration, moduleAvailabilityChecker);
services.AddOffers(Module.Offers, configuration, moduleAvailabilityChecker);
services.AddReports(Module.Reports, moduleAvailabilityChecker);
}

internal static void RegisterModules(this WebApplication app)
{
app.RegisterContracts(Module.Contracts);
app.RegisterPasses(Module.Passes);
app.RegisterOffers(Module.Offers);
app.RegisterReports(Module.Reports);
}
}
18 changes: 4 additions & 14 deletions Chapter-2-modules-separation/Src/Fitnet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
using EvolutionaryArchitecture.Fitnet;
using EvolutionaryArchitecture.Fitnet.Common.Api.ErrorHandling;
using EvolutionaryArchitecture.Fitnet.Common.Core.SystemClock;
using EvolutionaryArchitecture.Fitnet.Common.Infrastructure;
using EvolutionaryArchitecture.Fitnet.Contracts.Api;
using EvolutionaryArchitecture.Fitnet.Offers.Api;
using EvolutionaryArchitecture.Fitnet.Passes.Api;
using EvolutionaryArchitecture.Fitnet.Reports;
using EvolutionaryArchitecture.Fitnet.Modules;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddSystemClock();
builder.Services.AddExceptionHandling();
builder.Services.AddCommonInfrastructure();
builder.Services.AddContracts(builder.Configuration, Module.Contracts);
builder.Services.AddPasses(builder.Configuration, Module.Passes);
builder.Services.AddOffers(builder.Configuration, Module.Offers);
builder.Services.AddReports(Module.Reports);
builder.Services.AddModules(builder.Configuration);

var app = builder.Build();

Expand All @@ -37,13 +29,11 @@

app.MapControllers();

app.RegisterContracts(Module.Contracts);
app.RegisterPasses(Module.Passes);
app.RegisterOffers(Module.Offers);
app.RegisterReports(Module.Reports);
app.RegisterModules();

app.Run();


namespace EvolutionaryArchitecture.Fitnet
{
[UsedImplicitly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"Reports": "Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword",
"Offers": "Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public static void RegisterOffers(this WebApplication app, string module)
app.UseOffers();
}

public static IServiceCollection AddOffers(this IServiceCollection services, IConfiguration configuration,
string module)
public static IServiceCollection AddOffers(this IServiceCollection services,
string module,
IConfiguration configuration,
ModuleAvailabilityChecker moduleAvailabilityChecker)
{
if (!services.IsModuleEnabled(module))
if (!moduleAvailabilityChecker.IsModuleEnabled(module))
{
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public static void RegisterPasses(this WebApplication app, string module)
app.MapPasses();
}

public static IServiceCollection AddPasses(this IServiceCollection services, IConfiguration configuration,
string module)
public static IServiceCollection AddPasses(this IServiceCollection services,
string module, IConfiguration configuration,
ModuleAvailabilityChecker availabilityChecker)
{
if (!services.IsModuleEnabled(module))
if (!availabilityChecker.IsModuleEnabled(module))
{
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static void RegisterReports(this WebApplication app, string module)
app.MapReports();
}

public static IServiceCollection AddReports(this IServiceCollection services, string module)
public static IServiceCollection AddReports(this IServiceCollection services, string module, ModuleAvailabilityChecker availabilityChecker)
{
if (!services.IsModuleEnabled(module))
if (!availabilityChecker.IsModuleEnabled(module))
{
return services;
}
Expand Down
2 changes: 1 addition & 1 deletion Chapter-2-modules-separation/Src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ services:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 10s
timeout: 5s
retries: 5
retries: 5

0 comments on commit 1cfbed2

Please sign in to comment.