Skip to content

Commit

Permalink
Migrate to MongoDB 3.0.0 (#98)
Browse files Browse the repository at this point in the history
Migrated to MongoDB.Driver 3.0.0
Carael authored Nov 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 6864174 commit 1ab6e19
Showing 21 changed files with 184 additions and 77 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<PackageVersion Include="Moq" Version="4.16.1" />
<PackageVersion Include="Snapshooter.Xunit" Version="0.13.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Squadron.Mongo" Version="0.22.0" />
<PackageVersion Include="Squadron.Mongo" Version="0.23.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="Xunit.Priority" Version="1.1.6" />
<PackageVersion Include="FluentAssertions" Version="6.2.0" />
@@ -17,8 +17,8 @@
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="MongoDB.Driver" Version="2.30.0" />
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.5.0" />
<PackageVersion Include="MongoDB.Driver" Version="3.0.0" />
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0'">
29 changes: 16 additions & 13 deletions samples/MongoDB.Extensions.Samples.sln
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SimpleBlog", "SimpleBlog",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Host", "Context\Host\Host.csproj", "{0CCED088-DBB6-4DA2-8DFC-D9968EEBB9FA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration", "Migration\Migration.csproj", "{8226313B-FAC9-4D0F-AEE8-424DD310BBFB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".root", ".root", "{5366F688-8087-42D7-BBB3-3D8E6391AD44}"
ProjectSection(SolutionItems) = preProject
..\Directory.Packages.props = ..\Directory.Packages.props
@@ -44,7 +42,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Session", "..\src\Session\S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prime.Extensions", "..\src\Prime.Extensions\Prime.Extensions.csproj", "{67DC36E5-D3B7-4912-A255-D582B1DEB728}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration", "..\src\Migration\Migration.csproj", "{E5A11398-5B6F-43AF-B4B0-133B0FA9218C}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Migrations", "Migrations", "{82249713-3988-41F8-9D81-CF489E82D526}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration", "Migration\Migration.csproj", "{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration", "..\src\Migration\Migration.csproj", "{9C348CD9-3297-4200-AB59-B0DF19C1D517}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -68,10 +70,6 @@ Global
{0CCED088-DBB6-4DA2-8DFC-D9968EEBB9FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CCED088-DBB6-4DA2-8DFC-D9968EEBB9FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CCED088-DBB6-4DA2-8DFC-D9968EEBB9FA}.Release|Any CPU.Build.0 = Release|Any CPU
{8226313B-FAC9-4D0F-AEE8-424DD310BBFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8226313B-FAC9-4D0F-AEE8-424DD310BBFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8226313B-FAC9-4D0F-AEE8-424DD310BBFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8226313B-FAC9-4D0F-AEE8-424DD310BBFB}.Release|Any CPU.Build.0 = Release|Any CPU
{C3BB5B96-BE90-43F8-BB59-C4610E21152A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3BB5B96-BE90-43F8-BB59-C4610E21152A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3BB5B96-BE90-43F8-BB59-C4610E21152A}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -88,10 +86,14 @@ Global
{67DC36E5-D3B7-4912-A255-D582B1DEB728}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67DC36E5-D3B7-4912-A255-D582B1DEB728}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67DC36E5-D3B7-4912-A255-D582B1DEB728}.Release|Any CPU.Build.0 = Release|Any CPU
{E5A11398-5B6F-43AF-B4B0-133B0FA9218C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5A11398-5B6F-43AF-B4B0-133B0FA9218C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5A11398-5B6F-43AF-B4B0-133B0FA9218C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5A11398-5B6F-43AF-B4B0-133B0FA9218C}.Release|Any CPU.Build.0 = Release|Any CPU
{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79}.Release|Any CPU.Build.0 = Release|Any CPU
{9C348CD9-3297-4200-AB59-B0DF19C1D517}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C348CD9-3297-4200-AB59-B0DF19C1D517}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C348CD9-3297-4200-AB59-B0DF19C1D517}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C348CD9-3297-4200-AB59-B0DF19C1D517}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -105,14 +107,15 @@ Global
{A880C40E-8DAF-4EFF-AE50-B8983AF424D3} = {1B6D9C2E-9D39-49E2-8522-29049C3D4821}
{1B6D9C2E-9D39-49E2-8522-29049C3D4821} = {ED7512B7-0598-4815-9DE5-B3BE0F55C1D2}
{0CCED088-DBB6-4DA2-8DFC-D9968EEBB9FA} = {C4DEA4A3-B50A-4ACD-9017-4919576CE069}
{8226313B-FAC9-4D0F-AEE8-424DD310BBFB} = {ED7512B7-0598-4815-9DE5-B3BE0F55C1D2}
{ED7512B7-0598-4815-9DE5-B3BE0F55C1D2} = {5366F688-8087-42D7-BBB3-3D8E6391AD44}
{97896734-F855-4E7F-8BAF-9099D8ABCC90} = {5366F688-8087-42D7-BBB3-3D8E6391AD44}
{C3BB5B96-BE90-43F8-BB59-C4610E21152A} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
{E4C04570-8EB7-48B6-9099-01BAE4F1155F} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
{ED223FF3-7F6B-46B5-9A87-6694DB555C6C} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
{67DC36E5-D3B7-4912-A255-D582B1DEB728} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
{E5A11398-5B6F-43AF-B4B0-133B0FA9218C} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
{82249713-3988-41F8-9D81-CF489E82D526} = {ED7512B7-0598-4815-9DE5-B3BE0F55C1D2}
{85DA3AFC-784D-4F63-8E1F-B4D2D76A2A79} = {82249713-3988-41F8-9D81-CF489E82D526}
{9C348CD9-3297-4200-AB59-B0DF19C1D517} = {97896734-F855-4E7F-8BAF-9099D8ABCC90}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E28A6DDE-2575-483D-8C8B-7929357BA44A}
4 changes: 2 additions & 2 deletions src/Context.InterferingTests/MongoDatabaseBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -43,13 +43,13 @@ public void ConfigureConnection_SetSpecificConnectionSettings_MongoConnectionSet
// Act
mongoDatabaseBuilder
.ConfigureConnection(settings => settings.ApplicationName = "Test")
.ConfigureConnection(settings => settings.ConnectionMode = ConnectionMode.Direct)
.ConfigureConnection(settings => settings.DirectConnection = true)
.ConfigureConnection(settings => settings.WriteConcern = WriteConcern.W3);
MongoDbContextData result = mongoDatabaseBuilder.Build();

