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
+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();
}
}
}
}