Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance EntityHelper's TrySetId method. #3490

Merged
merged 1 commit into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
Expand Down Expand Up @@ -95,32 +97,40 @@ public static Expression<Func<TEntity, bool>> CreateEqualityExpressionForId<TEnt
var lambdaBody = Expression.Equal(leftExpression, rightExpression);
return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam);
}


private static readonly ConcurrentDictionary<string, PropertyInfo> CachedPropertyInfo =
new ConcurrentDictionary<string, PropertyInfo>();

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)
);
var property = CachedPropertyInfo.GetOrAdd(
entity.GetType().FullName + checkForDisableGuidGenerationAttribute, () =>
{
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)
{
return;
}
if (idProperty == null)
{
return null;
}

if (checkForDisableGuidGenerationAttribute)
{
if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{
return;
}
}
if (checkForDisableGuidGenerationAttribute)
{
if (idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{
return null;
}
}

return idProperty;
});

idProperty.SetValue(entity, idFactory());
property?.SetValue(entity, idFactory());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using Shouldly;
using Xunit;

Expand Down Expand Up @@ -33,6 +34,15 @@ public static void SetId_DisablesIdGeneration()
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>
{

Expand All @@ -53,5 +63,14 @@ private class MyEntityDisablesIdGeneration : Entity<Guid>
[DisableIdGeneration]
public override Guid Id { get; protected set; }
}

public class NewIdEntity : Entity<Guid>
{
public new Guid Id
{
get => base.Id;
set => base.Id = value;
}
}
}
}