From 796cd1b0e500ca8dbd200835fc8aa24a2a5280ba Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 21 Dec 2022 20:19:56 +0800 Subject: [PATCH] Add `AbpIncludeNonPublicPropertiesModifiers` when `AddEntityCache`. --- .../EntityCacheServiceCollectionExtensions.cs | 29 ++++++++++++------- .../AbpIncludeNonPublicPropertiesModifiers.cs | 8 ++--- ...cludeNonPublicPropertiesModifiers_Tests.cs | 22 +++++++++----- .../Abp/MemoryDb/AbpMemoryDbTestModule.cs | 14 +++++---- .../Abp/TestApp/Testing/EntityCache_Tests.cs | 28 +++++++++++------- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs index cb7e0e5ee7..bf7ceac45c 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs @@ -4,6 +4,8 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Caching; +using Volo.Abp.Json.SystemTextJson; +using Volo.Abp.Json.SystemTextJson.Modifiers; namespace Volo.Abp.Domain.Entities.Caching; @@ -11,7 +13,7 @@ public static class EntityCacheServiceCollectionExtensions { public static IServiceCollection AddEntityCache( this IServiceCollection services, - [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity { services @@ -21,18 +23,23 @@ public static class EntityCacheServiceCollectionExtensions >(); services .TryAddTransient>(); - + services.Configure(options => { options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); }); - + + services.Configure(options => + { + options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers().CreateModifyAction(x => x.Id)); + }); + return services; } - + public static IServiceCollection AddEntityCache( this IServiceCollection services, - [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity where TEntityCacheItem : class { @@ -43,18 +50,18 @@ public static class EntityCacheServiceCollectionExtensions >(); services .TryAddTransient>(); - + services.Configure(options => { options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); }); - + return services; } public static IServiceCollection AddEntityCache( this IServiceCollection services, - [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity where TEntityCacheItem : class { @@ -64,12 +71,12 @@ public static class EntityCacheServiceCollectionExtensions EntityCacheWithObjectMapperContext >(); services.TryAddTransient>(); - + services.Configure(options => { options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); }); - + return services; } @@ -79,4 +86,4 @@ public static class EntityCacheServiceCollectionExtensions AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2) }; } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs index 3721c3ffcd..4669ddc666 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs @@ -28,14 +28,10 @@ public class AbpIncludeNonPublicPropertiesModifiers x.Set == null); if (propertyJsonInfo != null) { - var propertyInfo = typeof(TClass).GetProperty(propertyName, BindingFlags.NonPublic); + var propertyInfo = typeof(TClass).GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (propertyInfo != null) { - var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(TProperty), propertyJsonInfo.Name); - jsonPropertyInfo.Get = propertyInfo.GetValue; - jsonPropertyInfo.Set = propertyInfo.SetValue; - jsonTypeInfo.Properties.Remove(propertyJsonInfo); - jsonTypeInfo.Properties.Add(jsonPropertyInfo); + propertyJsonInfo.Set = propertyInfo.SetValue; } } } diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs index d05ab2dfa0..38d733a3d8 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs @@ -29,10 +29,14 @@ public class AbpIncludeNonPublicPropertiesModifiers_Tests : AbpJsonTestBase [Fact] public void Test() { - var json = _jsonSerializer.Serialize(new NonPublicPropertiesClass() + var model = new NonPublicPropertiesClass { Id = "id" - }); + }; + model.SetName("my-name"); + model.SetAge("42"); + + var json = _jsonSerializer.Serialize(model); json.ShouldContain("id"); json.ShouldContain("name"); @@ -40,8 +44,8 @@ public class AbpIncludeNonPublicPropertiesModifiers_Tests : AbpJsonTestBase var obj = _jsonSerializer.Deserialize(json); obj.Id.ShouldBe("id"); - obj.Name.ShouldBe("name"); - obj.Age.ShouldBe("age"); + obj.Name.ShouldBe("my-name"); + obj.Age.ShouldBe("42"); } class NonPublicPropertiesClass @@ -52,10 +56,14 @@ public class AbpIncludeNonPublicPropertiesModifiers_Tests : AbpJsonTestBase public string Age { get; protected set; } - public NonPublicPropertiesClass() + public void SetName(string name) + { + Name = name; + } + + public void SetAge(string age) { - Name = "name"; - Age = "age"; + Age = age; } } } diff --git a/framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs b/framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs index 218c118c4d..f5b6a39bf4 100644 --- a/framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs +++ b/framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs @@ -1,11 +1,12 @@ using System; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.Modularity; using Volo.Abp.TestApp.MemoryDb; using Volo.Abp.Data; using Volo.Abp.Autofac; -using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories.MemoryDb; +using Volo.Abp.Json.SystemTextJson; using Volo.Abp.MemoryDb.JsonConverters; using Volo.Abp.TestApp; using Volo.Abp.TestApp.Domain; @@ -33,9 +34,12 @@ public class AbpMemoryDbTestModule : AbpModule options.AddRepository(); }); - Configure(options => - { - options.JsonSerializerOptions.Converters.Add(new EntityJsonConverter()); - }); + context.Services.AddOptions() + .Configure((options, serviceProvider) => + { + options.JsonSerializerOptions.Converters.Add(new EntityJsonConverter()); + options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider + .GetRequiredService>()); + }); } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs index 72492238d4..ac6da9106b 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs @@ -25,7 +25,7 @@ public abstract class EntityCache_Tests : TestAppTestBase>(); ProductCacheItem = GetRequiredService>(); } - + [Fact] public async Task Should_Return_Null_IF_Entity_Not_Exist() { @@ -41,28 +41,32 @@ public abstract class EntityCache_Tests : TestAppTestBase(() => ProductEntityCache.GetAsync(notExistId)); await Assert.ThrowsAsync(() => ProductCacheItem.GetAsync(notExistId)); } - + [Fact] public async Task Should_Return_EntityCache() { var product = await ProductEntityCache.FindAsync(TestDataBuilder.ProductId); product.ShouldNotBeNull(); + product = await ProductEntityCache.FindAsync(TestDataBuilder.ProductId); + product.ShouldNotBeNull(); product.Id.ShouldBe(TestDataBuilder.ProductId); product.Name.ShouldBe("Product1"); product.Price.ShouldBe(decimal.One); - + var productCacheItem = await ProductCacheItem.FindAsync(product.Id); productCacheItem.ShouldNotBeNull(); + productCacheItem = await ProductCacheItem.FindAsync(product.Id); + productCacheItem.ShouldNotBeNull(); productCacheItem.Id.ShouldBe(TestDataBuilder.ProductId); productCacheItem.Name.ShouldBe("Product1"); productCacheItem.Price.ShouldBe(decimal.One); } - + [Fact] public async Task Should_Return_Null_IF_Deleted() { await ProductRepository.DeleteAsync(TestDataBuilder.ProductId); - + (await ProductEntityCache.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull(); (await ProductCacheItem.FindAsync(TestDataBuilder.ProductId)).ShouldBeNull(); } @@ -77,13 +81,13 @@ public abstract class EntityCache_Tests : TestAppTestBase : TestAppTestBase { + public Product() + { + + } + public Product(Guid id, string name, decimal price) : base(id) { @@ -102,9 +111,6 @@ public class Product : FullAuditedAggregateRoot Price = price; } - [JsonInclude] - public override Guid Id { get; protected set; } - public string Name { get; set; } public decimal Price { get; set; } @@ -119,4 +125,4 @@ public class ProductCacheItem public string Name { get; set; } public decimal Price { get; set; } -} \ No newline at end of file +}