Compare commits

..

2 Commits

Author SHA1 Message Date
Alexandre SPIESER 7bd356c7da Merge pull request #56 from mrtaikandi/mongodriver-2.20
Upgrade to MongoDb Driver 2.20 and use GuidRepresentationMode.V3 as the default representation.
This potentially can cause a breaking change to the package consumer, but the behaviour can be set to V2 by overriding the void SetGuidRepresentation(GuidRepresentation guidRepresentation) method.
GuidRepresentation and GuidRepresentationMode will be removed in the next major release of the MongoDB Driver:
https://www.mongodb.com/community/forums/t/c-guid-style-dont-work/126901/2
What's new in 2.20 and 2.19:
https://www.mongodb.com/docs/drivers/csharp/current/whats-new/#std-label-csharp-whats-new
2023-08-11 11:07:13 +01:00
Mohammadreza Taikandi f59cccb3d9 Upgrade to MongoDb Driver 2.20 and use GuidRepresentationMode.V3 as default representation. 2023-08-07 11:34:35 +01:00
14 changed files with 110 additions and 33 deletions
@@ -7,7 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" /> <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" /> <PackageReference Include="MongoDB.Driver" Version="2.20.0" />
<PackageReference Include="xunit" Version="2.5.0" /> <PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.console" Version="2.5.0"> <PackageReference Include="xunit.runner.console" Version="2.5.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@@ -531,8 +531,8 @@ public class UpdateManyAsyncTests : GenericTestContext<MongoDbUpdater>
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null) private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
{ {
var replacedId = Fixture.Create<Guid>(); var replacedId = Fixture.Create<Guid>();
var replaceResult = new ReplaceOneResult.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, BsonValue.Create(replacedId)); var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
var collection = MockOf<IMongoCollection<TestDocument>>(); var collection = MockOf<IMongoCollection<TestDocument>>();
collection collection
@@ -530,8 +530,8 @@ public class UpdateManyTests : GenericTestContext<MongoDbUpdater>
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null) private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
{ {
var replacedId = Fixture.Create<Guid>(); var replacedId = Fixture.Create<Guid>();
var replaceResult = new ReplaceOneResult.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, BsonValue.Create(replacedId)); var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
var collection = MockOf<IMongoCollection<TestDocument>>(); var collection = MockOf<IMongoCollection<TestDocument>>();
collection collection
@@ -755,8 +755,8 @@ public class UpdateOneAsyncTests : GenericTestContext<MongoDbUpdater>
{ {
var replacedId = Fixture.Create<Guid>(); var replacedId = Fixture.Create<Guid>();
var count = Fixture.Create<long>(); var count = Fixture.Create<long>();
var replaceResult = new ReplaceOneResult.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, BsonValue.Create(replacedId)); var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
var collection = MockOf<IMongoCollection<TestDocument>>(); var collection = MockOf<IMongoCollection<TestDocument>>();
collection collection
@@ -754,8 +754,8 @@ public class UpdateOneTests : GenericTestContext<MongoDbUpdater>
{ {
var replacedId = Fixture.Create<Guid>(); var replacedId = Fixture.Create<Guid>();
var count = Fixture.Create<long>(); var count = Fixture.Create<long>();
var replaceResult = new ReplaceOneResult.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, BsonValue.Create(replacedId)); var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
var collection = MockOf<IMongoCollection<TestDocument>>(); var collection = MockOf<IMongoCollection<TestDocument>>();
collection collection
+1 -1
View File
@@ -11,7 +11,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" /> <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly> </dependentAssembly>
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
+19 -10
View File
@@ -34,6 +34,12 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.7.100.14\lib\net45\AWSSDK.Core.dll</HintPath>
</Reference>
<Reference Include="AWSSDK.SecurityToken, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.SecurityToken.3.7.100.14\lib\net45\AWSSDK.SecurityToken.dll</HintPath>
</Reference>
<Reference Include="Crc32C.NET, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Crc32C.NET, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath> <HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath>
</Reference> </Reference>
@@ -49,17 +55,17 @@
<Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath> <HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Bson, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Bson, Version=2.20.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Bson.2.18.0\lib\net472\MongoDB.Bson.dll</HintPath> <HintPath>..\packages\MongoDB.Bson.2.20.0\lib\net472\MongoDB.Bson.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Driver, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Driver, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Driver.2.18.0\lib\net472\MongoDB.Driver.dll</HintPath> <HintPath>..\packages\MongoDB.Driver.2.18.0\lib\net472\MongoDB.Driver.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Driver.Core, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Driver.Core, Version=2.20.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Driver.Core.2.18.0\lib\net472\MongoDB.Driver.Core.dll</HintPath> <HintPath>..\packages\MongoDB.Driver.Core.2.20.0\lib\net472\MongoDB.Driver.Core.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Libmongocrypt, Version=1.6.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Libmongocrypt, Version=1.8.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Libmongocrypt.1.6.0\lib\net472\MongoDB.Libmongocrypt.dll</HintPath> <HintPath>..\packages\MongoDB.Libmongocrypt.1.8.0\lib\net472\MongoDB.Libmongocrypt.dll</HintPath>
</Reference> </Reference>
<Reference Include="nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> <Reference Include="nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.13.2\lib\net45\nunit.framework.dll</HintPath> <HintPath>..\packages\NUnit.3.13.2\lib\net45\nunit.framework.dll</HintPath>
@@ -160,8 +166,6 @@
<None Include="App.config"> <None Include="App.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>
<None Include="libmongocrypt.dylib" />
<None Include="libmongocrypt.so" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -174,6 +178,11 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Analyzer Include="..\packages\AWSSDK.SecurityToken.3.7.100.14\analyzers\dotnet\cs\AWSSDK.SecurityToken.CodeAnalysis.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="libmongocrypt.dylib" />
<Content Include="libmongocrypt.so" />
<Content Include="mongocrypt.dll" /> <Content Include="mongocrypt.dll" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -183,7 +192,7 @@
</PropertyGroup> </PropertyGroup>
<Error Condition="!Exists('..\packages\NUnit.3.13.2\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.2\build\NUnit.props'))" /> <Error Condition="!Exists('..\packages\NUnit.3.13.2\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.2\build\NUnit.props'))" />
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.4.0.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.4.0.0\build\net35\NUnit3TestAdapter.props'))" /> <Error Condition="!Exists('..\packages\NUnit3TestAdapter.4.0.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.4.0.0\build\net35\NUnit3TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MongoDB.Libmongocrypt.1.6.0\build\MongoDB.Libmongocrypt.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MongoDB.Libmongocrypt.1.6.0\build\MongoDB.Libmongocrypt.targets'))" /> <Error Condition="!Exists('..\packages\MongoDB.Libmongocrypt.1.8.0\build\MongoDB.Libmongocrypt.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MongoDB.Libmongocrypt.1.8.0\build\MongoDB.Libmongocrypt.targets'))" />
</Target> </Target>
<Import Project="..\packages\MongoDB.Libmongocrypt.1.6.0\build\MongoDB.Libmongocrypt.targets" Condition="Exists('..\packages\MongoDB.Libmongocrypt.1.6.0\build\MongoDB.Libmongocrypt.targets')" /> <Import Project="..\packages\MongoDB.Libmongocrypt.1.8.0\build\MongoDB.Libmongocrypt.targets" Condition="Exists('..\packages\MongoDB.Libmongocrypt.1.8.0\build\MongoDB.Libmongocrypt.targets')" />
</Project> </Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5 -3
View File
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="MongoDB.Driver.Core" version="2.20.0" targetFramework="net48" />
<package id="MongoDB.Libmongocrypt" version="1.8.0" targetFramework="net48" />
<package id="AWSSDK.Core" version="3.7.100.14" targetFramework="net48" />
<package id="AWSSDK.SecurityToken" version="3.7.100.14" targetFramework="net48" />
<package id="Crc32C.NET" version="1.0.5.0" targetFramework="net461" /> <package id="Crc32C.NET" version="1.0.5.0" targetFramework="net461" />
<package id="DnsClient" version="1.6.1" targetFramework="net48" /> <package id="DnsClient" version="1.6.1" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" targetFramework="net48" /> <package id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.0.0" targetFramework="net48" /> <package id="Microsoft.Extensions.Logging.Abstractions" version="2.0.0" targetFramework="net48" />
<package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net48" /> <package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net48" />
<package id="MongoDB.Bson" version="2.18.0" targetFramework="net48" /> <package id="MongoDB.Bson" version="2.20.0" targetFramework="net48" />
<package id="MongoDB.Driver" version="2.18.0" targetFramework="net48" /> <package id="MongoDB.Driver" version="2.18.0" targetFramework="net48" />
<package id="MongoDB.Driver.Core" version="2.18.0" targetFramework="net48" />
<package id="MongoDB.Libmongocrypt" version="1.6.0" targetFramework="net48" />
<package id="NUnit" version="3.13.2" targetFramework="net461" /> <package id="NUnit" version="3.13.2" targetFramework="net461" />
<package id="NUnit.ConsoleRunner" version="3.12.0" targetFramework="net461" /> <package id="NUnit.ConsoleRunner" version="3.12.0" targetFramework="net461" />
<package id="NUnit3TestAdapter" version="4.0.0" targetFramework="net461" /> <package id="NUnit3TestAdapter" version="4.0.0" targetFramework="net461" />
@@ -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);
}
}
+11 -4
View File
@@ -3,6 +3,9 @@ using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Utils; using MongoDbGenericRepository.Utils;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDbGenericRepository.Internals;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -21,7 +24,6 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
public IMongoDatabase Database { get; } public IMongoDatabase Database { get; }
/// <summary> /// <summary>
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>. /// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
/// </summary> /// </summary>
@@ -90,9 +92,14 @@ namespace MongoDbGenericRepository
/// Sets the Guid representation of the MongoDB Driver. /// Sets the Guid representation of the MongoDB Driver.
/// </summary> /// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param> /// <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> /// <summary>
@@ -114,7 +121,7 @@ namespace MongoDbGenericRepository
protected virtual void InitializeGuidRepresentation() protected virtual void InitializeGuidRepresentation()
{ {
// by default, avoid legacy UUID representation: use Binary 0x04 subtype. // by default, avoid legacy UUID representation: use Binary 0x04 subtype.
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard; SetGuidRepresentation(GuidRepresentation.Standard);
} }
/// <summary> /// <summary>
@@ -4,7 +4,7 @@
<TargetFrameworks>net472;netstandard2.0;</TargetFrameworks> <TargetFrameworks>net472;netstandard2.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>MongoDbGenericRepository</PackageId> <PackageId>MongoDbGenericRepository</PackageId>
<PackageVersion>1.5.1</PackageVersion> <PackageVersion>1.6.0</PackageVersion>
<Authors>Alexandre Spieser</Authors> <Authors>Alexandre Spieser</Authors>
<PackageTitle>MongoDb Generic Repository</PackageTitle> <PackageTitle>MongoDb Generic Repository</PackageTitle>
<Description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</Description> <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> <Copyright>Copyright 2023 (c) Alexandre Spieser. All rights reserved.</Copyright>
<PackageTags>MongoDb Repository Generic NoSql</PackageTags> <PackageTags>MongoDb Repository Generic NoSql</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.5.1</Version> <Version>1.6.0</Version>
<RepositoryUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</RepositoryUrl> <RepositoryUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</RepositoryUrl>
<RepositoryType>Git</RepositoryType> <RepositoryType>Git</RepositoryType>
</PropertyGroup> </PropertyGroup>
@@ -25,7 +25,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.18.0" /> <PackageReference Include="MongoDB.Driver" Version="2.20.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>