| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Query; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// The columns an <c>AuditedExecuteUpdate[Async]</c> sets, besides the audit fields. One shape for both EF versions: EF |
| | | 10 | | /// expression over <c>SetPropertyCalls<T></c>, EF 10 an <c>Action<UpdateSettersBuilder<T>></c>; this |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 13 | | public sealed class AuditedUpdateSetters< TEntity > |
| | | 14 | | where TEntity : class |
| | | 15 | | { |
| | | 16 | | #if NET10_0_OR_GREATER |
| | 12 | 17 | | private readonly List<Action<UpdateSettersBuilder<TEntity>>> _setters = []; |
| | | 18 | | #else |
| | 12 | 19 | | private readonly List<Func<Expression, Expression>> _setters = []; |
| | | 20 | | #endif |
| | | 21 | | |
| | | 22 | | |
| | 48 | 23 | | internal AuditedUpdateSetters() { } |
| | | 24 | | |
| | | 25 | | |
| | | 26 | | /// <summary>Sets <paramref name="property"/> to <paramref name="value"/> (sent as a parameter).</summary> |
| | | 27 | | /// <typeparam name="TProperty">The property type.</typeparam> |
| | | 28 | | /// <param name="property">The property, e.g. <c>o => o.Status</c>.</param> |
| | | 29 | | /// <param name="value">The new value.</param> |
| | | 30 | | /// <returns>These setters.</returns> |
| | | 31 | | /// <exception cref="ArgumentNullException"><paramref name="property"/> is <see langword="null"/>.</exception> |
| | | 32 | | public AuditedUpdateSetters<TEntity> SetProperty< TProperty >( Expression<Func<TEntity, TProperty>> property, TPrope |
| | | 33 | | { |
| | 116 | 34 | | ArgumentNullException.ThrowIfNull( property ); |
| | | 35 | | |
| | | 36 | | #if NET10_0_OR_GREATER |
| | 58 | 37 | | _setters.Add( builder => builder.SetProperty( property, value ) ); |
| | | 38 | | #else |
| | | 39 | | // A field of a constant box, so EF sends a parameter instead of inlining a literal. |
| | 58 | 40 | | Expression boxed = Expression.Field( Expression.Constant( new StrongBox<TProperty>( value ) ), nameof( StrongBox |
| | | 41 | | |
| | 58 | 42 | | _setters.Add( calls => Expression.Call( calls, Ef9.SetValue.MakeGenericMethod( typeof( TProperty ) ), property, |
| | | 43 | | #endif |
| | | 44 | | |
| | 116 | 45 | | return this; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Sets <paramref name="property"/> from an expression over the row, e.g. <c>o => o.Count + 1</c>.</sum |
| | | 49 | | /// <typeparam name="TProperty">The property type.</typeparam> |
| | | 50 | | /// <param name="property">The property.</param> |
| | | 51 | | /// <param name="value">The value expression.</param> |
| | | 52 | | /// <returns>These setters.</returns> |
| | | 53 | | /// <exception cref="ArgumentNullException">An argument is <see langword="null"/>.</exception> |
| | | 54 | | public AuditedUpdateSetters<TEntity> SetProperty< TProperty >( Expression<Func<TEntity, TProperty>> property, Expres |
| | | 55 | | { |
| | 4 | 56 | | ArgumentNullException.ThrowIfNull( property ); |
| | 4 | 57 | | ArgumentNullException.ThrowIfNull( value ); |
| | | 58 | | |
| | | 59 | | #if NET10_0_OR_GREATER |
| | 2 | 60 | | _setters.Add( builder => builder.SetProperty( property, value ) ); |
| | | 61 | | #else |
| | 2 | 62 | | _setters.Add( calls => Expression.Call( calls, Ef9.SetExpression.MakeGenericMethod( typeof( TProperty ) ), prope |
| | | 63 | | #endif |
| | | 64 | | |
| | 4 | 65 | | return this; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | |
| | | 69 | | /// <summary>Adds <c>row.Property = value</c> for an audit column whose type is only known at run time.</summary> |
| | | 70 | | internal void SetAuditValue( PropertyInfo property, object? value ) |
| | | 71 | | { |
| | 108 | 72 | | var row = Expression.Parameter( typeof( TEntity ), "e" ); |
| | 108 | 73 | | var lambda = Expression.Lambda( typeof( Func<,> ).MakeGenericType( typeof( TEntity ), property.PropertyType ), E |
| | | 74 | | |
| | 108 | 75 | | SetValueMethod.MakeGenericMethod( property.PropertyType ).Invoke( this, [ lambda, value ] ); |
| | 108 | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | internal int Count => _setters.Count; |
| | | 79 | | |
| | | 80 | | #if NET10_0_OR_GREATER |
| | 4 | 81 | | internal int Execute( IQueryable<TEntity> query ) => query.ExecuteUpdate( Apply ); |
| | | 82 | | |
| | 8 | 83 | | internal Task<int> ExecuteAsync( IQueryable<TEntity> query, CancellationToken cancellationToken ) => query.ExecuteUp |
| | | 84 | | |
| | | 85 | | private void Apply( UpdateSettersBuilder<TEntity> builder ) |
| | | 86 | | { |
| | 204 | 87 | | foreach ( var setter in _setters ) { setter( builder ); } |
| | 12 | 88 | | } |
| | | 89 | | #else |
| | 4 | 90 | | internal int Execute( IQueryable<TEntity> query ) => query.ExecuteUpdate( Build() ); |
| | | 91 | | |
| | 8 | 92 | | internal Task<int> ExecuteAsync( IQueryable<TEntity> query, CancellationToken cancellationToken ) => query.ExecuteUp |
| | | 93 | | |
| | | 94 | | private Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> Build() |
| | | 95 | | { |
| | 12 | 96 | | var calls = Expression.Parameter( typeof( SetPropertyCalls<TEntity> ), "s" ); |
| | 12 | 97 | | Expression body = calls; |
| | | 98 | | |
| | 204 | 99 | | foreach ( var setter in _setters ) { body = setter( body ); } |
| | | 100 | | |
| | | 101 | | |
| | 12 | 102 | | return Expression.Lambda<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>>( body, calls ); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// The two EF 9 <c>SetPropertyCalls<T>.SetProperty<P></c> overloads: ( Func<T, P>, P ) and ( Func |
| | | 106 | | private static class Ef9 |
| | | 107 | | { |
| | 4 | 108 | | internal static readonly MethodInfo SetValue = Find( valueIsExpression: false ); |
| | | 109 | | |
| | 4 | 110 | | internal static readonly MethodInfo SetExpression = Find( valueIsExpression: true ); |
| | | 111 | | |
| | | 112 | | |
| | | 113 | | private static MethodInfo Find( bool valueIsExpression ) |
| | 8 | 114 | | => typeof( SetPropertyCalls<TEntity> ).GetMethods() |
| | 8 | 115 | | .Single( m => m.Name == nameof( SetPropertyCalls<TEntity>.SetProperty |
| | 8 | 116 | | && m.GetParameters().Length == 2 |
| | 8 | 117 | | && m.GetParameters()[ 1 ].ParameterType.IsGenericParamet |
| | | 118 | | } |
| | | 119 | | #endif |
| | | 120 | | |
| | 8 | 121 | | private static readonly MethodInfo SetValueMethod = typeof( AuditedUpdateSetters<TEntity> ).GetMethods() |
| | 8 | 122 | | .Single( m => m.Name |
| | 8 | 123 | | && m.Ge |
| | | 124 | | } |