Skip to content

Commit 0374e05

Browse files
committed
refactor: Migration memory outbox to mongodb outbox for Rating service
1 parent 9756f79 commit 0374e05

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

Directory.Packages.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<PackageVersion Include="Aspire.Hosting.Redis" Version="$(AspireVersion)" />
2525
<PackageVersion Include="Aspire.Npgsql" Version="$(AspireVersion)" />
2626
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(AspireVersion)" />
27-
<PackageVersion Include="Aspire.MongoDB.Driver" Version="$(AspireVersion)" />
2827
<PackageVersion Include="Aspire.Azure.Storage.Blobs" Version="$(AspireVersion)" />
2928
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="$(AspireUnstablePackagesVersion)" />
3029
<PackageVersion Include="Aspire.StackExchange.Redis" Version="$(AspireVersion)" />
@@ -94,6 +93,7 @@
9493
<!-- MassTransit -->
9594
<PackageVersion Include="MassTransit" Version="$(MassTransitVersion)" />
9695
<PackageVersion Include="MassTransit.RabbitMQ" Version="$(MassTransitVersion)" />
96+
<PackageVersion Include="MassTransit.MongoDb" Version="$(MassTransitVersion)" />
9797
<PackageVersion Include="MassTransit.EntityFrameworkCore" Version="$(MassTransitVersion)" />
9898
<!-- Miscellaneous -->
9999
<PackageVersion Include="Scrutor" Version="4.2.2" />

src/BookWorm.Basket/Extensions/Extensions.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
2121

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

24-
builder.AddRabbitMqEventBus(typeof(global::Program), cfg => cfg.AddInMemoryInboxOutbox());
24+
builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
25+
{
26+
cfg.AddInMemoryInboxOutbox();
27+
});
2528

2629
builder.Services.AddSingleton<IActivityScope, ActivityScope>();
2730
builder.Services.AddSingleton<CommandHandlerMetrics>();

src/BookWorm.Ordering/Extensions/Extensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
2929
builder.Services.AddValidatorsFromAssemblyContaining<global::Program>(includeInternalTypes: true);
3030

3131
builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
32+
{
3233
cfg.AddEntityFrameworkOutbox<OrderingContext>(o =>
3334
{
3435
o.QueryDelay = TimeSpan.FromSeconds(1);
3536

3637
o.UsePostgres();
3738

3839
o.UseBusOutbox();
39-
}));
40+
});
41+
});
4042

4143
builder.Services.AddMarten(_ =>
4244
{

src/BookWorm.Rating/BookWorm.Rating.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ItemGroup>
44
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
55
<PackageReference Include="Swashbuckle.AspNetCore" />
6-
<PackageReference Include="Aspire.MongoDB.Driver" />
6+
<PackageReference Include="MassTransit.MongoDb" />
77
</ItemGroup>
88

99
<ItemGroup>

src/BookWorm.Rating/Extensions/Extensions.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
1818
builder.Services.AddProblemDetails();
1919

2020
builder.AddDefaultAuthentication();
21-
builder.AddMongoDBClient(ServiceName.Database.Rating);
2221
builder.Services.AddMediatR(cfg =>
2322
{
2423
cfg.RegisterServicesFromAssemblyContaining<global::Program>();
@@ -27,9 +26,25 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
2726
cfg.AddOpenBehavior(typeof(MetricsBehavior<,>));
2827
});
2928

29+
var conn = builder.Configuration.GetConnectionString(ServiceName.Database.Rating);
30+
31+
builder.Services.AddSingleton<IMongoClient>(_ => new MongoClient(conn));
32+
33+
builder.Services.AddSingleton(provider =>
34+
provider.GetService<IMongoClient>()!.GetDatabase(MongoUrl.Create(conn).DatabaseName));
35+
36+
builder.Services.AddScoped(provider =>
37+
provider.GetService<IMongoDatabase>()!.GetCollection<Feedback>(nameof(Feedback)));
38+
3039
builder.AddRabbitMqEventBus(typeof(global::Program), cfg =>
3140
{
32-
cfg.AddInMemoryInboxOutbox();
41+
cfg.AddMongoDbOutbox(o =>
42+
{
43+
o.DisableInboxCleanupService();
44+
o.ClientFactory(provider => provider.GetRequiredService<IMongoClient>());
45+
o.DatabaseFactory(provider => provider.GetRequiredService<IMongoDatabase>());
46+
o.UseBusOutbox(bo => bo.DisableDeliveryService());
47+
});
3348
});
3449

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

41-
builder.Services.AddSingleton(serviceProvider =>
42-
{
43-
var url = builder.Configuration.GetConnectionString(ServiceName.Database.Rating);
44-
var client = serviceProvider.GetService<IMongoClient>();
45-
return client!.GetDatabase(MongoUrl.Create(url).DatabaseName);
46-
});
47-
48-
builder.Services.AddScoped(serviceProvider =>
49-
{
50-
var database = serviceProvider.GetService<IMongoDatabase>();
51-
return database!.GetCollection<Feedback>(nameof(Feedback));
52-
});
53-
5456
builder.Services.AddScoped<IRatingRepository, RatingRepository>();
5557

5658
builder.AddVersioning();

0 commit comments

Comments
 (0)