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
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" 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.runner.console" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@@ -531,8 +531,8 @@ public class UpdateManyAsyncTests : GenericTestContext<MongoDbUpdater>
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
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<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
|
||||
@@ -530,8 +530,8 @@ public class UpdateManyTests : GenericTestContext<MongoDbUpdater>
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
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<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
|
||||
@@ -755,8 +755,8 @@ public class UpdateOneAsyncTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var count = Fixture.Create<long>();
|
||||
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<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
|
||||
@@ -754,8 +754,8 @@ public class UpdateOneTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var count = Fixture.Create<long>();
|
||||
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<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<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>
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
|
||||
@@ -34,6 +34,12 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<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">
|
||||
<HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -49,17 +55,17 @@
|
||||
<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>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Bson, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Bson.2.18.0\lib\net472\MongoDB.Bson.dll</HintPath>
|
||||
<Reference Include="MongoDB.Bson, Version=2.20.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Bson.2.20.0\lib\net472\MongoDB.Bson.dll</HintPath>
|
||||
</Reference>
|
||||
<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>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Driver.Core, Version=2.18.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Driver.Core.2.18.0\lib\net472\MongoDB.Driver.Core.dll</HintPath>
|
||||
<Reference Include="MongoDB.Driver.Core, Version=2.20.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Driver.Core.2.20.0\lib\net472\MongoDB.Driver.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Libmongocrypt, Version=1.6.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Libmongocrypt.1.6.0\lib\net472\MongoDB.Libmongocrypt.dll</HintPath>
|
||||
<Reference Include="MongoDB.Libmongocrypt, Version=1.8.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Libmongocrypt.1.8.0\lib\net472\MongoDB.Libmongocrypt.dll</HintPath>
|
||||
</Reference>
|
||||
<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>
|
||||
@@ -160,8 +166,6 @@
|
||||
<None Include="App.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<None Include="libmongocrypt.dylib" />
|
||||
<None Include="libmongocrypt.so" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -174,6 +178,11 @@
|
||||
</ProjectReference>
|
||||
</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" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
@@ -183,7 +192,7 @@
|
||||
</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\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>
|
||||
<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>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,14 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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="DnsClient" version="1.6.1" 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.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.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.ConsoleRunner" version="3.12.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);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user