Skip to content

Commit

Permalink
Add OutboxRecord ClassMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken committed Jan 15, 2024
1 parent 8d3bea1 commit 80ff0e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using MinimalDomainEvents.Outbox.Abstractions;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

namespace MinimalDomainEvents.Outbox.MongoDb;
public static class IOutboxDispatcherBuilderExtensions
{
public static IOutboxDispatcherBuilder AddMongo(this IOutboxDispatcherBuilder builder, MongoClient mongoClient)
{
TryRegisterOutboxRecordClassMap();
builder.Services.AddScoped<IPersistOutboxRecords>(sp => ActivatorUtilities.CreateInstance<MongoDbDomainEventPersister>(sp, builder.OutboxSettings, mongoClient));
return builder;
}

public static IOutboxDispatcherBuilder AddMongo(this IOutboxDispatcherBuilder builder, Func<IServiceProvider, MongoClient> mongoClientFactory)
{
TryRegisterOutboxRecordClassMap();
builder.Services.AddScoped<IPersistOutboxRecords>(sp => ActivatorUtilities.CreateInstance<MongoDbDomainEventPersister>(sp, builder.OutboxSettings, mongoClientFactory(sp)));
return builder;
}

private static bool TryRegisterOutboxRecordClassMap()
{
return BsonClassMap.TryRegisterClassMap<OutboxRecord>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace MinimalDomainEvents.Outbox.MongoDb.UnitTests;

[Collection("MongoDb Integration")]
public class IOutboxDispatcherBuilderExtensionsTests(MongoContainerFixture fixture)
public class OutboxBuilderExtensionsTests(MongoContainerFixture fixture)
{
[Fact]
public async Task WithDatabase_AfterUseMongo_StillWorks()
public async Task WithDatabase_After_UseMongo_Works()
{
var mongoClient = new MongoClient(fixture.ConnectionString);
IServiceCollection serviceCollection = new ServiceCollection();
Expand Down

0 comments on commit 80ff0e9

Please sign in to comment.