// Assert
Assert.Equal("Test", result.Client.Settings.ApplicationName);
Assert.Equal(ConnectionMode.Direct, result.Client.Settings.ConnectionMode);
Assert.True(result.Client.Settings.DirectConnection);
Assert.Equal(WriteConcern.W3, result.Client.Settings.WriteConcern);
}

Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
"CollectionName": "TheArtOfFoo",
"Settings": {
"AssignIdOnInsert": true,
"GuidRepresentation": "CSharpLegacy",
"IsFrozen": true,
"ReadConcern": {
"IsServerDefault": false,
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Extensions.Context.Extensions;

#nullable enable

namespace MongoDB.Extensions.Context.Internal;

public class TypeObjectSerializer : ObjectSerializer
internal class TypeObjectSerializer : ClassSerializerBase<object>, IHasDiscriminatorConvention
{
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new ();
private static readonly HashSet<string> _allowedTypesByNamespaces = new ();
private static readonly HashSet<string> _allowedTypesByDependencies = new ();
private readonly ObjectSerializer _objectSerializer;
private static readonly ConcurrentDictionary<Type, bool> _allowedTypes = new();
private static readonly HashSet<string> _allowedTypesByNamespaces = new();
private static readonly HashSet<string> _allowedTypesByDependencies = new();

public TypeObjectSerializer() : base(type => IsTypeAllowed(type))
public TypeObjectSerializer()
{
_objectSerializer = new ObjectSerializer(type => IsTypeAllowed(type));
DiscriminatorConvention = _objectSerializer.GetDiscriminatorConvention();
}

public static IReadOnlyDictionary<Type, bool> AllowedTypes
@@ -28,10 +34,10 @@ public static IReadOnlyCollection<string> AllowedTypesByDependencies

public static bool IsTypeAllowed(Type type)
{
return DefaultAllowedTypes(type) ||
_allowedTypes.ContainsKey(type) ||
IsInAllowedNamespaces(type) ||
IsInAllowedDependencyTypes(type);
return ObjectSerializer.DefaultAllowedTypes.Invoke(type) ||
_allowedTypes.ContainsKey(type) ||
IsInAllowedNamespaces(type) ||
IsInAllowedDependencyTypes(type);
}

public static void AddAllowedType<T>()
@@ -89,13 +95,13 @@ private static bool IsAllowedNameSpacePart(Type type)
{
foreach (string allowedNamespace in _allowedTypesByNamespaces)
{
if(string.IsNullOrEmpty(type.Namespace))
if (string.IsNullOrEmpty(type.Namespace))
{
return false;
}

if (type.Namespace.StartsWith(allowedNamespace,
StringComparison.OrdinalIgnoreCase))
StringComparison.OrdinalIgnoreCase))
{
return true;
}
@@ -113,4 +119,26 @@ private static bool IsInAllowedDependencyTypes(Type type)

return isInDependencyTypes;
}

public IDiscriminatorConvention DiscriminatorConvention { get; }

public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
return _objectSerializer.Deserialize(context, args);
}

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
_objectSerializer.Serialize(context, args, value);
}

