diff --git a/deployments/docker-compose/docker-compose.yaml b/deployments/docker-compose/docker-compose.yaml index ef50f79..e61ddf5 100644 --- a/deployments/docker-compose/docker-compose.yaml +++ b/deployments/docker-compose/docker-compose.yaml @@ -262,6 +262,7 @@ services: elasticsearch: container_name: elasticsearch image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2 + restart: unless-stopped ports: - 9200:9200 volumes: @@ -280,6 +281,7 @@ services: kibana: container_name: kibana image: docker.elastic.co/kibana/kibana:7.9.2 + restart: unless-stopped ports: - 5601:5601 depends_on: diff --git a/deployments/docker-compose/infrastracture.yaml b/deployments/docker-compose/infrastracture.yaml index 8bd9689..4aac29f 100644 --- a/deployments/docker-compose/infrastracture.yaml +++ b/deployments/docker-compose/infrastracture.yaml @@ -96,6 +96,7 @@ services: elasticsearch: container_name: elasticsearch image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2 + restart: unless-stopped ports: - 9200:9200 volumes: @@ -114,6 +115,7 @@ services: kibana: container_name: kibana image: docker.elastic.co/kibana/kibana:7.9.2 + restart: unless-stopped ports: - 5601:5601 depends_on: diff --git a/src/BuildingBlocks/EFCore/EfIdentityTxBehavior.cs b/src/BuildingBlocks/EFCore/EfIdentityTxBehavior.cs deleted file mode 100644 index e89b788..0000000 --- a/src/BuildingBlocks/EFCore/EfIdentityTxBehavior.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.Data; -using System.Text.Json; -using MediatR; -using Microsoft.Extensions.Logging; - -namespace BuildingBlocks.EFCore; - -public class EfIdentityTxBehavior : IPipelineBehavior - where TRequest : notnull, IRequest - where TResponse : notnull -{ - private readonly ILogger> _logger; - private readonly IDbContext _dbContextBase; - - public EfIdentityTxBehavior( - ILogger> logger, - IDbContext dbContextBase) - { - _logger = logger; - _dbContextBase = dbContextBase; - } - - public async Task Handle( - TRequest request, - CancellationToken cancellationToken, - RequestHandlerDelegate next) - { - _logger.LogInformation( - "{Prefix} Handled command {MediatrRequest}", - nameof(EfTxBehavior), - typeof(TRequest).FullName); - - _logger.LogDebug( - "{Prefix} Handled command {MediatrRequest} with content {RequestContent}", - nameof(EfTxBehavior), - typeof(TRequest).FullName, - JsonSerializer.Serialize(request)); - - _logger.LogInformation( - "{Prefix} Open the transaction for {MediatrRequest}", - nameof(EfTxBehavior), - typeof(TRequest).FullName); - - await _dbContextBase.BeginTransactionAsync(cancellationToken); - - try - { - var response = await next(); - - _logger.LogInformation( - "{Prefix} Executed the {MediatrRequest} request", - nameof(EfTxBehavior), - typeof(TRequest).FullName); - - await _dbContextBase.CommitTransactionAsync(cancellationToken); - - return response; - } - catch - { - await _dbContextBase.RollbackTransactionAsync(cancellationToken); - throw; - } - } -} diff --git a/src/BuildingBlocks/EFCore/Extensions.cs b/src/BuildingBlocks/EFCore/Extensions.cs index cafcfcf..577aefa 100644 --- a/src/BuildingBlocks/EFCore/Extensions.cs +++ b/src/BuildingBlocks/EFCore/Extensions.cs @@ -1,5 +1,6 @@ using System.Linq.Expressions; using BuildingBlocks.Core.Model; +using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.Configuration; @@ -12,7 +13,7 @@ public static class Extensions public static IServiceCollection AddCustomDbContext( this IServiceCollection services, IConfiguration configuration) - where TContext : AppDbContextBase + where TContext : DbContext, IDbContext { services.AddDbContext(options => options.UseSqlServer( @@ -24,6 +25,16 @@ public static class Extensions return services; } + public static IApplicationBuilder UseMigration(this IApplicationBuilder app) + where TContext : DbContext, IDbContext + { + MigrateDatabaseAsync(app.ApplicationServices).GetAwaiter().GetResult(); + SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult(); + + return app; + } + + // ref: https://github.com/pdevito3/MessageBusTestingInMemHarness/blob/main/RecipeManagement/src/RecipeManagement/Databases/RecipesDbContext.cs public static void FilterSoftDeletedProperties(this ModelBuilder modelBuilder) { @@ -41,4 +52,23 @@ public static class Extensions mutableEntityType.SetQueryFilter(lambdaExpression); } } + + private static async Task MigrateDatabaseAsync(IServiceProvider serviceProvider) + where TContext : DbContext, IDbContext + { + using var scope = serviceProvider.CreateScope(); + + var context = scope.ServiceProvider.GetRequiredService(); + await context.Database.MigrateAsync(); + } + + private static async Task SeedDataAsync(IServiceProvider serviceProvider) + { + using var scope = serviceProvider.CreateScope(); + var seeders = scope.ServiceProvider.GetServices(); + foreach (var seeder in seeders) + { + await seeder.SeedAllAsync(); + } + } } diff --git a/src/Services/Booking/src/Booking.Api/Program.cs b/src/Services/Booking/src/Booking.Api/Program.cs index 0da21fe..966b789 100644 --- a/src/Services/Booking/src/Booking.Api/Program.cs +++ b/src/Services/Booking/src/Booking.Api/Program.cs @@ -38,7 +38,7 @@ builder.AddCustomSerilog(); builder.Services.AddJwt(); builder.Services.AddControllers(); builder.Services.AddHttpContextAccessor(); -builder.Services.AddCustomSwagger(builder.Configuration, typeof(BookingRoot).Assembly); +builder.Services.AddCustomSwagger(configuration, typeof(BookingRoot).Assembly); builder.Services.AddCustomVersioning(); builder.Services.AddCustomMediatR(); builder.Services.AddValidatorsFromAssembly(typeof(BookingRoot).Assembly); @@ -69,7 +69,7 @@ if (app.Environment.IsDevelopment()) } app.UseSerilogRequestLogging(); -app.UseMigrations(); +app.UseMigration(); app.UseCorrelationId(); app.UseRouting(); app.UseHttpMetrics(); diff --git a/src/Services/Booking/src/Booking/Extensions/MigrationsExtensions.cs b/src/Services/Booking/src/Booking/Extensions/MigrationsExtensions.cs deleted file mode 100644 index b7f67b9..0000000 --- a/src/Services/Booking/src/Booking/Extensions/MigrationsExtensions.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Booking.Data; -using BuildingBlocks.EFCore; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Booking.Extensions; - -public static class MigrationsExtensions -{ - public static IApplicationBuilder UseMigrations(this IApplicationBuilder app) - { - MigrateDatabase(app.ApplicationServices); - SeedData(app.ApplicationServices); - - return app; - } - - private static void MigrateDatabase(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - - private static void SeedData(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - var seeders = scope.ServiceProvider.GetServices(); - foreach (var seeder in seeders) - { - seeder.SeedAllAsync().GetAwaiter().GetResult(); - } - } -} diff --git a/src/Services/Flight/src/Flight.Api/Program.cs b/src/Services/Flight/src/Flight.Api/Program.cs index f76cfdf..1e29209 100644 --- a/src/Services/Flight/src/Flight.Api/Program.cs +++ b/src/Services/Flight/src/Flight.Api/Program.cs @@ -37,6 +37,7 @@ Console.WriteLine(FiggleFonts.Standard.Render(appOptions.Name)); builder.Services.AddCustomDbContext(configuration); builder.Services.AddScoped(); + builder.Services.AddMongoDbContext(configuration); builder.Services.AddPersistMessage(configuration); @@ -44,7 +45,7 @@ builder.Services.AddPersistMessage(configuration); builder.AddCustomSerilog(); builder.Services.AddJwt(); builder.Services.AddControllers(); -builder.Services.AddCustomSwagger(builder.Configuration, typeof(FlightRoot).Assembly); +builder.Services.AddCustomSwagger(configuration, typeof(FlightRoot).Assembly); builder.Services.AddCustomVersioning(); builder.Services.AddCustomMediatR(); builder.Services.AddValidatorsFromAssembly(typeof(FlightRoot).Assembly); @@ -82,7 +83,7 @@ app.UseSerilogRequestLogging(); app.UseCorrelationId(); app.UseRouting(); app.UseHttpMetrics(); -app.UseMigrations(); +app.UseMigration(); app.UseProblemDetails(); app.UseHttpsRedirection(); app.UseCustomHealthCheck(); diff --git a/src/Services/Flight/src/Flight/Extensions/MigrationsExtensions.cs b/src/Services/Flight/src/Flight/Extensions/MigrationsExtensions.cs deleted file mode 100644 index 61d5235..0000000 --- a/src/Services/Flight/src/Flight/Extensions/MigrationsExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using BuildingBlocks.EFCore; -using Flight.Data; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Flight.Extensions; - -public static class MigrationsExtensions -{ - public static IApplicationBuilder UseMigrations(this IApplicationBuilder app) - { - MigrateDatabase(app.ApplicationServices); - SeedData(app.ApplicationServices); - - return app; - } - - private static void MigrateDatabase(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - - private static void SeedData(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - var seeders = scope.ServiceProvider.GetServices(); - foreach (var seeder in seeders) - { - seeder.SeedAllAsync().GetAwaiter().GetResult(); - } - } -} diff --git a/src/Services/Identity/src/Identity.Api/Program.cs b/src/Services/Identity/src/Identity.Api/Program.cs index aebc6ab..0b8a0ee 100644 --- a/src/Services/Identity/src/Identity.Api/Program.cs +++ b/src/Services/Identity/src/Identity.Api/Program.cs @@ -27,24 +27,19 @@ var env = builder.Environment; var appOptions = builder.Services.GetOptions("AppOptions"); Console.WriteLine(FiggleFonts.Standard.Render(appOptions.Name)); -builder.Services.AddScoped(provider => provider.GetService()!); - -builder.Services.AddDbContext(options => - options.UseSqlServer( - configuration.GetConnectionString("DefaultConnection"), - x => x.MigrationsAssembly(typeof(IdentityRoot).Assembly.GetName().Name))); +builder.Services.AddCustomDbContext(configuration); +builder.Services.AddScoped(); builder.Services.AddPersistMessage(configuration); builder.AddCustomSerilog(); builder.Services.AddControllers(); -builder.Services.AddCustomSwagger(builder.Configuration, typeof(IdentityRoot).Assembly); +builder.Services.AddCustomSwagger(configuration, typeof(IdentityRoot).Assembly); builder.Services.AddCustomVersioning(); builder.Services.AddCustomMediatR(); builder.Services.AddValidatorsFromAssembly(typeof(IdentityRoot).Assembly); builder.Services.AddCustomProblemDetails(); builder.Services.AddCustomMapster(typeof(IdentityRoot).Assembly); -builder.Services.AddScoped(); builder.Services.AddCustomHealthCheck(); builder.Services.AddTransient(); @@ -62,7 +57,7 @@ if (app.Environment.IsDevelopment()) } app.UseSerilogRequestLogging(); -app.UseMigrations(); +app.UseMigration(); app.UseCorrelationId(); app.UseRouting(); app.UseHttpMetrics(); diff --git a/src/Services/Identity/src/Identity/Extensions/MediatRExtensions.cs b/src/Services/Identity/src/Identity/Extensions/MediatRExtensions.cs index 97dbbf2..0d13166 100644 --- a/src/Services/Identity/src/Identity/Extensions/MediatRExtensions.cs +++ b/src/Services/Identity/src/Identity/Extensions/MediatRExtensions.cs @@ -13,7 +13,7 @@ public static class MediatRExtensions services.AddMediatR(typeof(IdentityRoot).Assembly); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); - services.AddScoped(typeof(IPipelineBehavior<,>), typeof(EfIdentityTxBehavior<,>)); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(EfTxBehavior<,>)); return services; } diff --git a/src/Services/Identity/src/Identity/Extensions/MigrationsExtensions.cs b/src/Services/Identity/src/Identity/Extensions/MigrationsExtensions.cs deleted file mode 100644 index 90477c7..0000000 --- a/src/Services/Identity/src/Identity/Extensions/MigrationsExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using BuildingBlocks.EFCore; -using Identity.Data; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Identity.Extensions; - -public static class MigrationsExtensions -{ - public static IApplicationBuilder UseMigrations(this IApplicationBuilder app) - { - MigrateDatabase(app.ApplicationServices); - SeedData(app.ApplicationServices); - - return app; - } - - private static void MigrateDatabase(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - - private static void SeedData(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - var seeders = scope.ServiceProvider.GetServices(); - foreach (var seeder in seeders) - { - seeder.SeedAllAsync().GetAwaiter().GetResult(); - } - } -} diff --git a/src/Services/Passenger/src/Passenger.Api/Program.cs b/src/Services/Passenger/src/Passenger.Api/Program.cs index bcb5804..134276e 100644 --- a/src/Services/Passenger/src/Passenger.Api/Program.cs +++ b/src/Services/Passenger/src/Passenger.Api/Program.cs @@ -34,7 +34,7 @@ builder.Services.AddPersistMessage(configuration); builder.AddCustomSerilog(); builder.Services.AddJwt(); builder.Services.AddControllers(); -builder.Services.AddCustomSwagger(builder.Configuration, typeof(PassengerRoot).Assembly); +builder.Services.AddCustomSwagger(configuration, typeof(PassengerRoot).Assembly); builder.Services.AddCustomVersioning(); builder.Services.AddCustomMediatR(); builder.Services.AddValidatorsFromAssembly(typeof(PassengerRoot).Assembly); @@ -62,7 +62,7 @@ if (app.Environment.IsDevelopment()) } app.UseSerilogRequestLogging(); -app.UseMigrations(); +app.UseMigration(); app.UseCorrelationId(); app.UseRouting(); app.UseHttpMetrics(); diff --git a/src/Services/Passenger/src/Passenger/Extensions/MigrationsExtensions.cs b/src/Services/Passenger/src/Passenger/Extensions/MigrationsExtensions.cs deleted file mode 100644 index 5ddfd01..0000000 --- a/src/Services/Passenger/src/Passenger/Extensions/MigrationsExtensions.cs +++ /dev/null @@ -1,38 +0,0 @@ -using BuildingBlocks.EFCore; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Passenger.Data; - -namespace Passenger.Extensions; - -public static class MigrationsExtensions -{ - public static IApplicationBuilder UseMigrations(this IApplicationBuilder app) - { - MigrateDatabase(app.ApplicationServices); - SeedData(app.ApplicationServices); - - return app; - } - - private static void MigrateDatabase(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - - private static void SeedData(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - var seeders = scope.ServiceProvider.GetServices(); - foreach (var seeder in seeders) - { - seeder.SeedAllAsync().GetAwaiter().GetResult(); - } - } -}