From c9de97ad33059983c6f4fece304d8920ddb4112e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 20 Aug 2020 10:11:27 +0300 Subject: [PATCH] Configure extra properties in the startup template --- docs/en/Module-Entity-Extensions.md | 3 ++ .../MyProjectNameDomainSharedModule.cs | 4 +- ...yProjectNameModuleExtensionConfigurator.cs | 45 ++++++++++++++++--- .../MyProjectNameDomainModule.cs | 6 --- .../MyProjectNameDomainObjectExtensions.cs | 32 ------------- ...rojectNameEfCoreEntityExtensionMappings.cs | 37 ++++++++------- 6 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 docs/en/Module-Entity-Extensions.md delete mode 100644 templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/ObjectExtending/MyProjectNameDomainObjectExtensions.cs diff --git a/docs/en/Module-Entity-Extensions.md b/docs/en/Module-Entity-Extensions.md new file mode 100644 index 0000000000..61a7093dc5 --- /dev/null +++ b/docs/en/Module-Entity-Extensions.md @@ -0,0 +1,3 @@ +# Module Entity Extensions + +See https://docs.abp.io/en/commercial/latest/guides/module-entity-extensions (it will be moved here soon). \ No newline at end of file diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs index 885d98a7c2..96432fabc8 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs @@ -29,7 +29,7 @@ namespace MyCompanyName.MyProjectName { public override void PreConfigureServices(ServiceConfigurationContext context) { - MyProjectNameModulePropertyConfigurator.Configure(); + MyProjectNameModuleExtensionConfigurator.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) @@ -48,7 +48,7 @@ namespace MyCompanyName.MyProjectName options.DefaultResourceType = typeof(MyProjectNameResource); }); - + Configure(options => { options.MapCodeNamespace("MyProjectName", typeof(MyProjectNameResource)); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameModuleExtensionConfigurator.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameModuleExtensionConfigurator.cs index ce3de2992f..69821d1aee 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameModuleExtensionConfigurator.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameModuleExtensionConfigurator.cs @@ -1,11 +1,11 @@ -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; using Volo.Abp.Identity; using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; namespace MyCompanyName.MyProjectName { - public static class MyProjectNameModulePropertyConfigurator + public static class MyProjectNameModuleExtensionConfigurator { private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -14,6 +14,7 @@ namespace MyCompanyName.MyProjectName OneTimeRunner.Run(() => { ConfigureExistingProperties(); + ConfigureExtraProperties(); }); } @@ -23,13 +24,47 @@ namespace MyCompanyName.MyProjectName * entities defined in the modules used by your application. * * Example: Change user and role name max lengths - + IdentityUserConsts.MaxNameLength = 99; IdentityRoleConsts.MaxNameLength = 99; - + * Notice: It is not suggested to change property lengths * unless you really need it. Go with the standard values wherever possible. */ } + + private static void ConfigureExtraProperties() + { + /* You can configure extra properties for the + * entities defined in the modules used by your application. + * + * This class can be used to define these extra properties + * with a high level, easy to use API. + * + * Example: Add a new property to the user entity of the identity module + + ObjectExtensionManager.Instance.Modules() + .ConfigureIdentity(identity => + { + identity.ConfigureUser(user => + { + user.AddOrUpdateProperty( //property type: string + "SocialSecurityNumber", //property name + property => + { + //validation rules + property.Attributes.Add(new RequiredAttribute()); + property.Attributes.Add(new StringLengthAttribute(64) {MinimumLength = 4}); + + //...other configurations for this property + } + ); + }); + }); + + * See the documentation for more: + * https://docs.abp.io/en/latest/Module-Entity-Extensions + */ + } } -} \ No newline at end of file +} diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/MyProjectNameDomainModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/MyProjectNameDomainModule.cs index 014594801f..91b05c877d 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/MyProjectNameDomainModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/MyProjectNameDomainModule.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MyCompanyName.MyProjectName.MultiTenancy; -using MyCompanyName.MyProjectName.ObjectExtending; using Volo.Abp.AuditLogging; using Volo.Abp.BackgroundJobs; using Volo.Abp.Emailing; @@ -32,11 +31,6 @@ namespace MyCompanyName.MyProjectName )] public class MyProjectNameDomainModule : AbpModule { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - MyProjectNameDomainObjectExtensions.Configure(); - } - public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/ObjectExtending/MyProjectNameDomainObjectExtensions.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/ObjectExtending/MyProjectNameDomainObjectExtensions.cs deleted file mode 100644 index 6c1439e7c2..0000000000 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/ObjectExtending/MyProjectNameDomainObjectExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Volo.Abp.Identity; -using Volo.Abp.ObjectExtending; -using Volo.Abp.Threading; - -namespace MyCompanyName.MyProjectName.ObjectExtending -{ - public static class MyProjectNameDomainObjectExtensions - { - private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); - - public static void Configure() - { - OneTimeRunner.Run(() => - { - /* You can configure extension properties to entities or other object types - * defined in the depended modules. - * - * If you are using EF Core and want to map the entity extension properties to new - * table fields in the database, then configure them in the MyProjectNameEfCoreEntityExtensionMappings - * - * Example: - * - * ObjectExtensionManager.Instance - * .AddOrUpdateProperty("Title"); - * - * See the documentation for more: - * https://docs.abp.io/en/abp/latest/Object-Extensions - */ - }); - } - } -} \ No newline at end of file diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEfCoreEntityExtensionMappings.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEfCoreEntityExtensionMappings.cs index 0b19050cb0..6dd70385c4 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEfCoreEntityExtensionMappings.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEfCoreEntityExtensionMappings.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Identity; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Identity; using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; @@ -10,26 +11,30 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore public static void Configure() { - MyProjectNameModulePropertyConfigurator.Configure(); - + MyProjectNameModuleExtensionConfigurator.Configure(); + OneTimeRunner.Run(() => { - /* You can configure entity extension properties for the - * entities defined in the used modules. - * - * The properties defined here becomes table fields. - * If you want to use the ExtraProperties dictionary of the entity - * instead of creating a new field, then define the property in the - * MyProjectNameDomainObjectExtensions class. + /* You can configure extra properties for the + * entities defined in the modules used by your application. * - * Example: + * This class can be used to map these extra properties to table fields in the database. * - * ObjectExtensionManager.Instance - * .MapEfCoreProperty( - * "MyProperty", - * b => b.HasMaxLength(128) - * ); + * USE THIS CLASS ONLY TO CONFIGURE EF CORE RELATED MAPPING. + * USE MyProjectNameModuleExtensionConfigurator CLASS (in the Domain.Shared project) + * FOR A HIGH LEVEL API TO DEFINE EXTRA PROPERTIES TO ENTITIES OF THE USED MODULES * + * Example: Map a property to a table field: + + ObjectExtensionManager.Instance + .MapEfCoreProperty( + "MyProperty", + (entityBuilder, propertyBuilder) => + { + propertyBuilder.HasMaxLength(128); + } + ); + * See the documentation for more: * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities */