Browse Source

Refactor EntityHelper.TrySetId method.

pull/3559/merge
Halil İbrahim Kalkan 7 years ago
parent
commit
c57f3ca5e5
  1. 29
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs

29
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs

@ -13,6 +13,9 @@ namespace Volo.Abp.Domain.Entities
/// </summary> /// </summary>
public static class EntityHelper public static class EntityHelper
{ {
private static readonly ConcurrentDictionary<string, PropertyInfo> CachedIdProperties =
new ConcurrentDictionary<string, PropertyInfo>();
public static bool IsEntity([NotNull] Type type) public static bool IsEntity([NotNull] Type type)
{ {
return typeof(IEntity).IsAssignableFrom(type); return typeof(IEntity).IsAssignableFrom(type);
@ -98,33 +101,29 @@ namespace Volo.Abp.Domain.Entities
return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam); return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam);
} }
private static readonly ConcurrentDictionary<string, PropertyInfo> CachedPropertyInfo =
new ConcurrentDictionary<string, PropertyInfo>();
public static void TrySetId<TKey>( public static void TrySetId<TKey>(
IEntity<TKey> entity, IEntity<TKey> entity,
Func<TKey> idFactory, Func<TKey> idFactory,
bool checkForDisableGuidGenerationAttribute = false) bool checkForDisableIdGenerationAttribute = false)
{ {
var property = CachedPropertyInfo.GetOrAdd( var property = CachedIdProperties.GetOrAdd(
entity.GetType().FullName + checkForDisableGuidGenerationAttribute, () => $"{entity.GetType().FullName}-{checkForDisableIdGenerationAttribute}", () =>
{ {
var entityType = entity.GetType(); var idProperty = entity
var idProperty = entityType.GetProperties() .GetType()
.Where(x => x.GetSetMethod(true) != null) .GetProperties()
.FirstOrDefault(x => x.Name == nameof(entity.Id)); .FirstOrDefault(x => x.Name == nameof(entity.Id) &&
x.GetSetMethod(true) != null);
if (idProperty == null) if (idProperty == null)
{ {
return null; return null;
} }
if (checkForDisableGuidGenerationAttribute) if (checkForDisableIdGenerationAttribute &&
idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{ {
if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true)) return null;
{
return null;
}
} }
return idProperty; return idProperty;

Loading…
Cancel
Save