Skip to content

Commit

Permalink
Added cancellation token support
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanHair committed Sep 21, 2023
1 parent 787cf57 commit 4401cf2
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public virtual Task<bool> Exists(Expression<Func<T, bool>> predicate, Cancellati

public virtual Task AddAsync(IEnumerable<T> objects, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

if(objects != null && objects.Any())
{
Entities.AddRange(objects);
Expand All @@ -76,6 +79,9 @@ public virtual Task AddAsync(IEnumerable<T> objects, CancellationToken cancellat

public virtual Task DeleteAsync(IEnumerable<T> objects, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

if(objects.Count() > 1)
{
Entities.RemoveRange(objects);
Expand All @@ -94,6 +100,9 @@ public virtual Task DeleteAsync(IEnumerable<T> objects, CancellationToken cancel

public virtual Task UpdateAsync(IEnumerable<T> objects, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

if (objects is null || objects.Any() == false)
return Task.CompletedTask;

Expand All @@ -103,40 +112,55 @@ public virtual Task UpdateAsync(IEnumerable<T> objects, CancellationToken cancel

public async Task AddAsync(T entity, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return;

if (entity is null)
return;

await Entities.AddAsync(entity);
await Db.SaveChangesAsync();
await Db.SaveChangesAsync(cancellationToken);
}

public Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

if (entity is null)
return Task.CompletedTask;

Entities.Update(entity);
return Db.SaveChangesAsync();
return Db.SaveChangesAsync(cancellationToken);
}

public Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

if (entity is null)
return Task.CompletedTask;

Entities.Remove(entity);
return Db.SaveChangesAsync();
return Db.SaveChangesAsync(cancellationToken);
}

public Task DeleteWhereAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
{
if(cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

Entities.RemoveRange(Entities.Where(predicate));
return Db.SaveChangesAsync();
return Db.SaveChangesAsync(cancellationToken);
}

public Task ApplyChanges(CancellationToken cancellationToken = default)
{
return Db.SaveChangesAsync();
if (cancellationToken.IsCancellationRequested)
return Task.CompletedTask;

return Db.SaveChangesAsync(cancellationToken);
}
}
}

0 comments on commit 4401cf2

Please sign in to comment.