Upgrade to MongoDb Driver 2.20 and use GuidRepresentationMode.V3 as default representation.

This commit is contained in:
Mohammadreza Taikandi
2023-08-07 10:47:12 +01:00
parent 710b7b992f
commit f59cccb3d9
14 changed files with 110 additions and 33 deletions
@@ -0,0 +1,59 @@
using System;
using System.Collections.Concurrent;
using MongoDB.Bson.Serialization;
namespace MongoDbGenericRepository.Internals
{
/// <summary>
/// An <see cref="IBsonSerializationProvider"/> that can handle multiple serializer registration calls.
/// </summary>
internal class RepositorySerializationProvider : IBsonSerializationProvider
{
private static volatile RepositorySerializationProvider _instance;
private static readonly object LockObject = new object();
private readonly ConcurrentDictionary<Type, IBsonSerializer> _cache;
private RepositorySerializationProvider()
{
_cache = new ConcurrentDictionary<Type, IBsonSerializer>();
}
public static RepositorySerializationProvider Instance
{
get
{
if (_instance == null)
{
lock (LockObject)
{
if (_instance == null)
{
_instance = new RepositorySerializationProvider();
BsonSerializer.RegisterSerializationProvider(_instance);
}
}
}
return _instance;
}
}
/// <inheritdoc />
public IBsonSerializer GetSerializer(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
return _cache.TryGetValue(type, out var serializer) ? serializer : null;
}
internal void RegisterSerializer<T>(IBsonSerializer<T> serializer) =>
RegisterSerializer(typeof(T), serializer);
internal void RegisterSerializer(Type type, IBsonSerializer serializer) =>
_cache.TryAdd(type, serializer);
}
}
+14 -7
View File
@@ -3,6 +3,9 @@ using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Utils;
using System.Linq;
using System.Reflection;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDbGenericRepository.Internals;
namespace MongoDbGenericRepository
{
@@ -21,7 +24,6 @@ namespace MongoDbGenericRepository
/// </summary>
public IMongoDatabase Database { get; }
/// <summary>
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
/// </summary>
@@ -90,9 +92,14 @@ namespace MongoDbGenericRepository
/// Sets the Guid representation of the MongoDB Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
public virtual void SetGuidRepresentation(GuidRepresentation guidRepresentation)
{
MongoDefaults.GuidRepresentation = guidRepresentation;
// GuidRepresentation and GuidRepresentationMode will be removed in the next major release of the MongoDB Driver.
// We can safely replace this with RepositorySerializationProvider.Instance.RegisterSerializer once we upgrade to the next major release.
#pragma warning disable CS0618
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
RepositorySerializationProvider.Instance.RegisterSerializer(new GuidSerializer(guidRepresentation));
#pragma warning restore CS0618
}
/// <summary>
@@ -103,8 +110,8 @@ namespace MongoDbGenericRepository
protected virtual string GetAttributeCollectionName<TDocument>()
{
return (typeof(TDocument).GetTypeInfo()
.GetCustomAttributes(typeof(CollectionNameAttribute))
.FirstOrDefault() as CollectionNameAttribute)?.Name;
.GetCustomAttributes(typeof(CollectionNameAttribute))
.FirstOrDefault() as CollectionNameAttribute)?.Name;
}
/// <summary>
@@ -114,7 +121,7 @@ namespace MongoDbGenericRepository
protected virtual void InitializeGuidRepresentation()
{
// by default, avoid legacy UUID representation: use Binary 0x04 subtype.
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
SetGuidRepresentation(GuidRepresentation.Standard);
}
/// <summary>
@@ -143,4 +150,4 @@ namespace MongoDbGenericRepository
return (typeof(TDocument).Name.Pluralize()).Camelize();
}
}
}
}
@@ -4,7 +4,7 @@
<TargetFrameworks>net472;netstandard2.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>MongoDbGenericRepository</PackageId>
<PackageVersion>1.5.1</PackageVersion>
<PackageVersion>1.6.0</PackageVersion>
<Authors>Alexandre Spieser</Authors>
<PackageTitle>MongoDb Generic Repository</PackageTitle>
<Description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</Description>
@@ -15,7 +15,7 @@
<Copyright>Copyright 2023 (c) Alexandre Spieser. All rights reserved.</Copyright>
<PackageTags>MongoDb Repository Generic NoSql</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.5.1</Version>
<Version>1.6.0</Version>
<RepositoryUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
</PropertyGroup>
@@ -25,7 +25,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
</ItemGroup>
</Project>