Skip to content

Commit 58612fe

Browse files
committed
Adapt ValueTask in Repository
1 parent 740c4a9 commit 58612fe

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

LinkDotNet.Blog.Infrastructure/Persistence/CachedRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public CachedRepository(IRepository<T> repository, IMemoryCache memoryCache)
2828
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(7),
2929
};
3030

31-
public async Task<T> GetByIdAsync(string id)
31+
public async ValueTask<T> GetByIdAsync(string id)
3232
{
3333
if (!memoryCache.TryGetValue(id, out T value))
3434
{
@@ -39,7 +39,7 @@ public async Task<T> GetByIdAsync(string id)
3939
return value;
4040
}
4141

42-
public async Task<IPagedList<T>> GetAllAsync(
42+
public async ValueTask<IPagedList<T>> GetAllAsync(
4343
Expression<Func<T, bool>> filter = null,
4444
Expression<Func<T, object>> orderBy = null,
4545
bool descending = true,
@@ -54,14 +54,14 @@ public async Task<IPagedList<T>> GetAllAsync(
5454
});
5555
}
5656

57-
public async Task StoreAsync(T entity)
57+
public async ValueTask StoreAsync(T entity)
5858
{
5959
await repository.StoreAsync(entity);
6060
ResetCache();
6161
memoryCache.Set(entity.Id, entity, Options);
6262
}
6363

64-
public async Task DeleteAsync(string id)
64+
public async ValueTask DeleteAsync(string id)
6565
{
6666
ResetCache();
6767
memoryCache.Remove(id);

LinkDotNet.Blog.Infrastructure/Persistence/IRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ namespace LinkDotNet.Blog.Infrastructure.Persistence;
99
public interface IRepository<TEntity>
1010
where TEntity : Entity
1111
{
12-
Task<TEntity> GetByIdAsync(string id);
12+
ValueTask<TEntity> GetByIdAsync(string id);
1313

14-
Task<IPagedList<TEntity>> GetAllAsync(
14+
ValueTask<IPagedList<TEntity>> GetAllAsync(
1515
Expression<Func<TEntity, bool>> filter = null,
1616
Expression<Func<TEntity,
1717
object>> orderBy = null,
1818
bool descending = true,
1919
int page = 1,
2020
int pageSize = int.MaxValue);
2121

22-
Task StoreAsync(TEntity entity);
22+
ValueTask StoreAsync(TEntity entity);
2323

24-
Task DeleteAsync(string id);
24+
ValueTask DeleteAsync(string id);
2525
}

LinkDotNet.Blog.Infrastructure/Persistence/InMemory/Repository.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public class Repository<TEntity> : IRepository<TEntity>
1313
{
1414
private readonly List<TEntity> entities = new();
1515

16-
public Task<TEntity> GetByIdAsync(string id)
16+
public ValueTask<TEntity> GetByIdAsync(string id)
1717
{
1818
var entity = entities.SingleOrDefault(b => b.Id == id);
19-
return Task.FromResult(entity);
19+
return new ValueTask<TEntity>(entity);
2020
}
2121

22-
public Task<IPagedList<TEntity>> GetAllAsync(
22+
public ValueTask<IPagedList<TEntity>> GetAllAsync(
2323
Expression<Func<TEntity, bool>> filter = null,
2424
Expression<Func<TEntity, object>> orderBy = null,
2525
bool descending = true,
@@ -39,10 +39,10 @@ public Task<IPagedList<TEntity>> GetAllAsync(
3939
: result.OrderBy(orderBy.Compile());
4040
}
4141

42-
return Task.FromResult(result.ToPagedList(page, pageSize));
42+
return new ValueTask<IPagedList<TEntity>>(result.ToPagedList(page, pageSize));
4343
}
4444

45-
public Task StoreAsync(TEntity entity)
45+
public ValueTask StoreAsync(TEntity entity)
4646
{
4747
if (string.IsNullOrEmpty(entity.Id))
4848
{
@@ -56,17 +56,17 @@ public Task StoreAsync(TEntity entity)
5656
}
5757

5858
entities.Add(entity);
59-
return Task.CompletedTask;
59+
return ValueTask.CompletedTask;
6060
}
6161

62-
public Task DeleteAsync(string id)
62+
public ValueTask DeleteAsync(string id)
6363
{
6464
var blogPostToDelete = entities.SingleOrDefault(b => b.Id == id);
6565
if (blogPostToDelete != null)
6666
{
6767
entities.Remove(blogPostToDelete);
6868
}
6969

70-
return Task.CompletedTask;
70+
return ValueTask.CompletedTask;
7171
}
7272
}

LinkDotNet.Blog.Infrastructure/Persistence/RavenDb/Repository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public Repository(IDocumentStore documentStore)
1818
this.documentStore = documentStore;
1919
}
2020

21-
public async Task<TEntity> GetByIdAsync(string id)
21+
public async ValueTask<TEntity> GetByIdAsync(string id)
2222
{
2323
using var session = documentStore.OpenAsyncSession();
2424
return await session.LoadAsync<TEntity>(id);
2525
}
2626

27-
public async Task<IPagedList<TEntity>> GetAllAsync(
27+
public async ValueTask<IPagedList<TEntity>> GetAllAsync(
2828
Expression<Func<TEntity, bool>> filter = null,
2929
Expression<Func<TEntity, object>> orderBy = null,
3030
bool descending = true,
@@ -49,14 +49,14 @@ public async Task<IPagedList<TEntity>> GetAllAsync(
4949
return await query.ToPagedListAsync(page, pageSize);
5050
}
5151

52-
public async Task StoreAsync(TEntity entity)
52+
public async ValueTask StoreAsync(TEntity entity)
5353
{
5454
using var session = documentStore.OpenAsyncSession();
5555
await session.StoreAsync(entity);
5656
await session.SaveChangesAsync();
5757
}
5858

59-
public async Task DeleteAsync(string id)
59+
public async ValueTask DeleteAsync(string id)
6060
{
6161
using var session = documentStore.OpenAsyncSession();
6262
session.Delete(id);

LinkDotNet.Blog.Infrastructure/Persistence/Sql/Repository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public Repository(BlogDbContext blogDbContext)
1818
this.blogDbContext = blogDbContext;
1919
}
2020

21-
public async Task<TEntity> GetByIdAsync(string id)
21+
public async ValueTask<TEntity> GetByIdAsync(string id)
2222
{
2323
return await blogDbContext.Set<TEntity>().SingleOrDefaultAsync(b => b.Id == id);
2424
}
2525

26-
public async Task<IPagedList<TEntity>> GetAllAsync(
26+
public async ValueTask<IPagedList<TEntity>> GetAllAsync(
2727
Expression<Func<TEntity, bool>> filter = null,
2828
Expression<Func<TEntity, object>> orderBy = null,
2929
bool descending = true,
@@ -47,7 +47,7 @@ public async Task<IPagedList<TEntity>> GetAllAsync(
4747
return await entity.ToPagedListAsync(page, pageSize);
4848
}
4949

50-
public async Task StoreAsync(TEntity entity)
50+
public async ValueTask StoreAsync(TEntity entity)
5151
{
5252
if (string.IsNullOrEmpty(entity.Id))
5353
{
@@ -61,7 +61,7 @@ public async Task StoreAsync(TEntity entity)
6161
await blogDbContext.SaveChangesAsync();
6262
}
6363

64-
public async Task DeleteAsync(string id)
64+
public async ValueTask DeleteAsync(string id)
6565
{
6666
var entityToDelete = await GetByIdAsync(id);
6767
if (entityToDelete != null)

0 commit comments

Comments
 (0)