Skip to content

Commit

Permalink
Use discard for nameof generic arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
codemunkie15 committed Oct 31, 2024
1 parent 87dbbae commit b01c071
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace EFCore.ChangeTriggers.ChangeEventQueries.Builders.PropertyBuilders
{
public abstract class BaseChangeEventQueryPropertyBuilder<TChangeEvent> : IChangeEventQueryPropertyBuilder<TChangeEvent>
internal abstract class BaseChangeEventQueryPropertyBuilder<TChangeEvent> : IChangeEventQueryPropertyBuilder<TChangeEvent>
where TChangeEvent : ChangeEvent
{
private readonly IQueryable query;
Expand All @@ -33,14 +33,33 @@ public BaseChangeEventQueryPropertyBuilder(IQueryable query)
cpJoinParam = Expression.Parameter(cpGenericType, "cp");
cpJoinProps = new()
{
Current = Expression.Property(cpJoinParam, nameof(ChangePair<object>.Current)),
Previous = Expression.Property(cpJoinParam, nameof(ChangePair<object>.Previous))
Current = Expression.Property(cpJoinParam, nameof(ChangePair<_>.Current)),
Previous = Expression.Property(cpJoinParam, nameof(ChangePair<_>.Previous))
};

changedAtProp = query.ElementType.GetProperty(nameof(IChange.ChangedAt))!;
foreignKeyEqualityCondition = BuildForeignKeyEqualityCondition();
}

/// <![CDATA[
/// this.query
/// .SelectMany(c =>
/// this.query
/// .Where(pc => pc.ChangedAt < c.ChangedAt)
/// .OrderByDescending(pc => pc.ChangedAt)
/// .Take(1),
/// (c, pc) => new ChangePair<TChange>
/// {
/// Current = c,
/// Previous = pc
/// })
/// .Where(cp => cp.Current.Property != cp.Previous.Property)
/// .Select(cp => new ChangeEvent
/// {
/// ChangedAt = cp.Current.ChangedAt,
/// OldValue = cp.Previous.Property,
/// NewValue = cp.Current.Property
/// });
public IQueryable<TChangeEvent> BuildChangeEventQuery(LambdaExpression valueSelector)
{
var selectors = new ChangePair<Expression>
Expand Down Expand Up @@ -89,8 +108,8 @@ private MethodCallExpression BuildSelectMany(Expression innerExpression)

var joinedChangesInit = Expression.MemberInit(
Expression.New(cpConstructor),
Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair<object>.Current))!, changeParams.Current),
Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair<object>.Previous))!, changeParams.Previous)
Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair<_>.Current))!, changeParams.Current),
Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair<_>.Previous))!, changeParams.Previous)
);

var resultSelector = Expression.Lambda(joinedChangesInit, changeParams.Current, changeParams.Previous);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public ChangeEventQueryPropertyBuilder(IQueryable query) : base(query)

protected override IEnumerable<MemberBinding> GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity)
{
yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy<object>.ChangedBy)));
yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource<object>.ChangeSource)));
yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy<_>.ChangedBy)));
yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource<_>.ChangeSource)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ChangeSourceChangeEventQueryPropertyBuilder(IQueryable query) : base(quer

protected override IEnumerable<MemberBinding> GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity)
{
yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource<object>.ChangeSource)));
yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource<_>.ChangeSource)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ChangedByChangeEventQueryPropertyBuilder(IQueryable query) : base(query)

protected override IEnumerable<MemberBinding> GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity)
{
yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy<object>.ChangedBy)));
yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy<_>.ChangedBy)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EFCore.ChangeTriggers.ChangeEventQueries.Builders.PropertyBuilders
{
public interface IChangeEventQueryPropertyBuilder<TChangeEvent>
internal interface IChangeEventQueryPropertyBuilder<TChangeEvent>
{
IQueryable<TChangeEvent> BuildChangeEventQuery(LambdaExpression valueSelector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public static EntityTypeBuilder HasChangeTriggerInternal(this EntityTypeBuilder
ExceptionStrings.NoPrimaryKeyConfigured(builder.Metadata.DisplayName()));

builder
.HasMany(nameof(ITracked<object>.Changes))
.WithOne(nameof(IHasTrackedEntity<object>.TrackedEntity))
.HasMany(nameof(ITracked<_>.Changes))
.WithOne(nameof(IHasTrackedEntity<_>.TrackedEntity))
.HasForeignKey(trackedTablePrimaryKey.Properties.Select(p => p.Name).ToArray())
.IsTrackedEntityForeignKey();

Expand All @@ -46,7 +46,7 @@ public static EntityTypeBuilder IsChangeTableInternal(this EntityTypeBuilder bui

public static EntityTypeBuilder HasChangedByInternal(this EntityTypeBuilder builder, Type changedByClrType)
{
const string changedByPropertyName = nameof(IHasChangedBy<object>.ChangedBy);
const string changedByPropertyName = nameof(IHasChangedBy<_>.ChangedBy);

builder.EnsureSameClrType(changedByPropertyName, changedByClrType);

Expand All @@ -65,7 +65,7 @@ public static EntityTypeBuilder HasChangedByInternal(this EntityTypeBuilder buil
else
{
// Configure ChangedBy as a scalar property
builder.Property(nameof(IHasChangedBy<object>.ChangedBy))
builder.Property(nameof(IHasChangedBy<_>.ChangedBy))
.IsChangedByProperty();
}

Expand All @@ -74,7 +74,7 @@ public static EntityTypeBuilder HasChangedByInternal(this EntityTypeBuilder buil

public static EntityTypeBuilder HasChangeSourceInternal(this EntityTypeBuilder builder, Type changeSourceClrType)
{
const string changeSourcePropertyName = nameof(IHasChangeSource<object>.ChangeSource);
const string changeSourcePropertyName = nameof(IHasChangeSource<_>.ChangeSource);

builder.EnsureSameClrType(changeSourcePropertyName, changeSourceClrType);

Expand All @@ -93,7 +93,7 @@ public static EntityTypeBuilder HasChangeSourceInternal(this EntityTypeBuilder b
else
{
// Configure ChangeSource as a scalar property
builder.Property(nameof(IHasChangeSource<object>.ChangeSource))
builder.Property(nameof(IHasChangeSource<_>.ChangeSource))
.IsChangeSourceProperty();
}

Expand Down
6 changes: 6 additions & 0 deletions src/EFCore.ChangeTriggers/_.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EFCore.ChangeTriggers
{
internal struct _
{
}
}

0 comments on commit b01c071

Please sign in to comment.