Skip to content

Commit

Permalink
refactor: Smaller code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Feb 17, 2024
1 parent 7d3b741 commit 7713d8b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/LinkDotNet.Blog.Infrastructure/PagedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace LinkDotNet.Blog.Infrastructure;
[DebuggerDisplay("PagedList<{typeof(T).Name}>, Count = {Count}")]
public sealed class PagedList<T> : IPagedList<T>
{
public static readonly PagedList<T> Empty = new(Enumerable.Empty<T>(), 0, 0, 0);
public static readonly PagedList<T> Empty = new([], 0, 0, 0);

private readonly IReadOnlyList<T> subset;
private readonly int totalPages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async ValueTask DeleteAsync(string id)
}
}

public async ValueTask DeleteBulkAsync(IEnumerable<string> ids) => await repository.DeleteBulkAsync(ids);
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids) => await repository.DeleteBulkAsync(ids);

public async ValueTask StoreBulkAsync(IEnumerable<T> records) => await repository.StoreBulkAsync(records);
public async ValueTask StoreBulkAsync(IReadOnlyCollection<T> records) => await repository.StoreBulkAsync(records);
}
4 changes: 2 additions & 2 deletions src/LinkDotNet.Blog.Infrastructure/Persistence/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(

ValueTask DeleteAsync(string id);

ValueTask DeleteBulkAsync(IEnumerable<string> ids);
ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids);

ValueTask StoreBulkAsync(IEnumerable<TEntity> records);
ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ValueTask DeleteAsync(string id)
return ValueTask.CompletedTask;
}

public ValueTask DeleteBulkAsync(IEnumerable<string> ids)
public ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
ArgumentNullException.ThrowIfNull(ids);

Expand All @@ -99,7 +99,7 @@ public ValueTask DeleteBulkAsync(IEnumerable<string> ids)
return ValueTask.CompletedTask;
}

public ValueTask StoreBulkAsync(IEnumerable<TEntity> records)
public ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
entities.AddRange(records);
return ValueTask.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ public async ValueTask DeleteAsync(string id)
await Collection.DeleteOneAsync(filter);
}

public async ValueTask DeleteBulkAsync(IEnumerable<string> ids)
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
var filter = Builders<TEntity>.Filter.In(doc => doc.Id, ids);
await Collection.DeleteManyAsync(filter);
}

public async ValueTask StoreBulkAsync(IEnumerable<TEntity> records)
public async ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
if (records.Any())
ArgumentNullException.ThrowIfNull(records);

if (records.Count != 0)
{
await Collection.InsertManyAsync(records);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async ValueTask DeleteAsync(string id)
await session.SaveChangesAsync();
}

public async ValueTask DeleteBulkAsync(IEnumerable<string> ids)
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
ArgumentNullException.ThrowIfNull(ids);

Expand All @@ -103,7 +103,7 @@ public async ValueTask DeleteBulkAsync(IEnumerable<string> ids)
await session.SaveChangesAsync();
}

public async ValueTask StoreBulkAsync(IEnumerable<TEntity> records)
public async ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
ArgumentNullException.ThrowIfNull(records);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public async ValueTask DeleteAsync(string id)
}
}

public async ValueTask DeleteBulkAsync(IEnumerable<string> ids)
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
var blogDbContext = await dbContextFactory.CreateDbContextAsync();
var strategy = blogDbContext.Database.CreateExecutionStrategy();

await strategy.ExecuteAsync(async () => await DeleteBulkAsyncInBatchesAsync());
await strategy.ExecuteAsync(DeleteBulkAsyncInBatchesAsync);

async Task DeleteBulkAsyncInBatchesAsync()
{
Expand All @@ -137,14 +137,14 @@ await blogDbContext.Set<TEntity>()
}
}

public async ValueTask StoreBulkAsync(IEnumerable<TEntity> records)
public async ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
ArgumentNullException.ThrowIfNull(records);

var blogDbContext = await dbContextFactory.CreateDbContextAsync();
var strategy = blogDbContext.Database.CreateExecutionStrategy();

await strategy.ExecuteAsync(async () => await StoreBulkAsyncInBatchesAsync());
await strategy.ExecuteAsync(StoreBulkAsyncInBatchesAsync);

async Task StoreBulkAsyncInBatchesAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ private async Task TransformRecordsAsync()

var mergedRecords = MergeRecords(newBlogPostRecords, oldBlogPostRecords);

await blogPostRecordRepository.DeleteBulkAsync(oldBlogPostRecords.Select(o => o.Id));
await blogPostRecordRepository.StoreBulkAsync(mergedRecords);
await blogPostRecordRepository.DeleteBulkAsync(oldBlogPostRecords.Select(o => o.Id).ToArray());
await blogPostRecordRepository.StoreBulkAsync(mergedRecords.ToArray());

LogDeletingUserRecords(userRecords.Count);
await userRecordRepository.DeleteBulkAsync(userRecords.Select(u => u.Id));
await userRecordRepository.DeleteBulkAsync(userRecords.Select(u => u.Id).ToArray());
LogDeletedUserRecords();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProject

public ValueTask DeleteAsync(string id) => throw new NotImplementedException();

public ValueTask DeleteBulkAsync(IEnumerable<string> ids) => throw new NotImplementedException();
public ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids) => throw new NotImplementedException();

public ValueTask StoreBulkAsync(IEnumerable<BlogPost> records) => throw new NotImplementedException();
public ValueTask StoreBulkAsync(IReadOnlyCollection<BlogPost> records) => throw new NotImplementedException();
}
}

0 comments on commit 7713d8b

Please sign in to comment.