| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Builders; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Seed settings for one entity type, from <c>options.Entity<TEntity>()</c> or an <see cref="ISeedEntityConfigura |
| | | 5 | | /// These win over the Domain attributes, which win over the Domain interfaces. Setting the same thing twice: the last o |
| | | 6 | | /// CLI warns). Every method returns the builder. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 9 | | public sealed class SeedEntityBuilder< TEntity > |
| | | 10 | | where TEntity : class |
| | | 11 | | { |
| | | 12 | | private readonly SeedEntitySettings _settings; |
| | | 13 | | |
| | | 14 | | |
| | 86 | 15 | | internal SeedEntityBuilder( SeedEntitySettings settings ) => _settings = settings; |
| | | 16 | | |
| | | 17 | | |
| | | 18 | | /// <summary>Identifies rows by a business key instead of the primary key. It must be backed by a unique index.</sum |
| | | 19 | | /// <param name="properties">The key properties, e.g. <c>x => x.IsoCode</c>.</param> |
| | | 20 | | /// <returns>This builder.</returns> |
| | | 21 | | /// <exception cref="ArgumentException">No property, or an expression that isn't a simple property access.</exceptio |
| | | 22 | | public SeedEntityBuilder<TEntity> MatchBy( params Expression<Func<TEntity, object?>>[] properties ) |
| | | 23 | | { |
| | 27 | 24 | | ArgumentNullException.ThrowIfNull( properties ); |
| | | 25 | | |
| | 30 | 26 | | if ( properties.Length == 0 ) { throw new ArgumentException( "MatchBy needs at least one property.", nameof( pro |
| | | 27 | | |
| | | 28 | | |
| | 24 | 29 | | _settings.SetMatchBy( properties.Select( PropertyName ).ToList() ); |
| | | 30 | | |
| | 21 | 31 | | return this; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Marks the soft-delete flag (<see langword="true"/> = deleted).</summary> |
| | | 35 | | /// <param name="property">The <see cref="bool"/> property.</param> |
| | | 36 | | /// <returns>This builder.</returns> |
| | | 37 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 38 | | public SeedEntityBuilder<TEntity> SoftDeleteFlag( Expression<Func<TEntity, bool>> property ) => Role( SeedRole.SoftD |
| | | 39 | | |
| | | 40 | | /// <summary>Marks the property that holds when the row was soft-deleted.</summary> |
| | | 41 | | /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ |
| | | 42 | | /// <param name="property">The property.</param> |
| | | 43 | | /// <returns>This builder.</returns> |
| | | 44 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 45 | | public SeedEntityBuilder<TEntity> DeletedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 46 | | |
| | | 47 | | /// <summary>Marks the property that holds the key of the user who soft-deleted the row.</summary> |
| | | 48 | | /// <typeparam name="TProperty">The property type (the system user's key type, or its nullable form).</typeparam> |
| | | 49 | | /// <param name="property">The property.</param> |
| | | 50 | | /// <returns>This builder.</returns> |
| | | 51 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 52 | | public SeedEntityBuilder<TEntity> DeletedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 53 | | |
| | | 54 | | /// <summary>Marks the property that holds the name of the user who soft-deleted the row.</summary> |
| | | 55 | | /// <param name="property">The <see cref="string"/> property.</param> |
| | | 56 | | /// <returns>This builder.</returns> |
| | | 57 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 58 | | public SeedEntityBuilder<TEntity> DeletedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Del |
| | | 59 | | |
| | | 60 | | /// <summary>Marks the property that holds when the row was created.</summary> |
| | | 61 | | /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ |
| | | 62 | | /// <param name="property">The property.</param> |
| | | 63 | | /// <returns>This builder.</returns> |
| | | 64 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 12 | 65 | | public SeedEntityBuilder<TEntity> CreatedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 66 | | |
| | | 67 | | /// <summary>Marks the property that holds the key of the user who created the row.</summary> |
| | | 68 | | /// <typeparam name="TProperty">The property type (the system user's key type).</typeparam> |
| | | 69 | | /// <param name="property">The property.</param> |
| | | 70 | | /// <returns>This builder.</returns> |
| | | 71 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 72 | | public SeedEntityBuilder<TEntity> CreatedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 73 | | |
| | | 74 | | /// <summary>Marks the property that holds the name of the user who created the row.</summary> |
| | | 75 | | /// <param name="property">The <see cref="string"/> property.</param> |
| | | 76 | | /// <returns>This builder.</returns> |
| | | 77 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 78 | | public SeedEntityBuilder<TEntity> CreatedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Cre |
| | | 79 | | |
| | | 80 | | /// <summary>Marks the property that holds when the row was last updated.</summary> |
| | | 81 | | /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ |
| | | 82 | | /// <param name="property">The property.</param> |
| | | 83 | | /// <returns>This builder.</returns> |
| | | 84 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 3 | 85 | | public SeedEntityBuilder<TEntity> UpdatedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 86 | | |
| | | 87 | | /// <summary>Marks the property that holds the key of the user who last updated the row.</summary> |
| | | 88 | | /// <typeparam name="TProperty">The property type (the system user's key type, or its nullable form).</typeparam> |
| | | 89 | | /// <param name="property">The property.</param> |
| | | 90 | | /// <returns>This builder.</returns> |
| | | 91 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 92 | | public SeedEntityBuilder<TEntity> UpdatedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role( |
| | | 93 | | |
| | | 94 | | /// <summary>Marks the property that holds the name of the user who last updated the row.</summary> |
| | | 95 | | /// <param name="property">The <see cref="string"/> property.</param> |
| | | 96 | | /// <returns>This builder.</returns> |
| | | 97 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | 0 | 98 | | public SeedEntityBuilder<TEntity> UpdatedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Upd |
| | | 99 | | |
| | | 100 | | /// <summary>Leaves a property out of seed data: never exported, compared or written (like <c>[SeedIgnore]</c>).</su |
| | | 101 | | /// <typeparam name="TProperty">The property type.</typeparam> |
| | | 102 | | /// <param name="property">The property.</param> |
| | | 103 | | /// <returns>This builder.</returns> |
| | | 104 | | /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception> |
| | | 105 | | public SeedEntityBuilder<TEntity> Ignore< TProperty >( Expression<Func<TEntity, TProperty>> property ) |
| | | 106 | | { |
| | 3 | 107 | | _settings.Ignore( PropertyName( property ) ); |
| | | 108 | | |
| | 3 | 109 | | return this; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary>How rows of this entity are removed. Default: <see cref="SeedDeleteMode.Auto"/>; a run can override it. |
| | | 113 | | /// <param name="mode">The mode.</param> |
| | | 114 | | /// <returns>This builder.</returns> |
| | | 115 | | public SeedEntityBuilder<TEntity> DeleteMode( SeedDeleteMode mode ) |
| | | 116 | | { |
| | 22 | 117 | | _settings.SetDeleteMode( mode ); |
| | | 118 | | |
| | 22 | 119 | | return this; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | |
| | | 123 | | private SeedEntityBuilder<TEntity> Role( SeedRole role, LambdaExpression property ) |
| | | 124 | | { |
| | 15 | 125 | | _settings.SetRole( role, PropertyName( property ) ); |
| | | 126 | | |
| | 15 | 127 | | return this; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static string PropertyName( LambdaExpression expression ) |
| | | 131 | | { |
| | 42 | 132 | | ArgumentNullException.ThrowIfNull( expression ); |
| | | 133 | | |
| | 42 | 134 | | var body = expression.Body is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecke |
| | | 135 | | |
| | 42 | 136 | | return body is MemberExpression { Member: PropertyInfo property } member && member.Expression == expression.Para |
| | 42 | 137 | | ? property.Name |
| | 42 | 138 | | : throw new ArgumentException( $"'{expression}' must be a property of the entity, e.g. x => x.Name.", |
| | | 139 | | } |
| | | 140 | | } |