Skip to content

Commit

Permalink
Migrated Infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Aug 31, 2024
1 parent 26becfb commit ad1d1dd
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>LinkDotNet.Blog.Infrastructure</AssemblyName>
<RootNamespace>LinkDotNet.Blog.Infrastructure</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ public CachedRepository(IRepository<T> repository, IMemoryCache memoryCache)
public ValueTask<HealthCheckResult> PerformHealthCheckAsync() => repository.PerformHealthCheckAsync();

public async ValueTask<T> GetByIdAsync(string id) =>
await memoryCache.GetOrCreateAsync(id, async entry =>
(await memoryCache.GetOrCreateAsync(id, async entry =>
{
entry.SlidingExpiration = TimeSpan.FromDays(7);
return await repository.GetByIdAsync(id);
});
}))!;

public async ValueTask<IPagedList<T>> GetAllAsync(
Expression<Func<T, bool>> filter = null,
Expression<Func<T, object>> orderBy = null,
public async ValueTask<IPagedList<T>> GetAllAsync(Expression<Func<T, bool>>? filter = null,
Expression<Func<T, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
await repository.GetAllAsync(filter, orderBy, descending, page, pageSize);

public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<T, TProjection>> selector,
Expression<Func<T, bool>> filter = null,
Expression<Func<T, object>> orderBy = null,
Expression<Func<T, TProjection>>? selector,
Expression<Func<T, bool>>? filter = null,
Expression<Func<T, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
Expand Down
11 changes: 5 additions & 6 deletions src/LinkDotNet.Blog.Infrastructure/Persistence/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ public interface IRepository<TEntity>
ValueTask<TEntity> GetByIdAsync(string id);

ValueTask<IPagedList<TEntity>> GetAllAsync(
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity,
object>> orderBy = null,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue);

ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
Expression<Func<TEntity, TProjection>>? selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ public sealed class Repository<TEntity> : IRepository<TEntity>

public ValueTask<TEntity> GetByIdAsync(string id)
{
var entity = entities.SingleOrDefault(b => b.Id == id);
var entity = entities.First(b => b.Id == id);
return new ValueTask<TEntity>(entity);
}

public ValueTask<IPagedList<TEntity>> GetAllAsync(
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
public ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);

public ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
Expression<Func<TEntity, TProjection>>? selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ public async ValueTask<TEntity> GetByIdAsync(string id)
return await result.FirstOrDefaultAsync();
}

public async ValueTask<IPagedList<TEntity>> GetAllAsync(
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
public async ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
await GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);

public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
Expression<Func<TEntity, TProjection>>? selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ public async ValueTask<TEntity> GetByIdAsync(string id)
return await session.LoadAsync<TEntity>(id);
}

public ValueTask<IPagedList<TEntity>> GetAllAsync(
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
public ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);

public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
Expression<Func<TEntity, TProjection>>? selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
Expand Down
15 changes: 7 additions & 8 deletions src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ public async ValueTask<HealthCheckResult> PerformHealthCheckAsync()
public async ValueTask<TEntity> GetByIdAsync(string id)
{
var blogDbContext = await dbContextFactory.CreateDbContextAsync();
return await blogDbContext.Set<TEntity>().SingleOrDefaultAsync(b => b.Id == id);
return await blogDbContext.Set<TEntity>().FirstAsync(b => b.Id == id);
}

public ValueTask<IPagedList<TEntity>> GetAllAsync(
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
public ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);

public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>> filter = null,
Expression<Func<TEntity, object>> orderBy = null,
Expression<Func<TEntity, TProjection>>? selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
Expand Down Expand Up @@ -125,7 +124,7 @@ async Task DeleteBulkAsyncInBatchesAsync()
var currentBatchIds = idList.Skip(batch * batchSize).Take(batchSize).ToList();

await blogDbContext.Set<TEntity>()
.Where(s => currentBatchIds.Contains(s.Id))
.Where(s => currentBatchIds.Contains(s.Id!))
.ExecuteDeleteAsync();

LogDeleteBatch(batch + 1, (batch + 1) * batchSize);
Expand Down
3 changes: 1 addition & 2 deletions src/LinkDotNet.Blog.Web/Features/Home/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,5 @@
=> BlogPostRepository.GetAllAsync(
p => p.IsPublished,
b => b.UpdatedDate,
pageSize: AppConfiguration.Value.BlogPostsPerPage,
page: page);
page: page, pageSize: AppConfiguration.Value.BlogPostsPerPage);
}
2 changes: 1 addition & 1 deletion src/LinkDotNet.Blog.Web/Features/Search/SearchPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
var term = Uri.UnescapeDataString(SearchTerm);
blogPosts = await BlogPostRepository.GetAllAsync(t => t.IsPublished && (t.Title.Contains(term)
|| t.Tags.Any(tag => tag == term)),
|| t.Tags.Any(tag => tag == term)),
b => b.UpdatedDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ private sealed class SlowRepository : IRepository<BlogPost>

public ValueTask<BlogPost> GetByIdAsync(string id) => throw new NotImplementedException();

public ValueTask<IPagedList<BlogPost>> GetAllAsync(
Expression<Func<BlogPost, bool>> filter = null,
public ValueTask<IPagedList<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null,
Expression<Func<BlogPost, object>> orderBy = null,
bool descending = true,
int page = 1,
Expand Down

0 comments on commit ad1d1dd

Please sign in to comment.