Skip to content

Commit

Permalink
Merge pull request #20951 from abpframework/ExtraPropertiesValueConve…
Browse files Browse the repository at this point in the history
…rter

Add a `Type` parameter to `ExtraPropertiesValueConverter`.
  • Loading branch information
salihozkara authored Oct 2, 2024
2 parents 0a35076 + b54263d commit b21e79a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
Expand Down Expand Up @@ -60,9 +61,11 @@ public static void TryConfigureExtraProperties(this EntityTypeBuilder b)
return;
}

var type = typeof(ExtraPropertiesValueConverter<>).MakeGenericType(b.Metadata.ClrType);
var extraPropertiesValueConverter = Activator.CreateInstance(type)!.As<ValueConverter<ExtraPropertyDictionary, string>>();
b.Property<ExtraPropertyDictionary>(nameof(IHasExtraProperties.ExtraProperties))
.HasColumnName(nameof(IHasExtraProperties.ExtraProperties))
.HasConversion(new ExtraPropertiesValueConverter(b.Metadata.ClrType))
.HasConversion(extraPropertiesValueConverter)
.Metadata.SetValueComparer(new ExtraPropertyDictionaryValueComparer());

b.TryConfigureObjectExtensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@

namespace Volo.Abp.EntityFrameworkCore.ValueConverters;

public class ExtraPropertiesValueConverter : ValueConverter<ExtraPropertyDictionary, string>
public class ExtraPropertiesValueConverter<TEntityType> : ValueConverter<ExtraPropertyDictionary, string>
{
public ExtraPropertiesValueConverter(Type entityType)
public ExtraPropertiesValueConverter()
: base(
d => SerializeObject(d, entityType),
s => DeserializeObject(s, entityType))
d => SerializeObject(d),
s => DeserializeObject(s))
{

}

public readonly static JsonSerializerOptions SerializeOptions = new JsonSerializerOptions();

private static string SerializeObject(ExtraPropertyDictionary extraProperties, Type? entityType)
private static string SerializeObject(ExtraPropertyDictionary extraProperties)
{
var copyDictionary = new Dictionary<string, object?>(extraProperties);

var entityType = typeof(TEntityType);
if (entityType != null)
{
var objectExtension = ObjectExtensionManager.Instance.GetOrNull(entityType);
Expand All @@ -50,7 +51,7 @@ private static string SerializeObject(ExtraPropertyDictionary extraProperties, T
}
};

private static ExtraPropertyDictionary DeserializeObject(string extraPropertiesAsJson, Type? entityType)
private static ExtraPropertyDictionary DeserializeObject(string extraPropertiesAsJson)
{
if (extraPropertiesAsJson.IsNullOrEmpty() || extraPropertiesAsJson == "{}")
{
Expand All @@ -60,6 +61,7 @@ private static ExtraPropertyDictionary DeserializeObject(string extraPropertiesA
var dictionary = JsonSerializer.Deserialize<ExtraPropertyDictionary>(extraPropertiesAsJson, DeserializeOptions) ??
new ExtraPropertyDictionary();

var entityType = typeof(TEntityType);
if (entityType != null)
{
var objectExtension = ObjectExtensionManager.Instance.GetOrNull(entityType);
Expand Down

0 comments on commit b21e79a

Please sign in to comment.