Skip to content

Commit

Permalink
Fixed MassTransit#4390 - MongoDB TryRegisterClassMap
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed May 22, 2023
1 parent 40b24c8 commit 967a9d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,23 @@ IMongoCollection<T> CollectionFactory(IServiceProvider provider)

static void RegisterClassMaps()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(InboxState)))
BsonClassMap.TryRegisterClassMap(new BsonClassMap<InboxState>(cfg =>
{
BsonClassMap.RegisterClassMap(new BsonClassMap<InboxState>(cfg =>
{
cfg.AutoMap();
cfg.MapIdProperty(x => x.Id);
}));
}
cfg.AutoMap();
cfg.MapIdProperty(x => x.Id);
}));

if (!BsonClassMap.IsClassMapRegistered(typeof(OutboxState)))
BsonClassMap.TryRegisterClassMap(new BsonClassMap<OutboxState>(cfg =>
{
BsonClassMap.RegisterClassMap(new BsonClassMap<OutboxState>(cfg =>
{
cfg.AutoMap();
cfg.MapIdProperty(x => x.OutboxId);
}));
}
cfg.AutoMap();
cfg.MapIdProperty(x => x.OutboxId);
}));

if (!BsonClassMap.IsClassMapRegistered(typeof(OutboxMessage)))
BsonClassMap.TryRegisterClassMap(new BsonClassMap<OutboxMessage>(cfg =>
{
BsonClassMap.RegisterClassMap(new BsonClassMap<OutboxMessage>(cfg =>
{
cfg.AutoMap();
cfg.MapIdProperty(x => x.Id);
}));
}
cfg.AutoMap();
cfg.MapIdProperty(x => x.Id);
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void Register<T>(ISagaRepositoryRegistrationConfigurator<T> configurator)
{
IMongoCollection<TSaga> MongoDbCollectionFactory(IServiceProvider provider)
{
if (!BsonClassMap.IsClassMapRegistered(typeof(TSaga)))
BsonClassMap.RegisterClassMap(_classMapFactory(provider));
BsonClassMap.TryRegisterClassMap(_classMapFactory(provider));

var database = ProviderDatabaseFactory(provider);
var collectionNameFormatter = CollectionNameFormatterFactory(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public MassTransitMongoDbConventions(ConventionFilter filter = default)

static bool IsMassTransitClass(Type type)
{
return type.FullName.StartsWith("MassTransit") || IsSagaClass(type) && type != typeof(AuditDocument);
return type.FullName.StartsWith("MassTransit") || (IsSagaClass(type) && type != typeof(AuditDocument));
}

static bool IsSagaClass(Type type)
Expand All @@ -56,10 +56,7 @@ static bool IsSagaClass(Type type)

public static void RegisterClass<T>(Expression<Func<T, Guid>> id)
{
if (BsonClassMap.IsClassMapRegistered(typeof(T)))
return;

BsonClassMap.RegisterClassMap<T>(x =>
BsonClassMap.TryRegisterClassMap<T>(x =>
{
x.AutoMap();
x.SetIdMember(x.GetMemberMap(id));
Expand All @@ -68,10 +65,7 @@ public static void RegisterClass<T>(Expression<Func<T, Guid>> id)

public static void RegisterClass<T>()
{
if (BsonClassMap.IsClassMapRegistered(typeof(T)))
return;

BsonClassMap.RegisterClassMap<T>(x =>
BsonClassMap.TryRegisterClassMap<T>(x =>
{
x.AutoMap();
x.SetDiscriminatorIsRequired(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace MassTransit.MongoDbIntegration.Audit
using MongoDB.Driver;


public class MongoDbAuditStore : IMessageAuditStore
public class MongoDbAuditStore :
IMessageAuditStore
{
readonly IMongoCollection<AuditDocument> _collection;

Expand All @@ -17,14 +18,14 @@ static MongoDbAuditStore()
if (BsonClassMap.IsClassMapRegistered(typeof(AuditDocument)))
return;

// easiest way to metadata since keys wont become element names, therefore subject to validation
// will allow keys like $correlationId to be kept
var headersSerializer = new DictionaryInterfaceImplementerSerializer<AuditHeaders, string, string>(
DictionaryRepresentation.ArrayOfDocuments
);

BsonClassMap.RegisterClassMap<AuditDocument>(x =>
BsonClassMap.TryRegisterClassMap<AuditDocument>(x =>
{
// easiest way to metadata since keys wont become element names, therefore subject to validation
// will allow keys like $correlationId to be kept
var headersSerializer = new DictionaryInterfaceImplementerSerializer<AuditHeaders, string, string>(
DictionaryRepresentation.ArrayOfDocuments
);

x.AutoMap();
x.MapIdMember(doc => doc.AuditId);
x.MapMember(doc => doc.Headers).SetSerializer(headersSerializer);
Expand Down

0 comments on commit 967a9d0

Please sign in to comment.