From 0374e054020c45ca0bb7239ef3564255b4f41717 Mon Sep 17 00:00:00 2001 From: Nguyen Xuan Nhan Date: Mon, 26 Aug 2024 00:18:01 +0700 Subject: [PATCH] refactor: Migration memory outbox to mongodb outbox for Rating service --- Directory.Packages.props | 2 +- src/BookWorm.Basket/Extensions/Extensions.cs | 5 ++- .../Extensions/Extensions.cs | 4 ++- src/BookWorm.Rating/BookWorm.Rating.csproj | 2 +- src/BookWorm.Rating/Extensions/Extensions.cs | 32 ++++++++++--------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 3f545e9..5d6f4d8 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -24,7 +24,6 @@ - @@ -94,6 +93,7 @@ + diff --git a/src/BookWorm.Basket/Extensions/Extensions.cs b/src/BookWorm.Basket/Extensions/Extensions.cs index 90adfb8..c802e4e 100644 --- a/src/BookWorm.Basket/Extensions/Extensions.cs +++ b/src/BookWorm.Basket/Extensions/Extensions.cs @@ -21,7 +21,10 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.AddValidatorsFromAssemblyContaining(includeInternalTypes: true); - builder.AddRabbitMqEventBus(typeof(global::Program), cfg => cfg.AddInMemoryInboxOutbox()); + builder.AddRabbitMqEventBus(typeof(global::Program), cfg => + { + cfg.AddInMemoryInboxOutbox(); + }); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/src/BookWorm.Ordering/Extensions/Extensions.cs b/src/BookWorm.Ordering/Extensions/Extensions.cs index 6008004..4631fa7 100644 --- a/src/BookWorm.Ordering/Extensions/Extensions.cs +++ b/src/BookWorm.Ordering/Extensions/Extensions.cs @@ -29,6 +29,7 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.AddValidatorsFromAssemblyContaining(includeInternalTypes: true); builder.AddRabbitMqEventBus(typeof(global::Program), cfg => + { cfg.AddEntityFrameworkOutbox(o => { o.QueryDelay = TimeSpan.FromSeconds(1); @@ -36,7 +37,8 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) o.UsePostgres(); o.UseBusOutbox(); - })); + }); + }); builder.Services.AddMarten(_ => { diff --git a/src/BookWorm.Rating/BookWorm.Rating.csproj b/src/BookWorm.Rating/BookWorm.Rating.csproj index c40a08c..8487eb4 100644 --- a/src/BookWorm.Rating/BookWorm.Rating.csproj +++ b/src/BookWorm.Rating/BookWorm.Rating.csproj @@ -3,7 +3,7 @@ - + diff --git a/src/BookWorm.Rating/Extensions/Extensions.cs b/src/BookWorm.Rating/Extensions/Extensions.cs index 7de583c..2c0f879 100644 --- a/src/BookWorm.Rating/Extensions/Extensions.cs +++ b/src/BookWorm.Rating/Extensions/Extensions.cs @@ -18,7 +18,6 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.AddProblemDetails(); builder.AddDefaultAuthentication(); - builder.AddMongoDBClient(ServiceName.Database.Rating); builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblyContaining(); @@ -27,9 +26,25 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) cfg.AddOpenBehavior(typeof(MetricsBehavior<,>)); }); + var conn = builder.Configuration.GetConnectionString(ServiceName.Database.Rating); + + builder.Services.AddSingleton(_ => new MongoClient(conn)); + + builder.Services.AddSingleton(provider => + provider.GetService()!.GetDatabase(MongoUrl.Create(conn).DatabaseName)); + + builder.Services.AddScoped(provider => + provider.GetService()!.GetCollection(nameof(Feedback))); + builder.AddRabbitMqEventBus(typeof(global::Program), cfg => { - cfg.AddInMemoryInboxOutbox(); + cfg.AddMongoDbOutbox(o => + { + o.DisableInboxCleanupService(); + o.ClientFactory(provider => provider.GetRequiredService()); + o.DatabaseFactory(provider => provider.GetRequiredService()); + o.UseBusOutbox(bo => bo.DisableDeliveryService()); + }); }); builder.Services.AddValidatorsFromAssemblyContaining(includeInternalTypes: true); @@ -38,19 +53,6 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.AddSingleton(); builder.Services.AddSingleton(); - builder.Services.AddSingleton(serviceProvider => - { - var url = builder.Configuration.GetConnectionString(ServiceName.Database.Rating); - var client = serviceProvider.GetService(); - return client!.GetDatabase(MongoUrl.Create(url).DatabaseName); - }); - - builder.Services.AddScoped(serviceProvider => - { - var database = serviceProvider.GetService(); - return database!.GetCollection(nameof(Feedback)); - }); - builder.Services.AddScoped(); builder.AddVersioning();