public override bool Equals(object? obj)
{
return _objectSerializer.Equals(obj);
}

public override int GetHashCode()
{
return _objectSerializer.GetHashCode();
}
}
34 changes: 29 additions & 5 deletions src/Migration/MigrationSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;

namespace MongoDB.Extensions.Migration;

class MigrationSerializer<T> : BsonClassMapSerializer<T> where T : IVersioned
class MigrationSerializer<T> : SerializerBase<T>, IBsonIdProvider, IBsonDocumentSerializer, IBsonPolymorphicSerializer,
IHasDiscriminatorConvention where T : IVersioned
{
private readonly EntityContext _context;
private readonly MigrationRunner<T> _migrationRunner;
private readonly BsonClassMapSerializer<T> _bsonClassMapSerializer;

public MigrationSerializer(EntityContext context) : base(BsonClassMap.LookupClassMap(typeof(T)))
public MigrationSerializer(EntityContext context)
{
_bsonClassMapSerializer = new BsonClassMapSerializer<T>(BsonClassMap.LookupClassMap(typeof(T)));
_context = context;
_migrationRunner = new MigrationRunner<T>(context);
}


public override void Serialize(
BsonSerializationContext context,
BsonSerializationArgs args,
T value)
{
value.Version = _context.Option.CurrentVersion;
base.Serialize(context, args, value);
_bsonClassMapSerializer.Serialize(context, args, value);
}

public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
@@ -35,10 +39,30 @@ public override T Deserialize(BsonDeserializationContext context, BsonDeserializ
var migratedContext =
BsonDeserializationContext.CreateRoot(new BsonDocumentReader(bsonDocument));

T entity = base.Deserialize(migratedContext, args);
T entity = _bsonClassMapSerializer.Deserialize(migratedContext, args);

entity.Version = _context.Option.CurrentVersion;

return entity;
}

public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
{
return _bsonClassMapSerializer.TryGetMemberSerializationInfo(memberName, out serializationInfo);
}

public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator)
{
return _bsonClassMapSerializer.GetDocumentId(document, out id, out idNominalType, out idGenerator);
}

public void SetDocumentId(object document, object id)
{
_bsonClassMapSerializer.SetDocumentId(document, id);
}

public bool IsDiscriminatorCompatibleWithObjectSerializer =>
_bsonClassMapSerializer.IsDiscriminatorCompatibleWithObjectSerializer;

public IDiscriminatorConvention DiscriminatorConvention => _bsonClassMapSerializer.DiscriminatorConvention;
}
24 changes: 12 additions & 12 deletions src/MongoDB.Extensions.sln
Original file line number Diff line number Diff line change
@@ -21,10 +21,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Transactions.Tests", "Trans
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Context.InterferingTests", "Context.InterferingTests\Context.InterferingTests.csproj", "{6655AD69-6FD7-4AE2-BF09-53152911D7BE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration", "Migration\Migration.csproj", "{696254EA-91B9-4809-9D7F-EDD731C74622}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration.Tests", "Migration.Tests\Migration.Tests.csproj", "{A7D17D8A-99BC-40AC-92B4-996790F7F26E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionFiles", ".SolutionFiles", "{A21A1FF3-25C1-4FE1-994E-E16CDF98BB99}"
ProjectSection(SolutionItems) = preProject
Dependencies.props = Dependencies.props
@@ -39,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionFiles", ".Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Context.AllowedTypes.Tests", "Context.AllowedTypes.Tests\Context.AllowedTypes.Tests.csproj", "{C59B2068-C3F4-4900-A1CB-7BFD63F94095}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration", "Migration\Migration.csproj", "{07804939-0794-47A5-8DD4-832394E90CA6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Migration.Tests", "Migration.Tests\Migration.Tests.csproj", "{D8F246AC-65CC-4EF0-B058-08970D269B61}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -81,18 +81,18 @@ Global
{6655AD69-6FD7-4AE2-BF09-53152911D7BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6655AD69-6FD7-4AE2-BF09-53152911D7BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6655AD69-6FD7-4AE2-BF09-53152911D7BE}.Release|Any CPU.Build.0 = Release|Any CPU
{696254EA-91B9-4809-9D7F-EDD731C74622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{696254EA-91B9-4809-9D7F-EDD731C74622}.Debug|Any CPU.Build.0 = Debug|Any CPU
{696254EA-91B9-4809-9D7F-EDD731C74622}.Release|Any CPU.ActiveCfg = Release|Any CPU
{696254EA-91B9-4809-9D7F-EDD731C74622}.Release|Any CPU.Build.0 = Release|Any CPU
{A7D17D8A-99BC-40AC-92B4-996790F7F26E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7D17D8A-99BC-40AC-92B4-996790F7F26E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7D17D8A-99BC-40AC-92B4-996790F7F26E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7D17D8A-99BC-40AC-92B4-996790F7F26E}.Release|Any CPU.Build.0 = Release|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C59B2068-C3F4-4900-A1CB-7BFD63F94095}.Release|Any CPU.Build.0 = Release|Any CPU
{07804939-0794-47A5-8DD4-832394E90CA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07804939-0794-47A5-8DD4-832394E90CA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07804939-0794-47A5-8DD4-832394E90CA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07804939-0794-47A5-8DD4-832394E90CA6}.Release|Any CPU.Build.0 = Release|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8F246AC-65CC-4EF0-B058-08970D269B61}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Snapshooter.Xunit;
using Squadron;
@@ -187,6 +189,7 @@ public Foo(Guid id, string name)
Name = name;
}

[BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; }

public string Name { get; }
3 changes: 3 additions & 0 deletions src/Prime.Extensions.Tests/Helpers/Bar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace MongoDB.Prime.Extensions.Tests;

@@ -11,6 +13,7 @@ public Bar(Guid id, string name, string value)
Value = value;
}

[BsonGuidRepresentation(GuidRepresentation.CSharpLegacy)]
public Guid Id { get; }

public string Name { get; }
2 changes: 2 additions & 0 deletions src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Snapshooter.Xunit;
using Squadron;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$set" : {
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd")
"_id" : HexData(3, "91217544-0be1-5a43-a0e9-62e7f13d41cd")
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "$or" : [{ "$and" : [{ "_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") }, { "Name" : "Walse" }, { "Name" : "Toronto" }] }] }
{ "$or" : [{ "$and" : [{ "_id" : HexData(3, "91217544-0be1-5a43-a0e9-62e7f13d41cd") }, { "Name" : "Walse" }, { "Name" : "Toronto" }] }] }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$or" : [{
"$and" : [{
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd")
"_id" : HexData(3, "91217544-0be1-5a43-a0e9-62e7f13d41cd")
}, {
"Name" : "Walse"
}, {
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$set" : {
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd")
"_id" : HexData(3, "91217544-0be1-5a43-a0e9-62e7f13d41cd")
}
}
4 changes: 2 additions & 2 deletions src/Prime.Extensions/FilterDefinitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ public static BsonDocument ToBsonDocument<T>(
IBsonSerializerRegistry serializerRegistry =
BsonSerializer.SerializerRegistry;

return filter.Render(serializerRegistry
.GetSerializer<T>(), serializerRegistry);
return filter.Render(new RenderArgs<T>(serializerRegistry
.GetSerializer<T>(), serializerRegistry));
}
}
}
39 changes: 18 additions & 21 deletions src/Prime.Extensions/MongoCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -112,34 +112,31 @@ public static Task InsertManyAsync<TDocument>(
return collection.InsertManyAsync(
documents, options, cancellationToken);
}

public static string? Explain<T>(
this IMongoCollection<T> collection,
FilterDefinition<T> filter)
FilterDefinition<T> filter,
FindOptions? findOptions = null)
{
var options = new FindOptions
var command = new BsonDocument
{
Modifiers = new BsonDocument("$explain", true)
{
"explain",
new BsonDocument
{
{ "find", collection.CollectionNamespace.CollectionName },
{ "filter", filter.ToBsonDocument() }
}
}
};

return Explain(collection, filter, options);
}

