From 6990b243362e61422978ad9478d94c62cff65588 Mon Sep 17 00:00:00 2001 From: Murat Cakir Date: Wed, 28 Sep 2022 20:54:52 +0200 Subject: [PATCH] NamedEntity.GetEntityName() --- .../Domain/LocalizedEntityDescriptor.cs | 3 ++- .../IAclRestrictedQueryExtensions.cs | 3 ++- .../Platform/Seo/Domain/NamedEntity.cs | 18 ++++++++++++++++++ .../IStoreRestrictedQueryExtensions.cs | 3 ++- .../Brands/ManufacturerQueryExtensionsTests.cs | 5 +++-- .../Content/Menus/MenuQueryExtensionsTests.cs | 5 +++-- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Smartstore.Core/Platform/Localization/Domain/LocalizedEntityDescriptor.cs b/src/Smartstore.Core/Platform/Localization/Domain/LocalizedEntityDescriptor.cs index 4d10651d16..155790cb36 100644 --- a/src/Smartstore.Core/Platform/Localization/Domain/LocalizedEntityDescriptor.cs +++ b/src/Smartstore.Core/Platform/Localization/Domain/LocalizedEntityDescriptor.cs @@ -1,4 +1,5 @@ using System.Reflection; +using Smartstore.Core.Seo; namespace Smartstore.Core.Localization { @@ -16,7 +17,7 @@ public class LocalizedEntityDescriptor public string KeyGroup { - get => _keyGroup ?? EntityType.Name; + get => _keyGroup ?? NamedEntity.GetEntityName(EntityType); init => _keyGroup = value; } diff --git a/src/Smartstore.Core/Platform/Security/Extensions/IAclRestrictedQueryExtensions.cs b/src/Smartstore.Core/Platform/Security/Extensions/IAclRestrictedQueryExtensions.cs index 9a19458af7..85d6e7210e 100644 --- a/src/Smartstore.Core/Platform/Security/Extensions/IAclRestrictedQueryExtensions.cs +++ b/src/Smartstore.Core/Platform/Security/Extensions/IAclRestrictedQueryExtensions.cs @@ -1,6 +1,7 @@ using System.Runtime.CompilerServices; using Smartstore.Core.Data; using Smartstore.Core.Identity; +using Smartstore.Core.Seo; namespace Smartstore.Core.Security { @@ -38,7 +39,7 @@ public static IQueryable ApplyAclFilter(this IQueryable query, int[] cu return query; } - var entityName = new T().GetEntityName(); + var entityName = NamedEntity.GetEntityName(); var subQuery = db.AclRecords .Where(x => x.EntityName == entityName && customerRoleIds.Contains(x.CustomerRoleId)) diff --git a/src/Smartstore.Core/Platform/Seo/Domain/NamedEntity.cs b/src/Smartstore.Core/Platform/Seo/Domain/NamedEntity.cs index ab4900a902..1879c6b048 100644 --- a/src/Smartstore.Core/Platform/Seo/Domain/NamedEntity.cs +++ b/src/Smartstore.Core/Platform/Seo/Domain/NamedEntity.cs @@ -16,5 +16,23 @@ public string GetDisplayNameMemberName() public override string GetEntityName() => EntityName; + + public static string GetEntityName() + where T : INamedEntity, new() + { + return new T().GetEntityName(); + } + + public static string GetEntityName(Type entityType) + { + Guard.NotNull(entityType, nameof(entityType)); + + if (entityType.HasDefaultConstructor() && typeof(INamedEntity).IsAssignableFrom(entityType)) + { + return (Activator.CreateInstance(entityType) as INamedEntity).GetEntityName(); + } + + return entityType.Name; + } } } diff --git a/src/Smartstore.Core/Platform/Stores/Extensions/IStoreRestrictedQueryExtensions.cs b/src/Smartstore.Core/Platform/Stores/Extensions/IStoreRestrictedQueryExtensions.cs index 283735ad86..338fc70d95 100644 --- a/src/Smartstore.Core/Platform/Stores/Extensions/IStoreRestrictedQueryExtensions.cs +++ b/src/Smartstore.Core/Platform/Stores/Extensions/IStoreRestrictedQueryExtensions.cs @@ -1,4 +1,5 @@ using Smartstore.Core.Data; +using Smartstore.Core.Seo; namespace Smartstore.Core.Stores { @@ -24,7 +25,7 @@ public static IQueryable ApplyStoreFilter(this IQueryable query, int st return query; } - var entityName = new T().GetEntityName(); + var entityName = NamedEntity.GetEntityName(); var subQuery = db.StoreMappings .Where(x => x.EntityName == entityName && x.StoreId == storeId) diff --git a/test/Smartstore.Core.Tests/Catalog/Brands/ManufacturerQueryExtensionsTests.cs b/test/Smartstore.Core.Tests/Catalog/Brands/ManufacturerQueryExtensionsTests.cs index ac4cfdb229..75bcf3ac11 100644 --- a/test/Smartstore.Core.Tests/Catalog/Brands/ManufacturerQueryExtensionsTests.cs +++ b/test/Smartstore.Core.Tests/Catalog/Brands/ManufacturerQueryExtensionsTests.cs @@ -6,6 +6,7 @@ using Smartstore.Core.Catalog.Brands; using Smartstore.Core.Catalog.Products; using Smartstore.Core.Security; +using Smartstore.Core.Seo; using Smartstore.Core.Stores; namespace Smartstore.Core.Tests.Catalog.Brands @@ -15,8 +16,8 @@ public class ManufacturerQueryExtensionsTests : ServiceTestBase { #region Test data - private readonly static string _manufacturerEntityName = new Manufacturer().GetEntityName(); - private readonly static string _productEntityName = new Product().GetEntityName(); + private readonly static string _manufacturerEntityName = NamedEntity.GetEntityName(); + private readonly static string _productEntityName = NamedEntity.GetEntityName(); private readonly List _manufacturers = new() { diff --git a/test/Smartstore.Core.Tests/Content/Menus/MenuQueryExtensionsTests.cs b/test/Smartstore.Core.Tests/Content/Menus/MenuQueryExtensionsTests.cs index 77864bc2ac..4813d9d3af 100644 --- a/test/Smartstore.Core.Tests/Content/Menus/MenuQueryExtensionsTests.cs +++ b/test/Smartstore.Core.Tests/Content/Menus/MenuQueryExtensionsTests.cs @@ -6,6 +6,7 @@ using Smartstore.Core.Catalog.Products; using Smartstore.Core.Content.Menus; using Smartstore.Core.Security; +using Smartstore.Core.Seo; using Smartstore.Core.Stores; namespace Smartstore.Core.Tests.Content.Menus @@ -15,8 +16,8 @@ public class MenuQueryExtensionsTests : ServiceTestBase { #region Test data - private readonly static string _menuEntityName = new MenuEntity().GetEntityName(); - private readonly static string _menuItemEntityName = new MenuItemEntity().GetEntityName(); + private readonly static string _menuEntityName = NamedEntity.GetEntityName(); + private readonly static string _menuItemEntityName = NamedEntity.GetEntityName(); private readonly List _menus = new() {