diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs index 783edf8b8b..ef3d87119a 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionInfoExtensions.cs @@ -1,37 +1,41 @@ using System; +using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace Volo.Abp.ObjectExtending { public static class EfCoreObjectExtensionInfoExtensions { - public static ObjectExtensionPropertyInfo MapEfCoreProperty( - this ObjectExtensionInfo objectExtensionInfo, - string propertyName, - Action propertyBuildAction) + public static ObjectExtensionInfo MapEfCoreProperty( + [NotNull] this ObjectExtensionInfo objectExtensionInfo, + [NotNull] string propertyName, + [CanBeNull] Action propertyBuildAction) { return objectExtensionInfo.MapEfCoreProperty( - typeof(TDbField), + typeof(TProperty), propertyName, propertyBuildAction ); } - public static ObjectExtensionPropertyInfo MapEfCoreProperty( - this ObjectExtensionInfo objectExtensionInfo, - Type dbFieldType, - string propertyName, - Action propertyBuildAction) + public static ObjectExtensionInfo MapEfCoreProperty( + [NotNull] this ObjectExtensionInfo objectExtensionInfo, + [NotNull] Type propertyType, + [NotNull] string propertyName, + [CanBeNull] Action propertyBuildAction) { + Check.NotNull(objectExtensionInfo, nameof(objectExtensionInfo)); + return objectExtensionInfo.AddOrUpdateProperty( + propertyType, propertyName, options => { options.MapEfCore( - dbFieldType, propertyBuildAction ); - }); + } + ); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs index 9261bdc3e8..a7c392f7c4 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs @@ -1,75 +1,73 @@ using System; using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Volo.Abp.Data; namespace Volo.Abp.ObjectExtending { public static class EfCoreObjectExtensionManagerExtensions { - public static ObjectExtensionInfo MapEfCoreProperty( + public static ObjectExtensionManager MapEfCoreProperty( this ObjectExtensionManager objectExtensionManager, string propertyName, Action propertyBuildAction) + where TObject : IHasExtraProperties { return objectExtensionManager.MapEfCoreProperty( typeof(TObject), - typeof(TDbField), + typeof(TProperty), propertyName, propertyBuildAction ); } - public static ObjectExtensionInfo MapEfCoreProperty( + public static ObjectExtensionManager MapEfCoreProperty( this ObjectExtensionManager objectExtensionManager, Type objectType, - Type dbFieldType, + Type propertyType, string propertyName, Action propertyBuildAction) { - return objectExtensionManager.AddOrUpdate( + return objectExtensionManager.AddOrUpdateProperty( objectType, - objectOptions => + propertyType, + propertyName, + options => { - objectOptions.AddOrUpdateProperty( - propertyName, - propertyOptions => - { - propertyOptions.MapEfCore( - dbFieldType, - propertyBuildAction - ); - } + options.MapEfCore( + propertyBuildAction ); - }); + } + ); } - public static void ConfigureEfCoreEntity( - this ObjectExtensionManager objectExtensionManager, - EntityTypeBuilder b) + 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 objectExtension = objectExtensionManager.GetOrNull(b.Metadata.ClrType); - if (objectExtension == null) + var efCoreMapping = property.GetEfCoreMappingOrNull(); + if (efCoreMapping == null) { - return; + continue; } - foreach (var property in objectExtension.GetProperties()) + /* Prevent multiple calls to the entityTypeBuilder.Property(...) method */ + if (b.Metadata.FindProperty(property.Name) != null) { - var efCoreMapping = property.GetEfCoreMappingOrNull(); - if (efCoreMapping == null) - { - continue; - } - - /* Prevent multiple calls to the entityTypeBuilder.Property(...) method */ - if (b.Metadata.FindProperty(property.Name) != null) - { - continue; - } + continue; + } - var propertyBuilder = b.Property(efCoreMapping.FieldType, property.Name); + var propertyBuilder = b.Property(property.Type, property.Name); - efCoreMapping.PropertyBuildAction?.Invoke(propertyBuilder); - } + efCoreMapping.PropertyBuildAction?.Invoke(propertyBuilder); } } } +} diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs index 9f97d0d7f4..e76edb167a 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionPropertyInfoExtensions.cs @@ -10,17 +10,16 @@ namespace Volo.Abp.ObjectExtending public const string EfCorePropertyConfigurationName = "EfCoreMapping"; public static ObjectExtensionPropertyInfo MapEfCore( - this ObjectExtensionPropertyInfo propertyExtension, - Type dbFieldType, - Action propertyBuildAction) + [NotNull] this ObjectExtensionPropertyInfo propertyExtension, + [CanBeNull] Action propertyBuildAction = null) { - var options = new ObjectExtensionPropertyInfoEfCoreMappingOptions( - dbFieldType, - propertyExtension, - propertyBuildAction - ); + Check.NotNull(propertyExtension, nameof(propertyExtension)); - propertyExtension.Configuration[EfCorePropertyConfigurationName] = options; + propertyExtension.Configuration[EfCorePropertyConfigurationName] = + new ObjectExtensionPropertyInfoEfCoreMappingOptions( + propertyExtension, + propertyBuildAction + ); return propertyExtension; } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs index 20dd1cef0a..0c9909f5d7 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfoEfCoreMappingOptions.cs @@ -12,18 +12,13 @@ namespace Volo.Abp.ObjectExtending [NotNull] public ObjectExtensionInfo ObjectExtension => ExtensionProperty.ObjectExtension; - [NotNull] - public Type FieldType { get; } - [CanBeNull] public Action PropertyBuildAction { get; set; } public ObjectExtensionPropertyInfoEfCoreMappingOptions( - [NotNull] Type fieldType, [NotNull] ObjectExtensionPropertyInfo extensionProperty, [CanBeNull] Action propertyBuildAction = null) { - FieldType = Check.NotNull(fieldType, nameof(fieldType)); ExtensionProperty = Check.NotNull(extensionProperty, nameof(extensionProperty)); PropertyBuildAction = propertyBuildAction; } diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs index 19f53c4b75..6f59465660 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionInfo.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using JetBrains.Annotations; namespace Volo.Abp.ObjectExtending { @@ -24,18 +25,33 @@ namespace Volo.Abp.ObjectExtending return Properties.ContainsKey(propertyName); } - public virtual ObjectExtensionPropertyInfo AddOrUpdateProperty( - string propertyName, - Action configureAction = null) + public virtual ObjectExtensionInfo AddOrUpdateProperty( + [NotNull] string propertyName, + [CanBeNull] Action configureAction = null) { + return AddOrUpdateProperty( + typeof(TProperty), + propertyName, + configureAction + ); + } + + public virtual ObjectExtensionInfo AddOrUpdateProperty( + [NotNull] Type propertyType, + [NotNull] string propertyName, + [CanBeNull] Action configureAction = null) + { + Check.NotNull(propertyType, nameof(propertyType)); + Check.NotNull(propertyName, nameof(propertyName)); + var propertyInfo = Properties.GetOrAdd( propertyName, - () => new ObjectExtensionPropertyInfo(this, propertyName) + () => new ObjectExtensionPropertyInfo(this, propertyType, propertyName) ); configureAction?.Invoke(propertyInfo); - return propertyInfo; + return this; } public virtual ImmutableList GetProperties() diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs index d6a42e82c7..0e94a3a984 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManager.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using JetBrains.Annotations; +using Volo.Abp.Data; namespace Volo.Abp.ObjectExtending { @@ -14,16 +16,19 @@ namespace Volo.Abp.ObjectExtending ObjectsExtensions = new Dictionary(); } - public virtual ObjectExtensionInfo AddOrUpdate( - Action configureAction = null) + public virtual ObjectExtensionManager AddOrUpdate( + [CanBeNull] Action configureAction = null) + where TObject : IHasExtraProperties { return AddOrUpdate(typeof(TObject), configureAction); } - public virtual ObjectExtensionInfo AddOrUpdate( - Type type, - Action configureAction = null) + public virtual ObjectExtensionManager AddOrUpdate( + [NotNull] Type type, + [CanBeNull] Action configureAction = null) { + Check.NotNull(type, nameof(type)); + var extensionInfo = ObjectsExtensions.GetOrAdd( type, () => new ObjectExtensionInfo(type) @@ -31,7 +36,7 @@ namespace Volo.Abp.ObjectExtending configureAction?.Invoke(extensionInfo); - return extensionInfo; + return this; } public virtual ObjectExtensionInfo GetOrNull() @@ -39,8 +44,10 @@ namespace Volo.Abp.ObjectExtending return GetOrNull(typeof(TObject)); } - public virtual ObjectExtensionInfo GetOrNull(Type type) + public virtual ObjectExtensionInfo GetOrNull([NotNull] Type type) { + Check.NotNull(type, nameof(type)); + return ObjectsExtensions.GetOrDefault(type); } } diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManagerExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManagerExtensions.cs new file mode 100644 index 0000000000..d21a8c22e1 --- /dev/null +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionManagerExtensions.cs @@ -0,0 +1,44 @@ +using System; +using JetBrains.Annotations; +using Volo.Abp.Data; + +namespace Volo.Abp.ObjectExtending +{ + public static class ObjectExtensionManagerExtensions + { + public static ObjectExtensionManager AddOrUpdateProperty( + [NotNull] this ObjectExtensionManager objectExtensionManager, + [NotNull] string propertyName, + [CanBeNull] Action configureAction = null) + where TObject : IHasExtraProperties + { + return objectExtensionManager.AddOrUpdateProperty( + typeof(TObject), + typeof(TProperty), + propertyName, + configureAction + ); + } + + public static ObjectExtensionManager AddOrUpdateProperty( + [NotNull] this ObjectExtensionManager objectExtensionManager, + [NotNull] Type objectType, + [NotNull] Type propertyType, + [NotNull] string propertyName, + [CanBeNull] Action configureAction = null) + { + Check.NotNull(objectExtensionManager, nameof(objectExtensionManager)); + + return objectExtensionManager.AddOrUpdate( + objectType, + options => + { + options.AddOrUpdateProperty( + propertyType, + propertyName, + configureAction + ); + }); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs index 58879cfd59..ca08f0f03b 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ObjectExtensionPropertyInfo.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using System; +using System.Collections.Generic; using JetBrains.Annotations; namespace Volo.Abp.ObjectExtending @@ -12,15 +12,19 @@ namespace Volo.Abp.ObjectExtending [NotNull] public string Name { get; } + [NotNull] + public Type Type { get; } + //[NotNull] //TODO: Will be implemented, probably in the v2.5 //public List ValidationAttributes { get; } [NotNull] public Dictionary Configuration { get; } - public ObjectExtensionPropertyInfo(ObjectExtensionInfo objectExtension, string name) + public ObjectExtensionPropertyInfo([NotNull] ObjectExtensionInfo objectExtension, [NotNull] Type type, [NotNull] string name) { ObjectExtension = Check.NotNull(objectExtension, nameof(objectExtension)); + Type = Check.NotNull(type, nameof(type)); Name = Check.NotNull(name, nameof(name)); //ValidationAttributes = new List();