mirror of https://github.com/abpframework/abp.git
6 changed files with 210 additions and 4 deletions
@ -0,0 +1,88 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Extensions |
|||
{ |
|||
public class EntityExtensionInfo |
|||
{ |
|||
public Dictionary<string, PropertyExtensionInfo> Properties { get; set; } |
|||
|
|||
public EntityExtensionInfo() |
|||
{ |
|||
Properties = new Dictionary<string, PropertyExtensionInfo>(); |
|||
} |
|||
} |
|||
|
|||
public class PropertyExtensionInfo |
|||
{ |
|||
public List<Action<PropertyBuilder>> Actions { get; } |
|||
|
|||
public Type PropertyType { get; } |
|||
|
|||
public PropertyExtensionInfo(Type propertyType) |
|||
{ |
|||
PropertyType = propertyType; |
|||
Actions = new List<Action<PropertyBuilder>>(); |
|||
} |
|||
} |
|||
|
|||
public static class EntityExtensions |
|||
{ |
|||
private static readonly Dictionary<Type, EntityExtensionInfo> ExtensionInfos; |
|||
|
|||
//TODO: Use PropertyBuilder<TProperty> instead
|
|||
|
|||
static EntityExtensions() |
|||
{ |
|||
ExtensionInfos = new Dictionary<Type, EntityExtensionInfo>(); |
|||
} |
|||
|
|||
public static void AddProperty<TEntity>( |
|||
string name, |
|||
Type propertyType, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
var extensionInfo = ExtensionInfos |
|||
.GetOrAdd(typeof(TEntity), () => new EntityExtensionInfo()); |
|||
|
|||
var propertyExtensionInfo = extensionInfo.Properties |
|||
.GetOrAdd(name, () => new PropertyExtensionInfo(propertyType)); |
|||
|
|||
propertyExtensionInfo.Actions.Add(propertyBuildAction); |
|||
} |
|||
|
|||
public static void ConfigureProperties<TEntity>(EntityTypeBuilder entityTypeBuilder) |
|||
{ |
|||
var entityExtensionInfo = ExtensionInfos.GetOrDefault(typeof(TEntity)); |
|||
if (entityExtensionInfo == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var propertyExtensionInfo in entityExtensionInfo.Properties) |
|||
{ |
|||
var property = entityTypeBuilder.Property(propertyExtensionInfo.Value.PropertyType, propertyExtensionInfo.Key); |
|||
foreach (var action in propertyExtensionInfo.Value.Actions) |
|||
{ |
|||
action(property); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static string[] GetPropertyNames(Type entityType) |
|||
{ |
|||
var entityExtensionInfo = ExtensionInfos.GetOrDefault(entityType); |
|||
if (entityExtensionInfo == null) |
|||
{ |
|||
return Array.Empty<string>(); |
|||
} |
|||
|
|||
return entityExtensionInfo |
|||
.Properties |
|||
.Select(p => p.Key) |
|||
.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
|||
using Newtonsoft.Json; |
|||
using Volo.Abp.EntityFrameworkCore.Extensions; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.ValueConverters |
|||
{ |
|||
public class ExtraPropertiesValueConverter : ValueConverter<Dictionary<string, object>, string> |
|||
{ |
|||
public ExtraPropertiesValueConverter(Type entityType) |
|||
: base( |
|||
d => SerializeObject(d, entityType), |
|||
s => DeserializeObject(s)) |
|||
{ |
|||
|
|||
} |
|||
|
|||
private static string SerializeObject(Dictionary<string, object> extraProperties, Type entityType) |
|||
{ |
|||
var copyDictionary = new Dictionary<string, object>(extraProperties); |
|||
|
|||
if (entityType != null) |
|||
{ |
|||
var propertyNames = EntityExtensions.GetPropertyNames(entityType); |
|||
|
|||
foreach (var propertyName in propertyNames) |
|||
{ |
|||
copyDictionary.Remove(propertyName); |
|||
} |
|||
} |
|||
|
|||
return JsonConvert.SerializeObject(copyDictionary, Formatting.None); |
|||
} |
|||
|
|||
private static Dictionary<string, object> DeserializeObject(string extraPropertiesAsJson) |
|||
{ |
|||
return JsonConvert.DeserializeObject<Dictionary<string, object>>(extraPropertiesAsJson); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue