Skip to content
Merged

#27 #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ManagedCode.Repository.AzureTable/BaseAzureTableRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ protected override async Task<TItem> GetAsyncInternal(Expression<Func<TItem, boo
var item = await _tableAdapter.Query<TItem>(predicate, take: 1, cancellationToken: token).ToListAsync(token);
return item.FirstOrDefault();
}

protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default)
{
return _tableAdapter.Query<TItem>(null, take: take, skip: skip, cancellationToken: token);
}

protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy, Order orderType, int? take = null, int skip = 0, CancellationToken token = default)
{
return _tableAdapter.Query(null, orderBy, orderType, take: take, skip: skip, cancellationToken: token);
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
<Description>Repository for AzureTable</Description>
</PropertyGroup>

Expand Down
26 changes: 26 additions & 0 deletions ManagedCode.Repository.Core/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,35 @@ public Task<TItem> GetAsync(Expression<Func<TItem, bool>> predicate, Cancellatio
return GetAsyncInternal(predicate, token);
}

public IAsyncEnumerable<TItem> GetAllAsync(int? take = null, int skip = 0, CancellationToken token = default)
{
return GetAllAsyncInternal(take, skip, token);
}

public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy, int? take = null, int skip = 0, CancellationToken token = default)
{
return GetAllAsyncInternal(orderBy, Order.By, take, skip, token);
}

public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy, Order orderType,
int? take = null,
int skip = 0,
CancellationToken token = default)
{
return GetAllAsyncInternal(orderBy, orderType, take, skip, token);
}

protected abstract Task<TItem> GetAsyncInternal(TId id, CancellationToken token = default);
protected abstract Task<TItem> GetAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);

protected abstract IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default);

protected abstract IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy,
Order orderType,
int? take = null,
int skip = 0,
CancellationToken token = default);

#endregion

#region Find
Expand Down
16 changes: 16 additions & 0 deletions ManagedCode.Repository.Core/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public interface IRepository<in TId, TItem> where TItem : IItem<TId>

Task<TItem> GetAsync(TId id, CancellationToken token = default);
Task<TItem> GetAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);

IAsyncEnumerable<TItem> GetAllAsync(
int? take = null,
int skip = 0,
CancellationToken token = default);

IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy,
int? take = null,
int skip = 0,
CancellationToken token = default);

IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy,
Order orderType,
int? take = null,
int skip = 0,
CancellationToken token = default);

IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate, int? take = null, int skip = 0, CancellationToken token = default);

Expand Down
49 changes: 49 additions & 0 deletions ManagedCode.Repository.Core/InMemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,55 @@ protected override Task<TItem> GetAsyncInternal(Expression<Func<TItem, bool>> pr
}
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default)
{
await Task.Yield();
lock (_storage)
{
var enumerable = _storage.Values.Skip(skip);

if (take.HasValue)
{
enumerable = enumerable.Take(take.Value);
}

foreach (var item in enumerable)
{
yield return item;
}
}
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy, Order orderType, int? take = null, int skip = 0, CancellationToken token = default)
{
await Task.Yield();
lock (_storage)
{

IEnumerable<TItem> enumerable;
if (orderType == Order.By)
{
enumerable = _storage.Values.OrderBy(orderBy.Compile());
}
else
{
enumerable = _storage.Values.OrderByDescending(orderBy.Compile());
}

enumerable = enumerable.Skip(0);

if (take.HasValue)
{
enumerable = enumerable.Take(take.Value);
}

foreach (var item in enumerable)
{
yield return item;
}
}
}

#endregion

#region Find
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/managed-code-hub/Repository</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
<Description>Base implementation for Repository</Description>
</PropertyGroup>

Expand Down
69 changes: 69 additions & 0 deletions ManagedCode.Repository.CosmosDB/CosmosDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,75 @@ protected override async Task<TItem> GetAsyncInternal(Expression<Func<TItem, boo

return null;
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, [EnumeratorCancellation] CancellationToken token = default)
{
var container = await _cosmosDbAdapter.GetContainer();
var query = container.GetItemLinqQueryable<TItem>().Where(SplitByType());

if (skip > 0)
{
query = query.Skip(skip);
}

if (take.HasValue)
{
query = query.Take(take.Value);
}

var feedIterator = query.ToFeedIterator();
using (var iterator = feedIterator)
{
while (iterator.HasMoreResults)
{
token.ThrowIfCancellationRequested();

foreach (var item in await iterator.ReadNextAsync(token))
{
yield return item;
}
}
}
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy, Order orderType, int? take = null, int skip = 0, [EnumeratorCancellation] CancellationToken token = default)
{
var container = await _cosmosDbAdapter.GetContainer();
var query = container.GetItemLinqQueryable<TItem>().Where(SplitByType());

if (orderType == Order.By)
{
query = query.OrderBy(orderBy);
}
else
{
query = query.OrderByDescending(orderBy);
}

if (skip > 0)
{
query = query.Skip(skip);
}

if (take.HasValue)
{
query = query.Take(take.Value);
}

var feedIterator = query.ToFeedIterator();
using (var iterator = feedIterator)
{
while (iterator.HasMoreResults)
{
token.ThrowIfCancellationRequested();

foreach (var item in await iterator.ReadNextAsync(token))
{
yield return item;
}
}
}
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
<Description>Repository for CosmosDB</Description>
</PropertyGroup>

Expand Down
55 changes: 55 additions & 0 deletions ManagedCode.Repository.LiteDB/LiteDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,61 @@ protected override async Task<TItem> GetAsyncInternal(Expression<Func<TItem, boo
await Task.Yield();
return GetDatabase().FindOne(predicate);
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default)
{
await Task.Yield();
var query = GetDatabase().Query().Skip(skip).Limit(take ?? 2147483647);
foreach (var item in query.ToEnumerable())
{
if (token.IsCancellationRequested)
{
break;
}

yield return item;
}
}

protected override async IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy, Order orderType, int? take = null, int skip = 0, CancellationToken token = default)
{
await Task.Yield();
var query = GetDatabase().Query();

if (orderType == Order.By)
{
query = query.OrderBy(orderBy);
}
else
{
query.OrderByDescending(orderBy);
}

if (take != null)
{
foreach (var item in query.Limit(take.Value).ToEnumerable())
{
if (token.IsCancellationRequested)
{
break;
}

yield return item;
}
}
else
{
foreach (var item in query.ToEnumerable())
{
if (token.IsCancellationRequested)
{
break;
}

yield return item;
}
}
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
<Description>Repository for LiteDB</Description>
</PropertyGroup>

Expand All @@ -19,7 +19,7 @@
<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.10" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down