diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 37b4dd3..dd8e28a 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -7,7 +7,7 @@ - + all diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs index f0322d1..7a9a30f 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs @@ -531,8 +531,8 @@ public class UpdateManyAsyncTests : GenericTestContext private Mock> SetupCollection(long count, string partitionKey = null) { var replacedId = Fixture.Create(); - var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId)); - var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + var replaceResult = new ReplaceOneResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); + var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); var collection = MockOf>(); collection diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs index 1b87b86..8da054a 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs @@ -530,8 +530,8 @@ public class UpdateManyTests : GenericTestContext private Mock> SetupCollection(long count, string partitionKey = null) { var replacedId = Fixture.Create(); - var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId)); - var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + var replaceResult = new ReplaceOneResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); + var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); var collection = MockOf>(); collection diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs index 5149e07..0217313 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs @@ -755,8 +755,8 @@ public class UpdateOneAsyncTests : GenericTestContext { var replacedId = Fixture.Create(); var count = Fixture.Create(); - var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); - var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); + var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); var collection = MockOf>(); collection diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs index e73728c..ae7f409 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs @@ -754,8 +754,8 @@ public class UpdateOneTests : GenericTestContext { var replacedId = Fixture.Create(); var count = Fixture.Create(); - var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); - var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); + var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard)); var collection = MockOf>(); collection diff --git a/IntegrationTests/App.config b/IntegrationTests/App.config index 148a2b5..04daefa 100644 --- a/IntegrationTests/App.config +++ b/IntegrationTests/App.config @@ -11,7 +11,7 @@ - + diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj index 6c6da8b..3823787 100644 --- a/IntegrationTests/IntegrationTests.csproj +++ b/IntegrationTests/IntegrationTests.csproj @@ -34,6 +34,12 @@ 4 + + ..\packages\AWSSDK.Core.3.7.100.14\lib\net45\AWSSDK.Core.dll + + + ..\packages\AWSSDK.SecurityToken.3.7.100.14\lib\net45\AWSSDK.SecurityToken.dll + ..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll @@ -49,17 +55,17 @@ ..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll - - ..\packages\MongoDB.Bson.2.18.0\lib\net472\MongoDB.Bson.dll + + ..\packages\MongoDB.Bson.2.20.0\lib\net472\MongoDB.Bson.dll ..\packages\MongoDB.Driver.2.18.0\lib\net472\MongoDB.Driver.dll - - ..\packages\MongoDB.Driver.Core.2.18.0\lib\net472\MongoDB.Driver.Core.dll + + ..\packages\MongoDB.Driver.Core.2.20.0\lib\net472\MongoDB.Driver.Core.dll - - ..\packages\MongoDB.Libmongocrypt.1.6.0\lib\net472\MongoDB.Libmongocrypt.dll + + ..\packages\MongoDB.Libmongocrypt.1.8.0\lib\net472\MongoDB.Libmongocrypt.dll ..\packages\NUnit.3.13.2\lib\net45\nunit.framework.dll @@ -160,8 +166,6 @@ Designer - - @@ -174,6 +178,11 @@ + + + + + @@ -183,7 +192,7 @@ - + - + \ No newline at end of file diff --git a/IntegrationTests/libmongocrypt.dylib b/IntegrationTests/libmongocrypt.dylib index d17acad..c667f0a 100644 Binary files a/IntegrationTests/libmongocrypt.dylib and b/IntegrationTests/libmongocrypt.dylib differ diff --git a/IntegrationTests/libmongocrypt.so b/IntegrationTests/libmongocrypt.so index c4c55c6..1f10d71 100644 Binary files a/IntegrationTests/libmongocrypt.so and b/IntegrationTests/libmongocrypt.so differ diff --git a/IntegrationTests/mongocrypt.dll b/IntegrationTests/mongocrypt.dll index bde3c75..d67a0c0 100644 Binary files a/IntegrationTests/mongocrypt.dll and b/IntegrationTests/mongocrypt.dll differ diff --git a/IntegrationTests/packages.config b/IntegrationTests/packages.config index c2d7c84..d7b6a43 100644 --- a/IntegrationTests/packages.config +++ b/IntegrationTests/packages.config @@ -1,14 +1,16 @@  + + + + - + - - diff --git a/MongoDbGenericRepository/Internals/RepositorySerializationProvider.cs b/MongoDbGenericRepository/Internals/RepositorySerializationProvider.cs new file mode 100644 index 0000000..d9f284a --- /dev/null +++ b/MongoDbGenericRepository/Internals/RepositorySerializationProvider.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Concurrent; +using MongoDB.Bson.Serialization; + +namespace MongoDbGenericRepository.Internals +{ + /// + /// An that can handle multiple serializer registration calls. + /// + internal class RepositorySerializationProvider : IBsonSerializationProvider + { + private static volatile RepositorySerializationProvider _instance; + private static readonly object LockObject = new object(); + + private readonly ConcurrentDictionary _cache; + + private RepositorySerializationProvider() + { + _cache = new ConcurrentDictionary(); + } + + public static RepositorySerializationProvider Instance + { + get + { + if (_instance == null) + { + lock (LockObject) + { + if (_instance == null) + { + _instance = new RepositorySerializationProvider(); + BsonSerializer.RegisterSerializationProvider(_instance); + } + } + } + + return _instance; + } + } + + /// + 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(IBsonSerializer serializer) => + RegisterSerializer(typeof(T), serializer); + + internal void RegisterSerializer(Type type, IBsonSerializer serializer) => + _cache.TryAdd(type, serializer); + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs index a21dc6c..2eae9ee 100644 --- a/MongoDbGenericRepository/MongoDbContext.cs +++ b/MongoDbGenericRepository/MongoDbContext.cs @@ -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 /// public IMongoDatabase Database { get; } - /// /// The constructor of the MongoDbContext, it needs an object implementing . /// @@ -90,9 +92,14 @@ namespace MongoDbGenericRepository /// Sets the Guid representation of the MongoDB Driver. /// /// The new value of the GuidRepresentation - 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 } /// @@ -103,8 +110,8 @@ namespace MongoDbGenericRepository protected virtual string GetAttributeCollectionName() { return (typeof(TDocument).GetTypeInfo() - .GetCustomAttributes(typeof(CollectionNameAttribute)) - .FirstOrDefault() as CollectionNameAttribute)?.Name; + .GetCustomAttributes(typeof(CollectionNameAttribute)) + .FirstOrDefault() as CollectionNameAttribute)?.Name; } /// @@ -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); } /// @@ -143,4 +150,4 @@ namespace MongoDbGenericRepository return (typeof(TDocument).Name.Pluralize()).Camelize(); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index da1cee7..ca64c90 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -4,7 +4,7 @@ net472;netstandard2.0; true MongoDbGenericRepository - 1.5.1 + 1.6.0 Alexandre Spieser MongoDb Generic Repository A generic repository implementation using the MongoDB C# Sharp 2.0 driver. @@ -15,7 +15,7 @@ Copyright 2023 (c) Alexandre Spieser. All rights reserved. MongoDb Repository Generic NoSql true - 1.5.1 + 1.6.0 https://github.com/alexandre-spieser/mongodb-generic-repository Git @@ -25,7 +25,7 @@ - +