Browse Source

Handle enum conversion on the ExtraPropertiesValueConverter

pull/3985/head
Halil İbrahim Kalkan 6 years ago
parent
commit
6c949053d7
  1. 12
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
  2. 1
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  3. 45
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs

12
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs

@ -301,5 +301,17 @@ namespace Volo.Abp.Reflection
.GetConverter(targetType)
.ConvertFromString(value);
}
public static object ConvertFrom<TTargetType>(object value)
{
return ConvertFrom(typeof(TTargetType), value);
}
public static object ConvertFrom(Type targetType, object value)
{
return TypeDescriptor
.GetConverter(targetType)
.ConvertFrom(value);
}
}
}

1
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -194,6 +194,7 @@ namespace Volo.Abp.EntityFrameworkCore
{
continue;
}
/* Checking "currentValue != null" has a good advantage:
* Assume that you we already using a named extra property,
* then decided to create a field (entity extension) for it.

45
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Newtonsoft.Json;
using Volo.Abp.ObjectExtending;
@ -11,7 +12,7 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
public ExtraPropertiesValueConverter(Type entityType)
: base(
d => SerializeObject(d, entityType),
s => DeserializeObject(s))
s => DeserializeObject(s, entityType))
{
}
@ -38,9 +39,47 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
return JsonConvert.SerializeObject(copyDictionary, Formatting.None);
}
private static Dictionary<string, object> DeserializeObject(string extraPropertiesAsJson)
private static Dictionary<string, object> DeserializeObject(string extraPropertiesAsJson, Type entityType)
{
return JsonConvert.DeserializeObject<Dictionary<string, object>>(extraPropertiesAsJson);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(extraPropertiesAsJson);
if (entityType != null)
{
var objectExtension = ObjectExtensionManager.Instance.GetOrNull(entityType);
if (objectExtension != null)
{
foreach (var property in objectExtension.GetProperties())
{
dictionary[property.Name] = GetNormalizedValue(dictionary, property);
}
}
}
return dictionary;
}
private static object GetNormalizedValue(Dictionary<string, object> dictionary, ObjectExtensionPropertyInfo property)
{
var value = dictionary.GetOrDefault(property.Name);
if (value == null)
{
return null;
}
try
{
if (property.Type.IsEnum)
{
return Enum.Parse(property.Type, value.ToString(), true);
}
//return Convert.ChangeType(value, property.Type);
return value;
}
catch
{
return value;
}
}
}
}
Loading…
Cancel
Save