From c57f3ca5e578161998404110281abc64e362b501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 11 Apr 2020 23:57:02 +0300 Subject: [PATCH] Refactor EntityHelper.TrySetId method. --- .../Volo/Abp/Domain/Entities/EntityHelper.cs | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs index 5233f2accc..ad082c3fb0 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs @@ -13,6 +13,9 @@ namespace Volo.Abp.Domain.Entities /// public static class EntityHelper { + private static readonly ConcurrentDictionary CachedIdProperties = + new ConcurrentDictionary(); + public static bool IsEntity([NotNull] Type type) { return typeof(IEntity).IsAssignableFrom(type); @@ -98,33 +101,29 @@ namespace Volo.Abp.Domain.Entities return Expression.Lambda>(lambdaBody, lambdaParam); } - private static readonly ConcurrentDictionary CachedPropertyInfo = - new ConcurrentDictionary(); - public static void TrySetId( IEntity entity, Func idFactory, - bool checkForDisableGuidGenerationAttribute = false) + bool checkForDisableIdGenerationAttribute = false) { - var property = CachedPropertyInfo.GetOrAdd( - entity.GetType().FullName + checkForDisableGuidGenerationAttribute, () => + var property = CachedIdProperties.GetOrAdd( + $"{entity.GetType().FullName}-{checkForDisableIdGenerationAttribute}", () => { - var entityType = entity.GetType(); - var idProperty = entityType.GetProperties() - .Where(x => x.GetSetMethod(true) != null) - .FirstOrDefault(x => x.Name == nameof(entity.Id)); + var idProperty = entity + .GetType() + .GetProperties() + .FirstOrDefault(x => x.Name == nameof(entity.Id) && + x.GetSetMethod(true) != null); if (idProperty == null) { return null; } - if (checkForDisableGuidGenerationAttribute) + if (checkForDisableIdGenerationAttribute && + idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true)) { - if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true)) - { - return null; - } + return null; } return idProperty;