#1580 Removed setter for IEntity<TKey>.Id

pull/1810/head
Halil İbrahim Kalkan 6 years ago
parent deb086e6a8
commit efc202bd56

@ -289,7 +289,11 @@ namespace Volo.Abp.Application.Services
return;
}
entityWithGuidId.Id = GuidGenerator.Create();
EntityHelper.TrySetId(
entityWithGuidId,
() => GuidGenerator.Create(),
true
);
}
/// <summary>

@ -0,0 +1,9 @@
using System;
namespace Volo.Abp.Domain.Entities
{
public class DisableIdGenerationAttribute : Attribute
{
}
}

@ -81,5 +81,32 @@ namespace Volo.Abp.Domain.Entities
var lambdaBody = Expression.Equal(leftExpression, rightExpression);
return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam);
}
public static void TrySetId<TKey>(
IEntity<TKey> entity,
Func<TKey> idFactory,
bool checkForDisableGuidGenerationAttribute = false)
{
//TODO: Can be optimized (by caching per entity type)?
var entityType = entity.GetType();
var idProperty = entityType.GetProperty(
nameof(entity.Id)
);
if (idProperty == null)
{
return;
}
if (checkForDisableGuidGenerationAttribute)
{
if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{
return;
}
}
idProperty.SetValue(entity, idFactory());
}
}
}

@ -22,6 +22,6 @@
/// <summary>
/// Unique identifier for this entity.
/// </summary>
TKey Id { get; set; }
TKey Id { get; }
}
}

@ -281,20 +281,37 @@ namespace Volo.Abp.EntityFrameworkCore
protected virtual void CheckAndSetId(EntityEntry entry)
{
//Set GUID Ids
var entity = entry.Entity as IEntity<Guid>;
if (entity != null && entity.Id == Guid.Empty)
if (entry.Entity is IEntity<Guid> entityWithGuidId)
{
var dbGeneratedAttr = ReflectionHelper
.GetSingleAttributeOrDefault<DatabaseGeneratedAttribute>(
entry.Property("Id").Metadata.PropertyInfo
);
TrySetGuidId(entry, entityWithGuidId);
}
}
if (dbGeneratedAttr == null || dbGeneratedAttr.DatabaseGeneratedOption == DatabaseGeneratedOption.None)
{
entity.Id = GuidGenerator.Create();
}
protected virtual void TrySetGuidId(EntityEntry entry, IEntity<Guid> entity)
{
if (entity.Id != default)
{
return;
}
var idProperty = entry.Property("Id").Metadata.PropertyInfo;
//Check for DatabaseGeneratedAttribute
var dbGeneratedAttr = ReflectionHelper
.GetSingleAttributeOrDefault<DatabaseGeneratedAttribute>(
idProperty
);
if (dbGeneratedAttr != null && dbGeneratedAttr.DatabaseGeneratedOption != DatabaseGeneratedOption.None)
{
return;
}
EntityHelper.TrySetId(
entity,
() => GuidGenerator.Create(),
true
);
}
protected virtual void SetCreationAuditProperties(EntityEntry entry)

@ -75,11 +75,13 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
protected virtual void SetIdIfNeeded(TEntity entity)
{
if (typeof(TKey) == typeof(int) || typeof(TKey) == typeof(long) || typeof(TKey) == typeof(Guid))
if (typeof(TKey) == typeof(int) ||
typeof(TKey) == typeof(long) ||
typeof(TKey) == typeof(Guid))
{
if (EntityHelper.HasDefaultId(entity))
{
entity.Id = Database.GenerateNextId<TEntity, TKey>();
EntityHelper.TrySetId(entity, () => Database.GenerateNextId<TEntity, TKey>());
}
}
}

@ -15,6 +15,7 @@ using Volo.Abp.EventBus.Local;
using Volo.Abp.Guids;
using Volo.Abp.MongoDB;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Reflection;
using Volo.Abp.Threading;
namespace Volo.Abp.Domain.Repositories.MongoDB
@ -315,12 +316,26 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
protected virtual void CheckAndSetId(TEntity entity)
{
if (entity is IEntity<Guid> entityWithGuidId && entityWithGuidId.Id == default)
if (entity is IEntity<Guid> entityWithGuidId)
{
entityWithGuidId.Id = GuidGenerator.Create();
TrySetGuidId(entityWithGuidId);
}
}
protected virtual void TrySetGuidId(IEntity<Guid> entity)
{
if (entity.Id != default)
{
return;
}
EntityHelper.TrySetId(
entity,
() => GuidGenerator.Create(),
true
);
}
protected virtual void SetCreationAuditProperties(TEntity entity)
{
AuditPropertySetter.SetCreationProperties(entity);

@ -0,0 +1,57 @@
using System;
using Shouldly;
using Xunit;
namespace Volo.Abp.Domain.Entities
{
public class EntityHelper_Tests
{
[Fact]
public static void SetId_DerivedFromAggregateRoot()
{
var idValue = Guid.NewGuid();
var myEntityDerivedFromAggregateRoot = new MyEntityDerivedFromAggregateRoot();
EntityHelper.TrySetId(myEntityDerivedFromAggregateRoot, () => idValue, true);
myEntityDerivedFromAggregateRoot.Id.ShouldBe(idValue);
}
[Fact]
public static void SetId_ImplementsIEntity()
{
var idValue = Guid.NewGuid();
var myEntityImplementsIEntity = new MyEntityImplementsIEntity();
EntityHelper.TrySetId(myEntityImplementsIEntity, () => idValue, true);
myEntityImplementsIEntity.Id.ShouldBe(idValue);
}
[Fact]
public static void SetId_DisablesIdGeneration()
{
var idValue = Guid.NewGuid();
var myEntityDisablesIdGeneration = new MyEntityDisablesIdGeneration();
EntityHelper.TrySetId(myEntityDisablesIdGeneration, () => idValue, true);
myEntityDisablesIdGeneration.Id.ShouldBe(default);
}
private class MyEntityDerivedFromAggregateRoot : AggregateRoot<Guid>
{
}
private class MyEntityImplementsIEntity : IEntity<Guid>
{
public Guid Id { get; set; }
public object[] GetKeys()
{
return new object[] { Id };
}
}
private class MyEntityDisablesIdGeneration : Entity<Guid>
{
[DisableIdGeneration]
public override Guid Id { get; set; }
}
}
}
Loading…
Cancel
Save