Browse Source

Remove duplicate TrySetId method.

pull/6054/head
maliming 6 years ago
parent
commit
a9e33bfd2d
  1. 31
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs
  2. 3
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/JsonConverters/EntityJsonConverter.cs

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

@ -266,36 +266,5 @@ namespace Volo.Abp.Domain.Entities
property?.SetValue(entity, idFactory()); property?.SetValue(entity, idFactory());
} }
public static void TrySetId<TKey>(
object entity,
TKey id,
bool checkForDisableIdGenerationAttribute = false)
{
var property = CachedIdProperties.GetOrAdd(
$"{entity.GetType().FullName}-{checkForDisableIdGenerationAttribute}", () =>
{
var idProperty = entity
.GetType()
.GetProperties()
.FirstOrDefault(x => x.Name == nameof(IEntity<object>.Id) &&
x.GetSetMethod(true) != null);
if (idProperty == null)
{
return null;
}
if (checkForDisableIdGenerationAttribute &&
idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{
return null;
}
return idProperty;
});
property?.SetValue(entity, id);
}
} }
} }

3
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/JsonConverters/EntityJsonConverter.cs

@ -6,6 +6,7 @@ using Volo.Abp.Domain.Entities;
namespace Volo.Abp.MemoryDb.JsonConverters namespace Volo.Abp.MemoryDb.JsonConverters
{ {
public class EntityJsonConverter<TEntity, TKey> : JsonConverter<TEntity> public class EntityJsonConverter<TEntity, TKey> : JsonConverter<TEntity>
where TEntity : IEntity<TKey>
{ {
public override TEntity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override TEntity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
@ -16,7 +17,7 @@ namespace Volo.Abp.MemoryDb.JsonConverters
var id = JsonSerializer.Deserialize<TKey>(idJsonElement.GetRawText()); var id = JsonSerializer.Deserialize<TKey>(idJsonElement.GetRawText());
if (id != null) if (id != null)
{ {
EntityHelper.TrySetId(entity, id); EntityHelper.TrySetId(entity, () => id);
} }
return entity; return entity;

Loading…
Cancel
Save