mirror of https://github.com/abpframework/abp.git
Browse Source
Removed EntityExtensionManager merged functionality to ObjectExtensionManager.pull/3401/head
23 changed files with 324 additions and 245 deletions
@ -1,14 +0,0 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Extensions |
|||
{ |
|||
public class EntityExtensionInfo |
|||
{ |
|||
public Dictionary<string, PropertyExtensionInfo> Properties { get; set; } |
|||
|
|||
public EntityExtensionInfo() |
|||
{ |
|||
Properties = new Dictionary<string, PropertyExtensionInfo>(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,133 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Extensions |
|||
{ |
|||
public static class EntityExtensionManager |
|||
{ |
|||
private static readonly Dictionary<Type, EntityExtensionInfo> ExtensionInfos; |
|||
|
|||
static EntityExtensionManager() |
|||
{ |
|||
ExtensionInfos = new Dictionary<Type, EntityExtensionInfo>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds an extension property for an entity.
|
|||
/// If it is already added, replaces the <paramref name="propertyBuildAction"/>
|
|||
/// by the given one!
|
|||
/// </summary>
|
|||
/// <typeparam name="TEntity">Type of the entity</typeparam>
|
|||
/// <typeparam name="TProperty">Type of the new property</typeparam>
|
|||
/// <param name="propertyName">Name of the property</param>
|
|||
/// <param name="propertyBuildAction">An action to configure the database mapping for the new property</param>
|
|||
public static void AddProperty<TEntity, TProperty>( |
|||
[NotNull]string propertyName, |
|||
[NotNull]Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
AddProperty( |
|||
typeof(TEntity), |
|||
typeof(TProperty), |
|||
propertyName, |
|||
propertyBuildAction |
|||
); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds an extension property for an entity.
|
|||
/// If it is already added, replaces the <paramref name="propertyBuildAction"/>
|
|||
/// by the given one!
|
|||
/// </summary>
|
|||
/// <param name="entityType">Type of the entity</param>
|
|||
/// <param name="propertyType">Type of the new property</param>
|
|||
/// <param name="propertyName">Name of the property</param>
|
|||
/// <param name="propertyBuildAction">An action to configure the database mapping for the new property</param>
|
|||
public static void AddProperty( |
|||
Type entityType, |
|||
Type propertyType, |
|||
[NotNull]string propertyName, |
|||
[NotNull]Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
Check.NotNull(entityType, nameof(entityType)); |
|||
Check.NotNull(propertyType, nameof(propertyType)); |
|||
Check.NotNullOrWhiteSpace(propertyName, nameof(propertyName)); |
|||
Check.NotNull(propertyBuildAction, nameof(propertyBuildAction)); |
|||
|
|||
var extensionInfo = ExtensionInfos |
|||
.GetOrAdd(entityType, () => new EntityExtensionInfo()); |
|||
|
|||
var propertyExtensionInfo = extensionInfo.Properties |
|||
.GetOrAdd(propertyName, () => new PropertyExtensionInfo(propertyType)); |
|||
|
|||
propertyExtensionInfo.Action = propertyBuildAction; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Configures the entity mapping for the defined extensions.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEntity">The entity tye</typeparam>
|
|||
/// <param name="entityTypeBuilder">Entity type builder</param>
|
|||
public static void ConfigureExtensions<TEntity>( |
|||
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder) |
|||
where TEntity : class, IHasExtraProperties |
|||
{ |
|||
ConfigureExtensions(typeof(TEntity), entityTypeBuilder); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Configures the entity mapping for the defined extensions.
|
|||
/// </summary>
|
|||
/// <param name="entityType">Type of the entity</param>
|
|||
/// <param name="entityTypeBuilder">Entity type builder</param>
|
|||
public static void ConfigureExtensions( |
|||
[NotNull] Type entityType, |
|||
[NotNull] EntityTypeBuilder entityTypeBuilder) |
|||
{ |
|||
Check.NotNull(entityType, nameof(entityType)); |
|||
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); |
|||
|
|||
var entityExtensionInfo = ExtensionInfos.GetOrDefault(entityType); |
|||
if (entityExtensionInfo == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var propertyExtensionInfo in entityExtensionInfo.Properties) |
|||
{ |
|||
var propertyName = propertyExtensionInfo.Key; |
|||
var propertyType = propertyExtensionInfo.Value.PropertyType; |
|||
|
|||
/* Prevent multiple calls to the entityTypeBuilder.Property(...) method */ |
|||
if (entityTypeBuilder.Metadata.FindProperty(propertyName) != null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
var property = entityTypeBuilder.Property( |
|||
propertyType, |
|||
propertyName |
|||
); |
|||
|
|||
propertyExtensionInfo.Value.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(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Extensions |
|||
{ |
|||
public class PropertyExtensionInfo |
|||
{ |
|||
public Action<PropertyBuilder> Action { get; set; } |
|||
|
|||
public Type PropertyType { get; } |
|||
|
|||
public PropertyExtensionInfo(Type propertyType) |
|||
{ |
|||
PropertyType = propertyType; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public static class EfCoreObjectExtensionInfoExtensions |
|||
{ |
|||
public static ObjectExtensionPropertyInfo MapEfCoreProperty<TDbField>( |
|||
this ObjectExtensionInfo objectExtensionInfo, |
|||
string propertyName, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
return objectExtensionInfo.MapEfCoreProperty( |
|||
typeof(TDbField), |
|||
propertyName, |
|||
propertyBuildAction |
|||
); |
|||
} |
|||
|
|||
public static ObjectExtensionPropertyInfo MapEfCoreProperty( |
|||
this ObjectExtensionInfo objectExtensionInfo, |
|||
Type dbFieldType, |
|||
string propertyName, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
return objectExtensionInfo.AddOrUpdateProperty( |
|||
propertyName, |
|||
options => |
|||
{ |
|||
options.MapEfCore( |
|||
dbFieldType, |
|||
propertyBuildAction |
|||
); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public static class EfCoreObjectExtensionManagerExtensions |
|||
{ |
|||
public static ObjectExtensionInfo MapEfCoreProperty<TObject, TDbField>( |
|||
this ObjectExtensionManager objectExtensionManager, |
|||
string propertyName, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
return objectExtensionManager.MapEfCoreProperty( |
|||
typeof(TObject), |
|||
typeof(TDbField), |
|||
propertyName, |
|||
propertyBuildAction |
|||
); |
|||
} |
|||
|
|||
public static ObjectExtensionInfo MapEfCoreProperty( |
|||
this ObjectExtensionManager objectExtensionManager, |
|||
Type objectType, |
|||
Type dbFieldType, |
|||
string propertyName, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
return objectExtensionManager.AddOrUpdate( |
|||
objectType, |
|||
objectOptions => |
|||
{ |
|||
objectOptions.AddOrUpdateProperty( |
|||
propertyName, |
|||
propertyOptions => |
|||
{ |
|||
propertyOptions.MapEfCore( |
|||
dbFieldType, |
|||
propertyBuildAction |
|||
); |
|||
} |
|||
); |
|||
}); |
|||
} |
|||
|
|||
public static void ConfigureEfCoreEntity( |
|||
this ObjectExtensionManager objectExtensionManager, |
|||
EntityTypeBuilder b) |
|||
{ |
|||
var objectExtension = objectExtensionManager.GetOrNull(b.Metadata.ClrType); |
|||
if (objectExtension == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var property in objectExtension.GetProperties()) |
|||
{ |
|||
var efCoreMapping = property.GetEfCoreMappingOrNull(); |
|||
if (efCoreMapping == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
/* Prevent multiple calls to the entityTypeBuilder.Property(...) method */ |
|||
if (b.Metadata.FindProperty(property.Name) != null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
var propertyBuilder = b.Property(efCoreMapping.FieldType, property.Name); |
|||
|
|||
efCoreMapping.PropertyBuildAction?.Invoke(propertyBuilder); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public static class EfCoreObjectExtensionPropertyInfoExtensions |
|||
{ |
|||
public const string EfCorePropertyConfigurationName = "EfCoreMapping"; |
|||
|
|||
public static ObjectExtensionPropertyInfo MapEfCore( |
|||
this ObjectExtensionPropertyInfo propertyExtension, |
|||
Type dbFieldType, |
|||
Action<PropertyBuilder> propertyBuildAction) |
|||
{ |
|||
var options = new ObjectExtensionPropertyInfoEfCoreMappingOptions( |
|||
dbFieldType, |
|||
propertyExtension, |
|||
propertyBuildAction |
|||
); |
|||
|
|||
propertyExtension.Configuration[EfCorePropertyConfigurationName] = options; |
|||
|
|||
return propertyExtension; |
|||
} |
|||
|
|||
[CanBeNull] |
|||
public static ObjectExtensionPropertyInfoEfCoreMappingOptions GetEfCoreMappingOrNull( |
|||
this ObjectExtensionPropertyInfo propertyExtension) |
|||
{ |
|||
return propertyExtension.Configuration.GetOrDefault(EfCorePropertyConfigurationName) |
|||
as ObjectExtensionPropertyInfoEfCoreMappingOptions; |
|||
} |
|||
|
|||
public static bool IsMappedToFieldForEfCore(this ObjectExtensionPropertyInfo propertyExtension) |
|||
{ |
|||
return propertyExtension.Configuration.ContainsKey(EfCorePropertyConfigurationName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public class ObjectExtensionPropertyInfoEfCoreMappingOptions |
|||
{ |
|||
[NotNull] |
|||
public ObjectExtensionPropertyInfo ExtensionProperty { get; } |
|||
|
|||
[NotNull] |
|||
public ObjectExtensionInfo ObjectExtension => ExtensionProperty.ObjectExtension; |
|||
|
|||
[NotNull] |
|||
public Type FieldType { get; } |
|||
|
|||
[CanBeNull] |
|||
public Action<PropertyBuilder> PropertyBuildAction { get; set; } |
|||
|
|||
public ObjectExtensionPropertyInfoEfCoreMappingOptions( |
|||
[NotNull] Type fieldType, |
|||
[NotNull] ObjectExtensionPropertyInfo extensionProperty, |
|||
[CanBeNull] Action<PropertyBuilder> propertyBuildAction = null) |
|||
{ |
|||
FieldType = Check.NotNull(fieldType, nameof(fieldType)); |
|||
ExtensionProperty = Check.NotNull(extensionProperty, nameof(extensionProperty)); |
|||
PropertyBuildAction = propertyBuildAction; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue