From d3e35cea97798bf8187b685e006c71d655fb7402 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 14 Apr 2025 18:17:35 +0800 Subject: [PATCH 1/3] Upgrade MongoDB.Driver to 3.3.0 and add `AbpCustomGuidMapper`. --- Directory.Packages.props | 2 +- .../Volo/Abp/MongoDB/AbpCustomGuidMapper.cs | 13 +++++++++++++ .../Volo/Abp/MongoDB/AbpMongoDbModule.cs | 11 ++++++++--- .../MongoDB/Repositories/Repository_Basic_Tests.cs | 7 +++++-- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 65e51cbf91..4532f427a9 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -119,7 +119,7 @@ - + diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs new file mode 100644 index 0000000000..58ef742a3d --- /dev/null +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs @@ -0,0 +1,13 @@ +using System; +using MongoDB.Bson; + +namespace Volo.Abp.MongoDB; + +public class AbpCustomGuidMapper : ICustomBsonTypeMapper +{ + public bool TryMapToBsonValue(object value, out BsonValue bsonValue) + { + bsonValue = new BsonBinaryData((Guid)value, GuidRepresentation.Standard); + return true; + } +} diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs index 8e8ad345fc..7563a062f2 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MongoDB.Bson; using MongoDB.Bson.Serialization; @@ -16,6 +17,12 @@ namespace Volo.Abp.MongoDB; [DependsOn(typeof(AbpDddDomainModule))] public class AbpMongoDbModule : AbpModule { + static AbpMongoDbModule() + { + BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); + BsonTypeMapper.RegisterCustomTypeMapper(typeof(Guid), new AbpCustomGuidMapper()); + } + public override void PreConfigureServices(ServiceConfigurationContext context) { context.Services.AddConventionalRegistrar(new AbpMongoDbConventionalRegistrar()); @@ -23,8 +30,6 @@ public class AbpMongoDbModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { - BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); - context.Services.TryAddTransient( typeof(IMongoDbContextProvider<>), typeof(UnitOfWorkMongoDbContextProvider<>) diff --git a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs index e2ad9088cb..ca434e989f 100644 --- a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs +++ b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using MongoDB.Driver.Linq; using Shouldly; +using Volo.Abp.Data; using Volo.Abp.Domain.Repositories; using Volo.Abp.TestApp; using Volo.Abp.TestApp.Domain; @@ -56,6 +57,7 @@ public class Repository_Basic_Tests : Repository_Basic_Tests p.PersonId == person.Id && p.Number == "1234567890").ShouldBeTrue(); + person.GetProperty("test-guid-property").ShouldBe(person.Id); } - + [Fact] public async Task Filter_Case_Insensitive() { (await CityRepository.GetQueryableAsync()).FirstOrDefault(c => c.Name == "ISTANBUL").ShouldBeNull(); (await CityRepository.GetQueryableAsync()).FirstOrDefault(c => c.Name == "istanbul").ShouldBeNull(); (await CityRepository.GetQueryableAsync()).FirstOrDefault(c => c.Name == "Istanbul").ShouldNotBeNull(); - + (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "douglas").ShouldNotBeNull(); (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "DOUGLAS").ShouldNotBeNull(); (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "Douglas").ShouldNotBeNull(); From 507aee72af291ccb4f96e5fe269fe11d71aa5450 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 14 Apr 2025 20:30:40 +0800 Subject: [PATCH 2/3] Refactor InsertAsync test to include nullable GUID property validation --- .../Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs index ca434e989f..3df8699f6d 100644 --- a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs +++ b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs @@ -53,11 +53,12 @@ public class Repository_Basic_Tests : Repository_Basic_Tests p.PersonId == person.Id && p.Number == "1234567890").ShouldBeTrue(); person.GetProperty("test-guid-property").ShouldBe(person.Id); + person.GetProperty("test-nullable-guid-property").ShouldBe(person.Id); } [Fact] From f229f807d83da0ad017e7eaa92e267a32ed205a1 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 14 Apr 2025 20:35:38 +0800 Subject: [PATCH 3/3] Rename AbpCustomGuidMapper to AbpGuidCustomBsonTypeMapper for consistency --- .../Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs | 2 +- .../src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs index 58ef742a3d..4cfacc39fd 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpCustomGuidMapper.cs @@ -3,7 +3,7 @@ using MongoDB.Bson; namespace Volo.Abp.MongoDB; -public class AbpCustomGuidMapper : ICustomBsonTypeMapper +public class AbpGuidCustomBsonTypeMapper : ICustomBsonTypeMapper { public bool TryMapToBsonValue(object value, out BsonValue bsonValue) { diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs index 7563a062f2..195491d989 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs @@ -20,7 +20,7 @@ public class AbpMongoDbModule : AbpModule static AbpMongoDbModule() { BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); - BsonTypeMapper.RegisterCustomTypeMapper(typeof(Guid), new AbpCustomGuidMapper()); + BsonTypeMapper.RegisterCustomTypeMapper(typeof(Guid), new AbpGuidCustomBsonTypeMapper()); } public override void PreConfigureServices(ServiceConfigurationContext context)