Browse Source

Add an options to disable the `AbpMongoDbDateTimeSerializer`.

pull/10149/head
maliming 5 years ago
parent
commit
2eddc9aa2b
  1. 18
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbOptions.cs
  2. 19
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs
  3. 41
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Serializer/MongoDB_DateTimeKind_Tests.cs

18
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbOptions.cs

@ -0,0 +1,18 @@
using Volo.Abp.Timing;
namespace Volo.Abp.MongoDB
{
public class AbpMongoDbOptions
{
/// <summary>
/// Serializer the datetime based on <see cref="AbpClockOptions.Kind"/> in MongoDb.
/// Default: true.
/// </summary>
public bool UseAbpClockHandleDateTime { get; set; }
public AbpMongoDbOptions()
{
UseAbpClockHandleDateTime = true;
}
}
}

19
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs

@ -28,7 +28,7 @@ namespace Volo.Abp.MongoDB
{ {
lock (SyncObj) lock (SyncObj)
{ {
var clockOptions = dbContext.LazyServiceProvider?.LazyGetService<IOptions<AbpClockOptions>>(); var useAbpClockHandleDateTime = dbContext.LazyServiceProvider.LazyGetRequiredService<IOptions<AbpMongoDbOptions>>().Value.UseAbpClockHandleDateTime;
var entityModels = _entityModelBuilders var entityModels = _entityModelBuilders
.Select(x => x.Value) .Select(x => x.Value)
@ -41,7 +41,7 @@ namespace Volo.Abp.MongoDB
{ {
var map = entityModel.As<IHasBsonClassMap>().GetMap(); var map = entityModel.As<IHasBsonClassMap>().GetMap();
if (clockOptions != null) if (useAbpClockHandleDateTime)
{ {
var dateTimePropertyInfos = entityModel.EntityType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) var dateTimePropertyInfos = entityModel.EntityType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(property => .Where(property =>
@ -58,11 +58,11 @@ namespace Volo.Abp.MongoDB
if (property.PropertyType == typeof(DateTime?)) if (property.PropertyType == typeof(DateTime?))
{ {
map.MapProperty(property.Name).SetSerializer(new NullableSerializer<DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(clockOptions.Value.Kind, disableDateTimeNormalization))); map.MapProperty(property.Name).SetSerializer(new NullableSerializer<DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), disableDateTimeNormalization)));
} }
else else
{ {
map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(clockOptions.Value.Kind, disableDateTimeNormalization)); map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), disableDateTimeNormalization));
} }
}); });
} }
@ -86,7 +86,7 @@ namespace Volo.Abp.MongoDB
var map = new BsonClassMap(baseClass); var map = new BsonClassMap(baseClass);
map.ConfigureAbpConventions(); map.ConfigureAbpConventions();
if (clockOptions != null) if (useAbpClockHandleDateTime)
{ {
var dateTimePropertyInfos = baseClass.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) var dateTimePropertyInfos = baseClass.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(property => .Where(property =>
@ -99,11 +99,11 @@ namespace Volo.Abp.MongoDB
{ {
if (property.PropertyType == typeof(DateTime?)) if (property.PropertyType == typeof(DateTime?))
{ {
map.MapProperty(property.Name).SetSerializer(new NullableSerializer<DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(clockOptions.Value.Kind, false))); map.MapProperty(property.Name).SetSerializer(new NullableSerializer<DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), false)));
} }
else else
{ {
map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(clockOptions.Value.Kind, false)); map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), false));
} }
}); });
} }
@ -116,6 +116,11 @@ namespace Volo.Abp.MongoDB
} }
} }
private DateTimeKind GetDateTimeKind(AbpMongoDbContext dbContext)
{
return dbContext.LazyServiceProvider.LazyGetRequiredService<IOptions<AbpClockOptions>>().Value.Kind;
}
public virtual void Entity<TEntity>(Action<IMongoEntityModelBuilder<TEntity>> buildAction = null) public virtual void Entity<TEntity>(Action<IMongoEntityModelBuilder<TEntity>> buildAction = null)
{ {
var model = (IMongoEntityModelBuilder<TEntity>)_entityModelBuilders.GetOrAdd( var model = (IMongoEntityModelBuilder<TEntity>)_entityModelBuilders.GetOrAdd(

41
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Serializer/MongoDB_DateTimeKind_Tests.cs

@ -1,8 +1,11 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers; using MongoDB.Bson.Serialization.Serializers;
using Shouldly;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing; using Volo.Abp.TestApp.Testing;
using Volo.Abp.Timing; using Volo.Abp.Timing;
using Xunit; using Xunit;
@ -10,7 +13,7 @@ using Xunit;
namespace Volo.Abp.MongoDB.Serializer namespace Volo.Abp.MongoDB.Serializer
{ {
[Collection(MongoTestCollection.Name)] [Collection(MongoTestCollection.Name)]
public abstract class MongoDB_DateTimeKind_Tests : DateTimeKind_Tests<AbpMongoDbTestModule>, IDisposable public abstract class MongoDB_DateTimeKind_Tests : DateTimeKind_Tests<AbpMongoDbTestModule>
{ {
protected override void AfterAddApplication(IServiceCollection services) protected override void AfterAddApplication(IServiceCollection services)
{ {
@ -78,4 +81,40 @@ namespace Volo.Abp.MongoDB.Serializer
base.AfterAddApplication(services); base.AfterAddApplication(services);
} }
} }
[Collection(MongoTestCollection.Name)]
public class DisableDateTimeKindTests : TestAppTestBase<AbpMongoDbTestModule>
{
protected IPersonRepository PersonRepository { get; }
public DisableDateTimeKindTests()
{
PersonRepository = GetRequiredService<IPersonRepository>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<AbpMongoDbOptions>(x => x.UseAbpClockHandleDateTime = false);
base.AfterAddApplication(services);
}
[Fact]
public async Task DateTime_Kind_Should_Be_Normalized_By_MongoDb_Test()
{
var personId = Guid.NewGuid();
await PersonRepository.InsertAsync(new Person(personId, "bob lee", 18)
{
Birthday = DateTime.Parse("2020-01-01 00:00:00"),
LastActive = DateTime.Parse("2020-01-01 00:00:00"),
}, true);
var person = await PersonRepository.GetAsync(personId);
person.ShouldNotBeNull();
person.CreationTime.Kind.ShouldBe(DateTimeKind.Utc);
person.Birthday.ShouldNotBeNull();
person.Birthday.Value.Kind.ShouldBe(DateTimeKind.Utc);
}
}
} }

Loading…
Cancel
Save