Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using commercetools.Sdk.Domain;

namespace commercetools.Sdk.Client
{
public class ClientQueryAllEnumerator<TCommand> : IEnumerator<TCommand>
{
private const int DefaultOffset = 0;
private const int DefaultLimit = 20;

private IClient client;
private QueryCommand<TCommand> command;

private List<TCommand> resultSet;
private int position = -1;

public ClientQueryAllEnumerator(IClient client, QueryCommand<TCommand> command)
{
if (client == null)
{
throw new FieldAccessException("Client cannot be null");
}

this.client = client;
this.command = command;
}

public bool MoveNext()
{
this.position++;
if (this.command.QueryParameters is IPageable pageableParameters && pageableParameters.Limit == null)
{
var limit = DefaultLimit;
if (this.resultSet == null || this.position % limit == 0)
{
// Retrieve new set of results
var originalOffset = pageableParameters.Offset;

// Reset offset to original value after retrieveing for an eventual reuse of the command somewhere else
pageableParameters.Offset = (originalOffset ?? DefaultOffset) + this.position;
RetrieveResults(command);
pageableParameters.Offset = originalOffset;
}

return this.position % limit < this.resultSet.Count;
}

// If command is not paged
if (this.resultSet == null)
{
RetrieveResults(this.command);
}

return this.position < this.resultSet.Count;
}

private void RetrieveResults(QueryCommand<TCommand> queryCommand)
{
var queryTask = this.client.ExecuteAsync(queryCommand);
this.resultSet = queryTask.Result.Results;
}

public void Reset()
{
this.resultSet = null;
this.position = -1;
}

public TCommand Current
{
get
{
if (this.command.QueryParameters is IPageable pageableParameters)
{
var limit = pageableParameters.Limit ?? DefaultLimit;
return this.resultSet[this.position % limit];
}

return this.resultSet[this.position];
}
}

object IEnumerator.Current => Current;

public void Dispose()
{
this.resultSet?.Clear();

this.client = null;
this.command = null;
this.resultSet = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ public class ClientQueryProvider<T> : IQueryProvider
private readonly IClient client;

private IList<T> result = new List<T>();
private IEnumerator<T> enumerator;

public ClientQueryProvider(IClient client, QueryCommand<T> command)
public ClientQueryProvider(IClient client, QueryCommand<T> command, bool queryAll)
{
this.client = client;
this.Command = command;

if (queryAll)
{
enumerator = new ClientQueryAllEnumerator<T>(client, command);
}
}

public QueryCommand<T> Command { get; }
Expand Down Expand Up @@ -128,6 +134,12 @@ public object Execute(Expression expression)

public TResult Execute<TResult>(Expression expression)
{
if (enumerator != null)
{
enumerator.Reset();
return (TResult)enumerator;
}

if (this.client == null)
{
throw new FieldAccessException("Client cannot be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace commercetools.Sdk.Client

public class ClientQueryableCollection<T> : IOrderedQueryable<T>
{
public ClientQueryableCollection(IClient client, QueryCommand<T> command)
public ClientQueryableCollection(IClient client, QueryCommand<T> command, bool queryAll = false)
{
this.Provider = new ClientQueryProvider<T>(client, command);
this.Provider = new ClientQueryProvider<T>(client, command, queryAll);
this.Expression = Expression.Constant(this);
}

Expand Down
5 changes: 5 additions & 0 deletions commercetools.Sdk/commercetools.Sdk.Client/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static ClientQueryableCollection<T> Query<T>(this IClient client)
return new ClientQueryableCollection<T>(client, new QueryCommand<T>());
}

public static ClientQueryableCollection<T> QueryAll<T>(this IClient client)
{
return new ClientQueryableCollection<T>(client, new QueryCommand<T>(), true);
}

public static ClientQueryableCollection<ProductProjection> SearchProducts(this IClient client)
{
return new ClientQueryableCollection<ProductProjection>(client, new SearchProductProjectionsCommand());
Expand Down