Skip to content

Commit

Permalink
NamedEntity.GetEntityName()
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcakir committed Sep 28, 2022
1 parent 9c6f4ee commit 6990b24
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Smartstore.Core.Seo;

namespace Smartstore.Core.Localization
{
Expand All @@ -16,7 +17,7 @@ public class LocalizedEntityDescriptor

public string KeyGroup
{
get => _keyGroup ?? EntityType.Name;
get => _keyGroup ?? NamedEntity.GetEntityName(EntityType);
init => _keyGroup = value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.CompilerServices;
using Smartstore.Core.Data;
using Smartstore.Core.Identity;
using Smartstore.Core.Seo;

namespace Smartstore.Core.Security
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public static IQueryable<T> ApplyAclFilter<T>(this IQueryable<T> query, int[] cu
return query;
}

var entityName = new T().GetEntityName();
var entityName = NamedEntity.GetEntityName<T>();

var subQuery = db.AclRecords
.Where(x => x.EntityName == entityName && customerRoleIds.Contains(x.CustomerRoleId))
Expand Down
18 changes: 18 additions & 0 deletions src/Smartstore.Core/Platform/Seo/Domain/NamedEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,23 @@ public string GetDisplayNameMemberName()

public override string GetEntityName()
=> EntityName;

public static string GetEntityName<T>()
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Smartstore.Core.Data;
using Smartstore.Core.Seo;

namespace Smartstore.Core.Stores
{
Expand All @@ -24,7 +25,7 @@ public static IQueryable<T> ApplyStoreFilter<T>(this IQueryable<T> query, int st
return query;
}

var entityName = new T().GetEntityName();
var entityName = NamedEntity.GetEntityName<T>();

var subQuery = db.StoreMappings
.Where(x => x.EntityName == entityName && x.StoreId == storeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Manufacturer>();
private readonly static string _productEntityName = NamedEntity.GetEntityName<Product>();

private readonly List<Manufacturer> _manufacturers = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MenuEntity>();
private readonly static string _menuItemEntityName = NamedEntity.GetEntityName<MenuItemEntity>();

private readonly List<MenuEntity> _menus = new()
{
Expand Down

0 comments on commit 6990b24

Please sign in to comment.