public static string? Explain<T>(
this IMongoCollection<T> collection,
FilterDefinition<T> filter,
FindOptions findOptions)
{
findOptions.Modifiers =
new BsonDocument("$explain", true);

string? explain = collection
.Find(filter, findOptions)
.Project(new BsonDocument())
.FirstOrDefault()
?.ToJson();
if (findOptions?.Collation != null)
{
command["explain"]["collation"] = findOptions.Collation.ToBsonDocument();
}

return explain;
var result = collection.Database.RunCommand<BsonDocument>(command);
return result?.ToJson();
}
}
}
4 changes: 2 additions & 2 deletions src/Prime.Extensions/UpdateDefinitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ public static BsonValue ToBsonValue<T>(
IBsonSerializerRegistry serializerRegistry =
BsonSerializer.SerializerRegistry;

return filter.Render(serializerRegistry
.GetSerializer<T>(), serializerRegistry);
return filter.Render(new RenderArgs<T>(serializerRegistry
.GetSerializer<T>(), serializerRegistry));
}
}
}
1 change: 1 addition & 0 deletions src/Session.Tests/MongoServerExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -97,6 +97,7 @@ internal sealed class ServerSessionId
{
[BsonId]
[BsonElement("id")]
[BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; set; }

public static implicit operator Guid(ServerSessionId serverSessionId)
1 change: 1 addition & 0 deletions src/Transactions.Tests/TransactionCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ public class TransactionCollectionTests : IClassFixture<MongoReplicaSetResource>

public TransactionCollectionTests(MongoReplicaSetResource mongoResource)
{

_mongoResource = mongoResource;
}

3 changes: 3 additions & 0 deletions src/Transactions.Tests/User.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace MongoDB.Extensions.Transactions.Tests
{
@@ -10,6 +12,7 @@ public User(Guid id, string name)
Name = name;
}

[BsonGuidRepresentation(GuidRepresentation.CSharpLegacy)]
public Guid Id { get; }

public string Name { get; }
45 changes: 44 additions & 1 deletion src/Transactions/MongoTransactionClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
@@ -24,6 +25,42 @@
_clientSessionHandle = clientSessionHandle;
}

public ClientBulkWriteResult BulkWrite(
IReadOnlyList<BulkWriteModel> models,
ClientBulkWriteOptions? options = null,
CancellationToken cancellationToken = new())
{
if (TryGetSession(out IClientSessionHandle? session))
{
return _client.BulkWrite(session, models, options, cancellationToken);
}

return _client.BulkWrite(models, options, cancellationToken);
}

public ClientBulkWriteResult BulkWrite(IClientSessionHandle session, IReadOnlyList<BulkWriteModel> models,
ClientBulkWriteOptions options = null, CancellationToken cancellationToken = new CancellationToken())

Check warning on line 42 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 42 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 42 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.
{
return _client.BulkWrite(session, models, options, cancellationToken);
}

public Task<ClientBulkWriteResult> BulkWriteAsync(IReadOnlyList<BulkWriteModel> models, ClientBulkWriteOptions options = null,

Check warning on line 47 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 47 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 47 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.
CancellationToken cancellationToken = new CancellationToken())
{
if (TryGetSession(out IClientSessionHandle? session))
{
return _client.BulkWriteAsync(session, models, options, cancellationToken);
}

return _client.BulkWriteAsync(models, options, cancellationToken);
}

public Task<ClientBulkWriteResult> BulkWriteAsync(IClientSessionHandle session, IReadOnlyList<BulkWriteModel> models, ClientBulkWriteOptions options = null,

Check warning on line 58 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 58 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 58 in src/Transactions/MongoTransactionClient.cs

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.
CancellationToken cancellationToken = new CancellationToken())
{
return _client.BulkWriteAsync(session, models, options, cancellationToken);
}

public void DropDatabase(
string name,
CancellationToken cancellationToken = default)
@@ -303,5 +340,11 @@

return TransactionStore.TryGetSession(
_client, out sessionHandle);
}
}

public void Dispose()
{
_clientSessionHandle?.Dispose();
_client.Dispose();
}
}

0 comments on commit 1ab6e19

Please sign in to comment.