From b01c0717a3a3fe8c2317ed87e6562b033501b2cc Mon Sep 17 00:00:00 2001 From: Callum Ellis Date: Thu, 31 Oct 2024 15:15:37 +0000 Subject: [PATCH] Use discard for nameof generic arguments --- .../BaseChangeEventQueryPropertyBuilder.cs | 29 +++++++++++++++---- .../ChangeEventQueryPropertyBuilder``.cs | 4 +-- ...geSourceChangeEventQueryPropertyBuilder.cs | 2 +- ...hangedByChangeEventQueryPropertyBuilder.cs | 2 +- .../IChangeEventQueryPropertyBuilder.cs | 2 +- .../EntityTypeBuilderExtensionsInternal.cs | 12 ++++---- src/EFCore.ChangeTriggers/_.cs | 6 ++++ 7 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/EFCore.ChangeTriggers/_.cs diff --git a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/BaseChangeEventQueryPropertyBuilder.cs b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/BaseChangeEventQueryPropertyBuilder.cs index 65c1a30..654c6ee 100644 --- a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/BaseChangeEventQueryPropertyBuilder.cs +++ b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/BaseChangeEventQueryPropertyBuilder.cs @@ -6,7 +6,7 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.Builders.PropertyBuilders { - public abstract class BaseChangeEventQueryPropertyBuilder : IChangeEventQueryPropertyBuilder + internal abstract class BaseChangeEventQueryPropertyBuilder : IChangeEventQueryPropertyBuilder where TChangeEvent : ChangeEvent { private readonly IQueryable query; @@ -33,14 +33,33 @@ public BaseChangeEventQueryPropertyBuilder(IQueryable query) cpJoinParam = Expression.Parameter(cpGenericType, "cp"); cpJoinProps = new() { - Current = Expression.Property(cpJoinParam, nameof(ChangePair.Current)), - Previous = Expression.Property(cpJoinParam, nameof(ChangePair.Previous)) + Current = Expression.Property(cpJoinParam, nameof(ChangePair<_>.Current)), + Previous = Expression.Property(cpJoinParam, nameof(ChangePair<_>.Previous)) }; changedAtProp = query.ElementType.GetProperty(nameof(IChange.ChangedAt))!; foreignKeyEqualityCondition = BuildForeignKeyEqualityCondition(); } + /// + /// this.query + /// .Where(pc => pc.ChangedAt < c.ChangedAt) + /// .OrderByDescending(pc => pc.ChangedAt) + /// .Take(1), + /// (c, pc) => new ChangePair + /// { + /// 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 BuildChangeEventQuery(LambdaExpression valueSelector) { var selectors = new ChangePair @@ -89,8 +108,8 @@ private MethodCallExpression BuildSelectMany(Expression innerExpression) var joinedChangesInit = Expression.MemberInit( Expression.New(cpConstructor), - Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair.Current))!, changeParams.Current), - Expression.Bind(cpGenericType.GetProperty(nameof(ChangePair.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); diff --git a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeEventQueryPropertyBuilder``.cs b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeEventQueryPropertyBuilder``.cs index d1830a4..3897586 100644 --- a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeEventQueryPropertyBuilder``.cs +++ b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeEventQueryPropertyBuilder``.cs @@ -15,8 +15,8 @@ public ChangeEventQueryPropertyBuilder(IQueryable query) : base(query) protected override IEnumerable GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity) { - yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy.ChangedBy))); - yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource.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))); } } } \ No newline at end of file diff --git a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeSourceChangeEventQueryPropertyBuilder.cs b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeSourceChangeEventQueryPropertyBuilder.cs index 1afcc4b..91a90c2 100644 --- a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeSourceChangeEventQueryPropertyBuilder.cs +++ b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangeSourceChangeEventQueryPropertyBuilder.cs @@ -15,7 +15,7 @@ public ChangeSourceChangeEventQueryPropertyBuilder(IQueryable query) : base(quer protected override IEnumerable GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity) { - yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource.ChangeSource))); + yield return BuildChangeEventPropertyBinding(ce => ce.ChangeSource, Expression.Property(changeEntity, nameof(IHasChangeSource<_>.ChangeSource))); } } } \ No newline at end of file diff --git a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangedByChangeEventQueryPropertyBuilder.cs b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangedByChangeEventQueryPropertyBuilder.cs index 4731556..10fb43a 100644 --- a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangedByChangeEventQueryPropertyBuilder.cs +++ b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/ChangedByChangeEventQueryPropertyBuilder.cs @@ -15,7 +15,7 @@ public ChangedByChangeEventQueryPropertyBuilder(IQueryable query) : base(query) protected override IEnumerable GetAdditionalChangeEventPropertyBindings(MemberExpression changeEntity) { - yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy.ChangedBy))); + yield return BuildChangeEventPropertyBinding(ce => ce.ChangedBy, Expression.Property(changeEntity, nameof(IHasChangedBy<_>.ChangedBy))); } } } \ No newline at end of file diff --git a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/IChangeEventQueryPropertyBuilder.cs b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/IChangeEventQueryPropertyBuilder.cs index 1cfa09d..0178546 100644 --- a/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/IChangeEventQueryPropertyBuilder.cs +++ b/src/EFCore.ChangeTriggers.ChangeEventQueries/Builders/PropertyBuilders/IChangeEventQueryPropertyBuilder.cs @@ -2,7 +2,7 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.Builders.PropertyBuilders { - public interface IChangeEventQueryPropertyBuilder + internal interface IChangeEventQueryPropertyBuilder { IQueryable BuildChangeEventQuery(LambdaExpression valueSelector); } diff --git a/src/EFCore.ChangeTriggers/Metadata/Builders/EntityTypeBuilderExtensionsInternal.cs b/src/EFCore.ChangeTriggers/Metadata/Builders/EntityTypeBuilderExtensionsInternal.cs index ebfc89f..075f925 100644 --- a/src/EFCore.ChangeTriggers/Metadata/Builders/EntityTypeBuilderExtensionsInternal.cs +++ b/src/EFCore.ChangeTriggers/Metadata/Builders/EntityTypeBuilderExtensionsInternal.cs @@ -18,8 +18,8 @@ public static EntityTypeBuilder HasChangeTriggerInternal(this EntityTypeBuilder ExceptionStrings.NoPrimaryKeyConfigured(builder.Metadata.DisplayName())); builder - .HasMany(nameof(ITracked.Changes)) - .WithOne(nameof(IHasTrackedEntity.TrackedEntity)) + .HasMany(nameof(ITracked<_>.Changes)) + .WithOne(nameof(IHasTrackedEntity<_>.TrackedEntity)) .HasForeignKey(trackedTablePrimaryKey.Properties.Select(p => p.Name).ToArray()) .IsTrackedEntityForeignKey(); @@ -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.ChangedBy); + const string changedByPropertyName = nameof(IHasChangedBy<_>.ChangedBy); builder.EnsureSameClrType(changedByPropertyName, changedByClrType); @@ -65,7 +65,7 @@ public static EntityTypeBuilder HasChangedByInternal(this EntityTypeBuilder buil else { // Configure ChangedBy as a scalar property - builder.Property(nameof(IHasChangedBy.ChangedBy)) + builder.Property(nameof(IHasChangedBy<_>.ChangedBy)) .IsChangedByProperty(); } @@ -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.ChangeSource); + const string changeSourcePropertyName = nameof(IHasChangeSource<_>.ChangeSource); builder.EnsureSameClrType(changeSourcePropertyName, changeSourceClrType); @@ -93,7 +93,7 @@ public static EntityTypeBuilder HasChangeSourceInternal(this EntityTypeBuilder b else { // Configure ChangeSource as a scalar property - builder.Property(nameof(IHasChangeSource.ChangeSource)) + builder.Property(nameof(IHasChangeSource<_>.ChangeSource)) .IsChangeSourceProperty(); } diff --git a/src/EFCore.ChangeTriggers/_.cs b/src/EFCore.ChangeTriggers/_.cs new file mode 100644 index 0000000..a0e29e0 --- /dev/null +++ b/src/EFCore.ChangeTriggers/_.cs @@ -0,0 +1,6 @@ +namespace EFCore.ChangeTriggers +{ + internal struct _ + { + } +}