Skip to content

Commit 82ae32b

Browse files
committed
Improve extensibility by virtualising repo methods
1 parent 6727c98 commit 82ae32b

2 files changed

Lines changed: 17 additions & 55 deletions

File tree

QueryKit.Repositories/BaseEntityReadRepository.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected BaseEntityReadRepository(IConnectionFactory factory)
5959
}
6060

6161
/// <inheritdoc />
62-
public async Task<TEntity?> GetByIdAsync(TKey id, CancellationToken cancellationToken = default)
62+
public virtual async Task<TEntity?> GetByIdAsync(TKey id, CancellationToken cancellationToken = default)
6363
{
6464
if (id is null)
6565
{
@@ -71,7 +71,7 @@ protected BaseEntityReadRepository(IConnectionFactory factory)
7171
}
7272

7373
/// <inheritdoc />
74-
public async Task<PageResult<TEntity>> GetListPagedAsync(FilterOptions? filter = null, SortOptions? sort = null,
74+
public virtual async Task<PageResult<TEntity>> GetListPagedAsync(FilterOptions? filter = null, SortOptions? sort = null,
7575
PageOptions? paging = null, bool includeDeleted = false, CancellationToken cancellationToken = default)
7676
{
7777
var (whereSql, parameters) = QuerySqlBuilder.BuildWhere<TEntity>(filter);
@@ -102,7 +102,7 @@ public async Task<PageResult<TEntity>> GetListPagedAsync(FilterOptions? filter =
102102
}
103103

104104
/// <inheritdoc />
105-
public async Task<IList<TEntity>> GetListAsync(FilterOptions? filter = null, SortOptions? sort = null,
105+
public virtual async Task<IList<TEntity>> GetListAsync(FilterOptions? filter = null, SortOptions? sort = null,
106106
bool includeDeleted = false,
107107
CancellationToken cancellationToken = default)
108108
{
@@ -137,7 +137,7 @@ public async Task<IList<TEntity>> GetListAsync(FilterOptions? filter = null, Sor
137137
}
138138

139139
/// <inheritdoc />
140-
public async Task<bool> IsUniqueIncludingDeletedAsync(string columnName, string? value,
140+
public virtual async Task<bool> IsUniqueIncludingDeletedAsync(string columnName, string? value,
141141
CancellationToken cancellationToken = default)
142142
{
143143
var resolved = ResolveColumnOrNull(columnName);
@@ -166,7 +166,7 @@ public async Task<bool> IsUniqueIncludingDeletedAsync(string columnName, string?
166166
}
167167

168168
/// <inheritdoc />
169-
public async Task<bool> IsUniqueExcludingDeletedAsync(string columnName, string? value,
169+
public virtual async Task<bool> IsUniqueExcludingDeletedAsync(string columnName, string? value,
170170
CancellationToken cancellationToken = default)
171171
{
172172
var resolved = ResolveColumnOrNull(columnName);
@@ -206,7 +206,7 @@ public async Task<bool> IsUniqueExcludingDeletedAsync(string columnName, string?
206206
/// <summary>
207207
/// Gets a single entity using the provided SQL and parameters.
208208
/// </summary>
209-
protected async Task<TEntity?> GetAsync(string sql, object? parameters, CancellationToken cancellationToken = default)
209+
protected virtual async Task<TEntity?> GetAsync(string sql, object? parameters, CancellationToken cancellationToken = default)
210210
{
211211
using var conn = await OpenConnection(cancellationToken);
212212
var cmd = new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
@@ -216,7 +216,7 @@ public async Task<bool> IsUniqueExcludingDeletedAsync(string columnName, string?
216216
/// <summary>
217217
/// Gets a list of entities using the provided SQL and parameters.
218218
/// </summary>
219-
protected async Task<IList<TEntity>> GetListAsync(string sql, object? parameters, CancellationToken cancellationToken = default)
219+
protected virtual async Task<IList<TEntity>> GetListAsync(string sql, object? parameters, CancellationToken cancellationToken = default)
220220
{
221221
using var conn = await OpenConnection(cancellationToken);
222222
var cmd = new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
@@ -227,7 +227,7 @@ protected async Task<IList<TEntity>> GetListAsync(string sql, object? parameters
227227
/// <summary>
228228
/// Gets a single record of type <typeparamref name="T"/> using the provided SQL and parameters.
229229
/// </summary>
230-
protected async Task<T?> GetAsync<T>(string sql, object? parameters, CancellationToken cancellationToken = default)
230+
protected virtual async Task<T?> GetAsync<T>(string sql, object? parameters, CancellationToken cancellationToken = default)
231231
{
232232
using var conn = await OpenConnection(cancellationToken);
233233
var cmd = new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
@@ -237,7 +237,7 @@ protected async Task<IList<TEntity>> GetListAsync(string sql, object? parameters
237237
/// <summary>
238238
/// Gets a list of records of type <typeparamref name="T"/> using the provided SQL and parameters.
239239
/// </summary>
240-
protected async Task<IList<T>> GetListAsync<T>(string sql, object? parameters, CancellationToken cancellationToken = default)
240+
protected virtual async Task<IList<T>> GetListAsync<T>(string sql, object? parameters, CancellationToken cancellationToken = default)
241241
{
242242
using var conn = await OpenConnection(cancellationToken);
243243
var cmd = new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
@@ -248,7 +248,7 @@ protected async Task<IList<T>> GetListAsync<T>(string sql, object? parameters, C
248248
/// <summary>
249249
/// Gets a paged list of records of type <typeparamref name="T"/> using the provided SQL, parameters, and optional filtering, sorting, and paging options.
250250
/// </summary>
251-
protected async Task<PageResult<T>> GetListPagedAsync<T>(string sql, object? parameters, FilterOptions? filter, SortOptions? sort, PageOptions? paging,
251+
protected virtual async Task<PageResult<T>> GetListPagedAsync<T>(string sql, object? parameters, FilterOptions? filter, SortOptions? sort, PageOptions? paging,
252252
bool includeDeleted = false, CancellationToken cancellationToken = default)
253253
{
254254
paging ??= new PageOptions();
@@ -292,7 +292,7 @@ protected async Task<PageResult<T>> GetListPagedAsync<T>(string sql, object? par
292292
/// <summary>
293293
/// Gets a paged list of records of type <typeparamref name="T"/> using the provided SQL for data retrieval and counting, along with parameters and paging options.
294294
/// </summary>
295-
protected async Task<PageResult<T>> GetListPagedAsync<T>(
295+
protected virtual async Task<PageResult<T>> GetListPagedAsync<T>(
296296
string dataSql,
297297
string countSql,
298298
object? parameters,
@@ -317,7 +317,7 @@ protected async Task<PageResult<T>> GetListPagedAsync<T>(
317317
/// Override to customize connection behavior.
318318
/// </summary>
319319
/// <returns>An open <see cref="IDbConnection"/>.</returns>
320-
protected async Task<IDbConnection> OpenConnection(CancellationToken cancellationToken = default)
320+
protected virtual async Task<IDbConnection> OpenConnection(CancellationToken cancellationToken = default)
321321
{
322322
var conn = _factory.Create();
323323

@@ -336,7 +336,7 @@ protected async Task<IDbConnection> OpenConnection(CancellationToken cancellatio
336336
/// <summary>
337337
/// Gets a paged list of <typeparamref name="TEntity"/> using the provided WHERE clause, ORDER BY clause, parameters, and paging options.
338338
/// </summary>
339-
protected async Task<PageResult<TEntity>> GetPagedAsync(string whereSql, string orderBy, object? parameters,
339+
protected virtual async Task<PageResult<TEntity>> GetPagedAsync(string whereSql, string orderBy, object? parameters,
340340
int page, int pageSize, CancellationToken cancellationToken = default)
341341
{
342342
using var conn = await OpenConnection(cancellationToken);

QueryKit.Repositories/BaseEntityRepository.cs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,29 @@ protected BaseEntityRepository(IConnectionFactory factory) : base(factory)
3131
}
3232

3333
/// <inheritdoc/>
34-
public async Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
34+
public virtual async Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
3535
{
3636
using var conn = await OpenConnection(cancellationToken);
3737

3838
var result = await conn.InsertAsync<TKey, TEntity>(entity, cancellationToken: cancellationToken);
3939

4040
if (result != null) entity.Id = result;
41-
42-
await OnInsertAsync(entity, cancellationToken);
4341

4442
return entity;
4543
}
4644

4745
/// <inheritdoc/>
48-
public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
46+
public virtual async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
4947
{
5048
using var conn = await OpenConnection(cancellationToken);
5149

5250
await conn.UpdateAsync(entity, cancellationToken: cancellationToken);
5351

54-
await OnUpdateAsync(entity, cancellationToken);
55-
5652
return entity;
5753
}
5854

5955
/// <inheritdoc/>
60-
public async Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
56+
public virtual async Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
6157
{
6258
if (IsNewEntity(entity))
6359
{
@@ -68,7 +64,7 @@ public async Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken
6864
}
6965

7066
/// <inheritdoc/>
71-
public async Task<bool> DeleteAsync(TKey id, CancellationToken cancellationToken = default)
67+
public virtual async Task<bool> DeleteAsync(TKey id, CancellationToken cancellationToken = default)
7268
{
7369
if (EqualityComparer<TKey>.Default.Equals(id, default!))
7470
{
@@ -87,22 +83,13 @@ public async Task<bool> DeleteAsync(TKey id, CancellationToken cancellationToken
8783
if (SoftDeleteProp is null)
8884
{
8985
var affected = await conn.DeleteAsync<TEntity>(id, cancellationToken: cancellationToken);
90-
if (affected > 0)
91-
{
92-
await OnDeleteAsync(entity, false, cancellationToken);
93-
}
9486
return affected > 0;
9587
}
9688

9789
SoftDeleteProp.SetValue(entity, true);
9890

9991
var rows = await conn.UpdateAsync(entity, cancellationToken: cancellationToken);
10092

101-
if (rows > 0)
102-
{
103-
await OnDeleteAsync(entity, true, cancellationToken);
104-
}
105-
10693
return rows > 0;
10794
}
10895

@@ -139,29 +126,4 @@ protected bool IsNewEntity(TEntity entity)
139126
{
140127
return EqualityComparer<TKey>.Default.Equals(entity.Id, default!);
141128
}
142-
143-
/// <summary>
144-
/// Custom logic to execute after an entity is inserted.
145-
/// </summary>
146-
protected virtual async Task OnInsertAsync(TEntity entity, CancellationToken cancellationToken = default)
147-
{
148-
await Task.CompletedTask;
149-
}
150-
151-
/// <summary>
152-
/// Custom logic to execute after an entity is updated.
153-
/// </summary>
154-
protected virtual async Task OnUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
155-
{
156-
await Task.CompletedTask;
157-
}
158-
159-
/// <summary>
160-
/// Custom logic to execute after an entity is deleted.
161-
/// </summary>
162-
protected virtual async Task OnDeleteAsync(TEntity entity, bool wasSoftDelete,
163-
CancellationToken cancellationToken = default)
164-
{
165-
await Task.CompletedTask;
166-
}
167129
}

0 commit comments

Comments
 (0)