From 59ade77831012899a30d9502b3e4d0fcdf75373e Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 26 Dec 2019 16:25:38 +0800 Subject: [PATCH] Add more ConfigureJsonConversionWithValueComparer extension methods. --- .../Modeling/AbpPropertyBuilderExtensions.cs | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpPropertyBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpPropertyBuilderExtensions.cs index 5dae5b4771..93aeb792da 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpPropertyBuilderExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpPropertyBuilderExtensions.cs @@ -9,7 +9,87 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling { public static class AbpPropertyBuilderExtensions { - public static void ConfigureJsonConversionWithValueComparer( + /// + /// Serialize and property attributes using json(JsonConvert). + /// + /// The builder for the property being configured. + /// Sets the custom ValueComparer for this property. + public static PropertyBuilder ConfigureJsonConversionWithValueComparer( + this PropertyBuilder propertyBuilder, + ValueComparer valueComparer) + { + propertyBuilder.HasConversion( + d => JsonConvert.SerializeObject(d, Formatting.None), + s => JsonConvert.DeserializeObject(s) + ) + .Metadata.SetValueComparer(valueComparer); + + return propertyBuilder; + } + + /// + /// Serialize and property attributes using json(JsonConvert). + /// + /// The builder for the property being configured. + /// + /// If true, then EF will use ValueComparer if the type + /// implements it. This is usually used when byte arrays act as keys. + /// + public static PropertyBuilder ConfigureJsonConversionWithValueComparer( + this PropertyBuilder propertyBuilder, + bool favorStructuralComparisons) + { + propertyBuilder.HasConversion( + d => JsonConvert.SerializeObject(d, Formatting.None), + s => JsonConvert.DeserializeObject(s) + ) + .Metadata.SetValueComparer(new ValueComparer(favorStructuralComparisons)); + + return propertyBuilder; + } + + /// + /// Serialize and property attributes using json(JsonConvert). + /// Creates a new ValueComparer with the given comparison expression. + /// A shallow copy will be used for the snapshot. + /// + /// The builder for the property being configured. + /// The comparison expression. + /// The associated hash code generator. + public static PropertyBuilder ConfigureJsonConversionWithValueComparer( + this PropertyBuilder propertyBuilder, + Expression> equalsExpression, + Expression> hashCodeExpression) + { + propertyBuilder.HasConversion( + d => JsonConvert.SerializeObject(d, Formatting.None), + s => JsonConvert.DeserializeObject(s) + ) + .Metadata.SetValueComparer(new ValueComparer( + equalsExpression, + hashCodeExpression)); + + return propertyBuilder; + } + + /// + /// Serialize and property attributes using json(JsonConvert). + /// + /// Creates a new ValueComparer with the given comparison and + /// snapshotting expressions. + /// + /// + /// Snapshotting is the process of creating a copy of the value into a snapshot so it can + /// later be compared to determine if it has changed. For some types, such as collections, + /// this needs to be a deep copy of the collection rather than just a shallow copy of the + /// reference. + /// + /// + /// The builder for the property being configured. + /// The comparison expression. + /// The associated hash code generator. + /// The snapshot expression. + public static PropertyBuilder ConfigureJsonConversionWithValueComparer( this PropertyBuilder propertyBuilder, Expression> equalsExpression, Expression> hashCodeExpression, @@ -23,6 +103,8 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling equalsExpression, hashCodeExpression, snapshotExpression)); + + return propertyBuilder; } } } \ No newline at end of file