|
| 1 | +using EfCoreKit.Abstractions.Interfaces; |
1 | 2 |
|
| 3 | +namespace EfCoreKit.Core.Context; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Configuration options for EfCoreKit features. |
| 7 | +/// Use the fluent methods to enable features before passing to <see cref="ServiceCollectionExtensions"/>. |
| 8 | +/// </summary> |
| 9 | +public sealed class EfCoreKitOptions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Gets a value indicating whether soft delete is enabled for entities implementing <see cref="ISoftDeletable"/>. |
| 13 | + /// </summary> |
| 14 | + public bool SoftDeleteEnabled { get; private set; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Gets a value indicating whether cascade soft delete is enabled. |
| 18 | + /// </summary> |
| 19 | + public bool CascadeSoftDeleteEnabled { get; private set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Gets a value indicating whether audit trail is enabled for entities implementing <see cref="IAuditable"/>. |
| 23 | + /// </summary> |
| 24 | + public bool AuditTrailEnabled { get; private set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets a value indicating whether full audit logging to a dedicated table is enabled. |
| 28 | + /// </summary> |
| 29 | + public bool FullAuditLogEnabled { get; private set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets a value indicating whether multi-tenancy is enabled for entities implementing <see cref="ITenantEntity"/>. |
| 33 | + /// </summary> |
| 34 | + public bool MultiTenancyEnabled { get; private set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets the type implementing <see cref="IUserProvider"/>. |
| 38 | + /// </summary> |
| 39 | + public Type? UserProviderType { get; private set; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets the type implementing <see cref="ITenantProvider"/>. |
| 43 | + /// </summary> |
| 44 | + public Type? TenantProviderType { get; private set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the threshold for logging slow queries. <c>null</c> disables slow query logging. |
| 48 | + /// </summary> |
| 49 | + public TimeSpan? SlowQueryThreshold { get; private set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Enables soft delete for entities implementing <see cref="ISoftDeletable"/>. |
| 53 | + /// </summary> |
| 54 | + /// <param name="cascade">Whether to enable cascade soft delete for related entities.</param> |
| 55 | + /// <returns>This instance for chaining.</returns> |
| 56 | + public EfCoreKitOptions EnableSoftDelete(bool cascade = false) |
| 57 | + { |
| 58 | + SoftDeleteEnabled = true; |
| 59 | + CascadeSoftDeleteEnabled = cascade; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Enables audit trail for entities implementing <see cref="IAuditable"/>. |
| 65 | + /// </summary> |
| 66 | + /// <param name="fullLog">Whether to enable full change logging to a dedicated audit table.</param> |
| 67 | + /// <returns>This instance for chaining.</returns> |
| 68 | + public EfCoreKitOptions EnableAuditTrail(bool fullLog = false) |
| 69 | + { |
| 70 | + AuditTrailEnabled = true; |
| 71 | + FullAuditLogEnabled = fullLog; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Enables multi-tenancy for entities implementing <see cref="ITenantEntity"/>. |
| 77 | + /// </summary> |
| 78 | + /// <returns>This instance for chaining.</returns> |
| 79 | + public EfCoreKitOptions EnableMultiTenancy() |
| 80 | + { |
| 81 | + MultiTenancyEnabled = true; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Registers the <see cref="IUserProvider"/> implementation to use for audit trail. |
| 87 | + /// </summary> |
| 88 | + /// <typeparam name="T">The concrete type implementing <see cref="IUserProvider"/>.</typeparam> |
| 89 | + /// <returns>This instance for chaining.</returns> |
| 90 | + public EfCoreKitOptions UseUserProvider<T>() where T : class, IUserProvider |
| 91 | + { |
| 92 | + UserProviderType = typeof(T); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Registers the <see cref="ITenantProvider"/> implementation to use for multi-tenancy. |
| 98 | + /// </summary> |
| 99 | + /// <typeparam name="T">The concrete type implementing <see cref="ITenantProvider"/>.</typeparam> |
| 100 | + /// <returns>This instance for chaining.</returns> |
| 101 | + public EfCoreKitOptions UseTenantProvider<T>() where T : class, ITenantProvider |
| 102 | + { |
| 103 | + TenantProviderType = typeof(T); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Enables slow query logging for queries exceeding the given threshold. |
| 109 | + /// </summary> |
| 110 | + /// <param name="threshold">The duration threshold.</param> |
| 111 | + /// <returns>This instance for chaining.</returns> |
| 112 | + public EfCoreKitOptions LogSlowQueries(TimeSpan threshold) |
| 113 | + { |
| 114 | + SlowQueryThreshold = threshold; |
| 115 | + return this; |
| 116 | + } |
| 117 | +} |
0 commit comments