Skip to content

Commit

Permalink
refactor: Migration memory outbox to mongodb outbox for Rating service
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Aug 25, 2024
1 parent 091fdf1 commit 82fbd6b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<PackageVersion Include="Aspire.Hosting.Redis" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Npgsql" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.MongoDB.Driver" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Azure.Storage.Blobs" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="$(AspireUnstablePackagesVersion)" />
<PackageVersion Include="Aspire.StackExchange.Redis" Version="$(AspireVersion)" />
Expand Down Expand Up @@ -94,6 +93,7 @@
<!-- MassTransit -->
<PackageVersion Include="MassTransit" Version="$(MassTransitVersion)" />
<PackageVersion Include="MassTransit.RabbitMQ" Version="$(MassTransitVersion)" />
<PackageVersion Include="MassTransit.MongoDb" Version="$(MassTransitVersion)" />
<PackageVersion Include="MassTransit.EntityFrameworkCore" Version="$(MassTransitVersion)" />
<!-- Miscellaneous -->
<PackageVersion Include="Scrutor" Version="4.2.2" />
Expand Down
5 changes: 4 additions & 1 deletion src/BookWorm.Basket/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)

builder.Services.AddValidatorsFromAssemblyContaining<global::Program>(includeInternalTypes: true);

builder.AddRabbitMqEventBus(typeof(global::Program), cfg => cfg.AddInMemoryInboxOutbox());
builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
{
cfg.AddInMemoryInboxOutbox();
});

builder.Services.AddSingleton<IActivityScope, ActivityScope>();
builder.Services.AddSingleton<CommandHandlerMetrics>();
Expand Down
4 changes: 3 additions & 1 deletion src/BookWorm.Ordering/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
builder.Services.AddValidatorsFromAssemblyContaining<global::Program>(includeInternalTypes: true);

builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
{
cfg.AddEntityFrameworkOutbox<OrderingContext>(o =>
{
o.QueryDelay = TimeSpan.FromSeconds(1);
o.UsePostgres();
o.UseBusOutbox();
}));
});
});

builder.Services.AddMarten(_ =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/BookWorm.Rating/BookWorm.Rating.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Aspire.MongoDB.Driver" />
<PackageReference Include="MassTransit.MongoDb" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 17 additions & 15 deletions src/BookWorm.Rating/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<global::Program>();
Expand All @@ -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<IMongoClient>(_ => new MongoClient(conn));

builder.Services.AddSingleton(provider =>
provider.GetService<IMongoClient>()!.GetDatabase(MongoUrl.Create(conn).DatabaseName));

builder.Services.AddScoped(provider =>
provider.GetService<IMongoDatabase>()!.GetCollection<Feedback>(nameof(Feedback)));

builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
{
cfg.AddInMemoryInboxOutbox();
cfg.AddMongoDbOutbox(o =>
{
o.DisableInboxCleanupService();
o.ClientFactory(provider => provider.GetRequiredService<IMongoClient>());
o.DatabaseFactory(provider => provider.GetRequiredService<IMongoDatabase>());
o.UseBusOutbox(bo => bo.DisableDeliveryService());
});
});

builder.Services.AddValidatorsFromAssemblyContaining<global::Program>(includeInternalTypes: true);
Expand All @@ -38,19 +53,6 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
builder.Services.AddSingleton<CommandHandlerMetrics>();
builder.Services.AddSingleton<QueryHandlerMetrics>();

builder.Services.AddSingleton(serviceProvider =>
{
var url = builder.Configuration.GetConnectionString(ServiceName.Database.Rating);
var client = serviceProvider.GetService<IMongoClient>();
return client!.GetDatabase(MongoUrl.Create(url).DatabaseName);
});

builder.Services.AddScoped(serviceProvider =>
{
var database = serviceProvider.GetService<IMongoDatabase>();
return database!.GetCollection<Feedback>(nameof(Feedback));
});

builder.Services.AddScoped<IRatingRepository, RatingRepository>();

builder.AddVersioning();
Expand Down

0 comments on commit 82fbd6b

Please sign in to comment.