Browse Source

Enhance EntityHelper's TrySetId method.

Resolve #3461
pull/3490/head
maliming 6 years ago
parent
commit
fb8dee5ccc
  1. 46
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs
  2. 19
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/EntityHelper_Tests.cs

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

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using JetBrains.Annotations; using JetBrains.Annotations;
@ -95,32 +97,40 @@ namespace Volo.Abp.Domain.Entities
var lambdaBody = Expression.Equal(leftExpression, rightExpression); var lambdaBody = Expression.Equal(leftExpression, rightExpression);
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 checkForDisableGuidGenerationAttribute = false)
{ {
//TODO: Can be optimized (by caching per entity type)? var property = CachedPropertyInfo.GetOrAdd(
var entityType = entity.GetType(); entity.GetType().FullName + checkForDisableGuidGenerationAttribute, () =>
var idProperty = entityType.GetProperty( {
nameof(entity.Id) var entityType = entity.GetType();
); var idProperty = entityType.GetProperties()
.Where(x => x.GetSetMethod(true) != null)
.FirstOrDefault(x => x.Name == nameof(entity.Id));
if (idProperty == null || idProperty.GetSetMethod(true) == null) if (idProperty == null)
{ {
return; return null;
} }
if (checkForDisableGuidGenerationAttribute) if (checkForDisableGuidGenerationAttribute)
{ {
if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true)) if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{ {
return; return null;
} }
} }
return idProperty;
});
idProperty.SetValue(entity, idFactory()); property?.SetValue(entity, idFactory());
} }
} }
} }

19
framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/EntityHelper_Tests.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -33,6 +34,15 @@ namespace Volo.Abp.Domain.Entities
myEntityDisablesIdGeneration.Id.ShouldBe(default); myEntityDisablesIdGeneration.Id.ShouldBe(default);
} }
[Fact]
public static void SetId_NewIdFromBaseClass()
{
var idValue = Guid.NewGuid();
var myNewIdEntity = new NewIdEntity();
EntityHelper.TrySetId(myNewIdEntity, () => idValue, true);
myNewIdEntity.Id.ShouldBe(idValue);
}
private class MyEntityDerivedFromAggregateRoot : AggregateRoot<Guid> private class MyEntityDerivedFromAggregateRoot : AggregateRoot<Guid>
{ {
@ -53,5 +63,14 @@ namespace Volo.Abp.Domain.Entities
[DisableIdGeneration] [DisableIdGeneration]
public override Guid Id { get; protected set; } public override Guid Id { get; protected set; }
} }
public class NewIdEntity : Entity<Guid>
{
public new Guid Id
{
get => base.Id;
set => base.Id = value;
}
}
} }
} }

Loading…
Cancel
Save