Browse Source

Merge pull request #23673 from abpframework/AbpBsonSerializer

Add AbpBsonSerializer utility class
issue-22408
Engincan VESKE 1 year ago
committed by GitHub
parent
commit
8ee5ad7ee7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 31
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpBsonSerializer.cs
  2. 3
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs

31
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpBsonSerializer.cs

@ -0,0 +1,31 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using MongoDB.Bson.Serialization;
namespace Volo.Abp.MongoDB;
public static class AbpBsonSerializer
{
private static readonly ConcurrentDictionary<Type, IBsonSerializer> Cache;
static AbpBsonSerializer()
{
var registry = BsonSerializer.SerializerRegistry;
var type = typeof(BsonSerializerRegistry);
var cacheField = type.GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance) ??
throw new AbpException($"Cannot find _cache field of {type.FullName}.");
Cache = (ConcurrentDictionary<Type, IBsonSerializer>)cacheField.GetValue(registry)!;
}
public static void RemoveSerializer<T>()
{
Cache.TryRemove(typeof(T), out _);
}
public static ConcurrentDictionary<Type, IBsonSerializer> GetSerializerCache()
{
return Cache;
}
}

3
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs

@ -19,7 +19,8 @@ public class AbpMongoDbModule : AbpModule
{ {
static AbpMongoDbModule() static AbpMongoDbModule()
{ {
BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); AbpBsonSerializer.RemoveSerializer<Guid>();
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonTypeMapper.RegisterCustomTypeMapper(typeof(Guid), new AbpGuidCustomBsonTypeMapper()); BsonTypeMapper.RegisterCustomTypeMapper(typeof(Guid), new AbpGuidCustomBsonTypeMapper());
} }

Loading…
Cancel
Save