Class Repository<T>
Generic repository providing CRUD for entity type T.
Uses compiled materializers via CL.MySQL2.Core.EntityMetadata<T> — no per-row reflection.
public sealed class Repository<T> where T : class, new()
Type Parameters
T
- Inheritance
-
Repository<T>
- Inherited Members
Constructors
Repository(ConnectionManager, ILogger?, TransactionScope, int, int)
public Repository(ConnectionManager connectionManager, ILogger? logger, TransactionScope transactionScope, int slowQueryThresholdMs = 1000, int maxBatchInsertSize = 500)
Parameters
connectionManagerConnectionManagerloggerILoggertransactionScopeTransactionScopeslowQueryThresholdMsintmaxBatchInsertSizeint
Repository(ConnectionManager, ILogger?, string, int, int)
public Repository(ConnectionManager connectionManager, ILogger? logger = null, string connectionId = "Default", int slowQueryThresholdMs = 1000, int maxBatchInsertSize = 500)
Parameters
connectionManagerConnectionManagerloggerILoggerconnectionIdstringslowQueryThresholdMsintmaxBatchInsertSizeint
Methods
AdjustAsync<TProperty>(object, Expression<Func<T, TProperty>>, TProperty, CancellationToken)
Atomically adjusts a numeric column by delta. Negative for decrement.
public Task<Result<int>> AdjustAsync<TProperty>(object id, Expression<Func<T, TProperty>> propertySelector, TProperty delta, CancellationToken ct = default)
Parameters
idobjectpropertySelectorExpression<Func<T, TProperty>>deltaTPropertyctCancellationToken
Returns
Type Parameters
TProperty
CountAsync(CancellationToken)
Returns the total row count for the table.
public Task<Result<long>> CountAsync(CancellationToken ct = default)
Parameters
Returns
DecrementAsync<TProperty>(object, Expression<Func<T, TProperty>>, TProperty, CancellationToken)
Decrement a numeric column by amount.
public Task<Result<int>> DecrementAsync<TProperty>(object id, Expression<Func<T, TProperty>> propertySelector, TProperty amount, CancellationToken ct = default)
Parameters
idobjectpropertySelectorExpression<Func<T, TProperty>>amountTPropertyctCancellationToken
Returns
Type Parameters
TProperty
DeleteAsync(object, CancellationToken)
Deletes an entity by its primary key. For a SoftDeleteAttribute entity this is a soft delete — it sets the timestamp column to UtcNow instead of removing the row (no-op if already deleted). Use HardDeleteAsync(object, CancellationToken) to remove the row physically. Returns true when a row was affected.
public Task<Result<bool>> DeleteAsync(object id, CancellationToken ct = default)
Parameters
idobjectctCancellationToken
Returns
FindAsync(Expression<Func<T, bool>>, CancellationToken)
Returns all entities matching the given LINQ predicate.
public Task<Result<List<T>>> FindAsync(Expression<Func<T, bool>> predicate, CancellationToken ct = default)
Parameters
predicateExpression<Func<T, bool>>ctCancellationToken
Returns
GetAllAsync(CancellationToken)
Retrieves all entities in the table.
public Task<Result<List<T>>> GetAllAsync(CancellationToken ct = default)
Parameters
Returns
GetByColumnAsync(string, object, CancellationToken)
Retrieves all entities where the specified column equals the given value.
public Task<Result<List<T>>> GetByColumnAsync(string column, object value, CancellationToken ct = default)
Parameters
columnstringvalueobjectctCancellationToken
Returns
GetByIdAsync(object, CancellationToken)
Retrieves an entity by its primary key value.
public Task<Result<T?>> GetByIdAsync(object id, CancellationToken ct = default)
Parameters
idobjectctCancellationToken
Returns
- Task<Result<T>>
GetPagedAsync(int, int, string?, bool, CancellationToken)
Retrieves a paged result set.
public Task<Result<PagedResult<T>>> GetPagedAsync(int page, int pageSize, string? orderByColumn = null, bool descending = false, CancellationToken ct = default)
Parameters
pageintpageSizeintorderByColumnstringdescendingboolctCancellationToken
Returns
- Task<Result<PagedResult<T>>>
HardDeleteAsync(object, CancellationToken)
Physically removes the row by primary key, even for soft-delete entities.
public Task<Result<bool>> HardDeleteAsync(object id, CancellationToken ct = default)
Parameters
idobjectctCancellationToken
Returns
IncrementAsync<TProperty>(object, Expression<Func<T, TProperty>>, TProperty, CancellationToken)
Increment a numeric column by amount.
public Task<Result<int>> IncrementAsync<TProperty>(object id, Expression<Func<T, TProperty>> propertySelector, TProperty amount, CancellationToken ct = default)
Parameters
idobjectpropertySelectorExpression<Func<T, TProperty>>amountTPropertyctCancellationToken
Returns
Type Parameters
TProperty
InsertAsync(T, CancellationToken)
Inserts a single entity and returns it (with auto-generated PK populated).
public Task<Result<T>> InsertAsync(T entity, CancellationToken ct = default)
Parameters
entityTctCancellationToken
Returns
- Task<Result<T>>
InsertManyAsync(IEnumerable<T>, CancellationToken)
Bulk-inserts a collection of entities using real batched INSERT statements.
Batches of up to maxBatchInsertSize (default 500) are sent per round-trip.
public Task<Result<int>> InsertManyAsync(IEnumerable<T> entities, CancellationToken ct = default)
Parameters
entitiesIEnumerable<T>ctCancellationToken
Returns
UpdateAsync(T, CancellationToken)
Updates an existing entity by PK.
public Task<Result<T>> UpdateAsync(T entity, CancellationToken ct = default)
Parameters
entityTctCancellationToken
Returns
- Task<Result<T>>
UpsertAsync(T, CancellationToken)
Inserts a single entity, or updates all non-auto-PK columns to the entity's values
if a UNIQUE/PRIMARY-KEY conflict occurs (set semantics). Issues
INSERT ... AS new ON DUPLICATE KEY UPDATE (MySQL 8.0.20+ alias syntax).
On a new insert the auto-PK is refreshed from LAST_INSERT_ID(); on a pure
update the entity's existing PK value is preserved.
public Task<Result<T>> UpsertAsync(T entity, CancellationToken ct = default)
Parameters
entityTctCancellationToken
Returns
- Task<Result<T>>
UpsertManyAsync(IEnumerable<T>, CancellationToken)
Bulk-upserts a collection of entities using batched
INSERT ... ON DUPLICATE KEY UPDATE statements (set semantics).
Batches of up to maxBatchInsertSize (default 500) are sent per round-trip.
Returns the total rows-affected count (MySQL counts 1 for each insert and 2 for each
update, so this is not equal to entities.Count).
public Task<Result<int>> UpsertManyAsync(IEnumerable<T> entities, CancellationToken ct = default)
Parameters
entitiesIEnumerable<T>ctCancellationToken
Returns
UpsertWithIncrementsAsync(T, IReadOnlyList<string>, IReadOnlyList<string>?, CancellationToken)
Inserts insertSeed if no UNIQUE/PRIMARY-KEY conflict occurs;
otherwise applies increment / set semantics to the listed properties on conflict.
Properties NOT listed in either array are insert-only — present in the
VALUES clause but absent from ON DUPLICATE KEY UPDATE (so they don't
change on conflict — useful for created_utc style columns). Property names
resolve through CL.MySQL2.Core.EntityMetadata<T> so callers can use
nameof(...) for compile-time-safe column references.
public Task<Result<int>> UpsertWithIncrementsAsync(T insertSeed, IReadOnlyList<string> incrementProperties, IReadOnlyList<string>? setProperties = null, CancellationToken ct = default)
Parameters
insertSeedTRow to INSERT if no conflict. All non-auto-PK columns go into the
VALUESclause.incrementPropertiesIReadOnlyList<string>C# property names whose columns should
col = col + new.colon conflict.setPropertiesIReadOnlyList<string>C# property names whose columns should
col = new.colon conflict. Defaults to empty.ctCancellationTokenCancellation token.
Returns
- Task<Result<int>>
Rows affected (MySQL: 1 = insert, 2 = update with changes, 0 = update with no change).
Exceptions
- ArgumentException
A name in
incrementPropertiesorsetPropertiesdoes not resolve to a property onT, refers to an auto-increment PK, or appears in both arrays.