Skip to content

Commit

Permalink
Merge pull request #233 from meysamhadeli/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
meysamhadeli authored Mar 11, 2023
2 parents 44abe97 + 414ae0d commit 50d0291
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
ci:
runs-on: ubuntu-latest
Expand Down
15 changes: 9 additions & 6 deletions src/BuildingBlocks/EventStoreDB/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Reflection;
using BuildingBlocks.EventStoreDB.BackgroundWorkers;
using BuildingBlocks.EventStoreDB.Events;
using BuildingBlocks.EventStoreDB.Projections;
using BuildingBlocks.EventStoreDB.Repository;
using BuildingBlocks.EventStoreDB.Subscriptions;
Expand All @@ -11,26 +10,30 @@

namespace BuildingBlocks.EventStoreDB;

public class EventStoreDBConfig
using Web;

public class EventStoreOptions
{
public string ConnectionString { get; set; } = default!;
}


public record EventStoreDBOptions(
bool UseInternalCheckpointing = true
);

public static class EventStoreDBConfigExtensions
{
private const string DefaultConfigKey = "EventStore";

public static IServiceCollection AddEventStoreDB(this IServiceCollection services, IConfiguration config,
EventStoreDBOptions? options = null)
{
var eventStoreDBConfig = config.GetSection(DefaultConfigKey).Get<EventStoreDBConfig>();

services
.AddSingleton(new EventStoreClient(EventStoreClientSettings.Create(eventStoreDBConfig.ConnectionString)))
.AddSingleton(x=>
{
var eventStoreOptions = services.GetOptions<EventStoreOptions>(nameof(EventStoreOptions));
return new EventStoreClient(EventStoreClientSettings.Create(eventStoreOptions.ConnectionString));
})
.AddScoped(typeof(IEventStoreDBRepository<>), typeof(EventStoreDBRepository<>))
.AddTransient<EventStoreDBSubscriptionToAll, EventStoreDBSubscriptionToAll>();

Expand Down
4 changes: 4 additions & 0 deletions src/BuildingBlocks/EventStoreDB/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace BuildingBlocks.EventStoreDB;

using Web;

public static class Extensions
{
// ref: https://github.com/oskardudycz/EventSourcing.NetCore/tree/main/Sample/EventStoreDB/ECommerce
Expand All @@ -13,6 +15,8 @@ public static IServiceCollection AddEventStore(
params Assembly[] assemblies
)
{
services.AddValidateOptions<EventStoreOptions>();

var assembliesToScan = assemblies.Length > 0 ? assemblies : new[] { Assembly.GetEntryAssembly()! };

return services
Expand Down
33 changes: 5 additions & 28 deletions src/BuildingBlocks/PersistMessageProcessor/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

namespace BuildingBlocks.PersistMessageProcessor;

using EFCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public static class Extensions
{
Expand Down Expand Up @@ -39,38 +37,17 @@ public static IServiceCollection AddPersistMessageProcessor(this IServiceCollect
return services;
}

public static IApplicationBuilder UseMigration<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
public static IApplicationBuilder UseMigrationPersistMessage<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
where TContext : DbContext, IPersistMessageDbContext
{
MigrateDatabaseAsync<TContext>(app.ApplicationServices).GetAwaiter().GetResult();

if (!env.IsEnvironment("test"))
{
SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult();
}

return app;
}

private static async Task MigrateDatabaseAsync<TContext>(IServiceProvider serviceProvider)
where TContext : DbContext, IPersistMessageDbContext
{
using var scope = serviceProvider.CreateScope();
using var scope = app.ApplicationServices.CreateScope();

var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
await persistMessageContext.Database.MigrateAsync();
persistMessageContext.Database.Migrate();

var context = scope.ServiceProvider.GetRequiredService<TContext>();
await context.Database.MigrateAsync();
}
context.Database.Migrate();

private static async Task SeedDataAsync(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
var seeders = scope.ServiceProvider.GetServices<IDataSeeder>();
foreach (var seeder in seeders)
{
await seeder.SeedAllAsync();
}
return app;
}
}
2 changes: 1 addition & 1 deletion src/BuildingBlocks/TestBase/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private void AddCustomAppSettings(IConfigurationBuilder configuration)
.ToString(NumberFormatInfo.InvariantInfo)),
new("MongoOptions:ConnectionString", MongoDbTestContainer.GetConnectionString()),
new("MongoOptions:DatabaseName", TestContainers.MongoContainerConfiguration.Name),
new("EventStore:ConnectionString", EventStoreDbTestContainer.GetConnectionString())
new("EventStoreOptions:ConnectionString", EventStoreDbTestContainer.GetConnectionString())
});
}

Expand Down
1 change: 0 additions & 1 deletion src/BuildingBlocks/TestBase/TestContainers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public static EventStoreDbContainer EventStoreTestContainer()
var builder = baseBuilder
.WithImage(EventStoreContainerConfiguration.ImageName)
.WithName(EventStoreContainerConfiguration.Name)
.WithPortBinding(EventStoreContainerConfiguration.Port, true)
.Build();

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"Enabled": true,
"ConnectionString": "Server=postgres;Port=5432;Database=persist_message;User Id=postgres;Password=postgres;Include Error Detail=true"
},
"EventStore": {
"EventStoreOptions": {
"ConnectionString": "esdb://eventstore:2113?tls=false"
},
"MongoOptions": {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Booking/src/Booking.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"BreakDuration" : 30
}
},
"EventStore": {
"EventStoreOptions": {
"ConnectionString": "esdb://localhost:2113?tls=false"
},
"MongoOptions": {
Expand Down
3 changes: 0 additions & 3 deletions src/Services/Booking/src/Booking.Api/appsettings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "booking-db-test"
},
"EventStore": {
"ConnectionString": "esdb://localhost:2113?tls=false"
},
"PersistMessageOptions": {
"Interval": 30,
"Enabled": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static WebApplication UseInfrastructure(this WebApplication app)
});
app.UseCorrelationId();
app.UseHttpMetrics();
app.UseMigration<PersistMessageDbContext>(env);
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseCustomHealthCheck();
app.MapMetrics();
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

namespace Flight.Extensions.Infrastructure;

using BuildingBlocks.PersistMessageProcessor.Data;
using Microsoft.AspNetCore.HttpOverrides;

public static class InfrastructureExtensions
Expand Down Expand Up @@ -105,6 +106,7 @@ public static WebApplication UseInfrastructure(this WebApplication app)
});
app.UseCorrelationId();
app.UseHttpMetrics();
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<FlightDbContext>(env);
app.MapMetrics();
app.UseCustomHealthCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace Identity.Extensions.Infrastructure;

using BuildingBlocks.PersistMessageProcessor.Data;
using Configurations;
using Microsoft.AspNetCore.HttpOverrides;

Expand Down Expand Up @@ -101,6 +102,7 @@ public static WebApplication UseInfrastructure(this WebApplication app)
{
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
});
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<IdentityContext>(env);
app.UseCorrelationId();
app.UseHttpMetrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace Passenger.Extensions.Infrastructure;

using BuildingBlocks.PersistMessageProcessor.Data;

public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
Expand Down Expand Up @@ -95,6 +97,7 @@ public static WebApplication UseInfrastructure(this WebApplication app)
{
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
});
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
app.UseMigration<PassengerDbContext>(env);
app.UseCorrelationId();
app.UseHttpMetrics();
Expand Down

0 comments on commit 50d0291

Please sign in to comment.