Executing query until there is nothing more left
I often query a database to get a batch of items to procees. I do this as long as the query returns some items. I use this pattern quite a lot so I thought I create a small helper so that I don't have to implement this logic again and again.
It's a small class that executes the query until there is nothing more left:
public static class Unfold
{
public static async Task ForEachAsync<T>
(
Func<CancellationToken, Task<IList<T>>> query,
Func<IList<T>, CancellationToken, Task> body,
CancellationToken cancellationToken
)
{
while (true)
{
var result = await query(cancellationToken);
if (result.Any())
{
await body(result, cancellationToken);
}
else
{
break;
}
}
}
}
The reason why I implemented it exactly this way is:
- all my queries are
async
- they must always return
IList<T>
(if they return a collection of course) - I always process a batch at a time that I then mark as processed
Example
The typical use-case is like this:
- get a batch of items from a repository
- process this batch
- repeat until the batch is empty
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
A test repository:
public class NumberRepository
{
private readonly IList<IList<int>> _numbers = new { new { 1, 2 }, new { 3, 4 }, new { 5 }, new int[0] };
private int _batchIndex;
public Task<IList<int>> GetNumbersAsync(CancellationToken cancellationToken) => Task.FromResult(_numbers[_batchIndex++]);
}
and the processing method:
private Task ProcessBatch<T>(T item, CancellationToken cancellationToken)
{
item.Dump();
return Task.CompletedTask;
}
What do you say? Is this a good or a bad solution? Is there anything missing (but null-checks)?
c# async-await
add a comment |
I often query a database to get a batch of items to procees. I do this as long as the query returns some items. I use this pattern quite a lot so I thought I create a small helper so that I don't have to implement this logic again and again.
It's a small class that executes the query until there is nothing more left:
public static class Unfold
{
public static async Task ForEachAsync<T>
(
Func<CancellationToken, Task<IList<T>>> query,
Func<IList<T>, CancellationToken, Task> body,
CancellationToken cancellationToken
)
{
while (true)
{
var result = await query(cancellationToken);
if (result.Any())
{
await body(result, cancellationToken);
}
else
{
break;
}
}
}
}
The reason why I implemented it exactly this way is:
- all my queries are
async
- they must always return
IList<T>
(if they return a collection of course) - I always process a batch at a time that I then mark as processed
Example
The typical use-case is like this:
- get a batch of items from a repository
- process this batch
- repeat until the batch is empty
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
A test repository:
public class NumberRepository
{
private readonly IList<IList<int>> _numbers = new { new { 1, 2 }, new { 3, 4 }, new { 5 }, new int[0] };
private int _batchIndex;
public Task<IList<int>> GetNumbersAsync(CancellationToken cancellationToken) => Task.FromResult(_numbers[_batchIndex++]);
}
and the processing method:
private Task ProcessBatch<T>(T item, CancellationToken cancellationToken)
{
item.Dump();
return Task.CompletedTask;
}
What do you say? Is this a good or a bad solution? Is there anything missing (but null-checks)?
c# async-await
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25
add a comment |
I often query a database to get a batch of items to procees. I do this as long as the query returns some items. I use this pattern quite a lot so I thought I create a small helper so that I don't have to implement this logic again and again.
It's a small class that executes the query until there is nothing more left:
public static class Unfold
{
public static async Task ForEachAsync<T>
(
Func<CancellationToken, Task<IList<T>>> query,
Func<IList<T>, CancellationToken, Task> body,
CancellationToken cancellationToken
)
{
while (true)
{
var result = await query(cancellationToken);
if (result.Any())
{
await body(result, cancellationToken);
}
else
{
break;
}
}
}
}
The reason why I implemented it exactly this way is:
- all my queries are
async
- they must always return
IList<T>
(if they return a collection of course) - I always process a batch at a time that I then mark as processed
Example
The typical use-case is like this:
- get a batch of items from a repository
- process this batch
- repeat until the batch is empty
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
A test repository:
public class NumberRepository
{
private readonly IList<IList<int>> _numbers = new { new { 1, 2 }, new { 3, 4 }, new { 5 }, new int[0] };
private int _batchIndex;
public Task<IList<int>> GetNumbersAsync(CancellationToken cancellationToken) => Task.FromResult(_numbers[_batchIndex++]);
}
and the processing method:
private Task ProcessBatch<T>(T item, CancellationToken cancellationToken)
{
item.Dump();
return Task.CompletedTask;
}
What do you say? Is this a good or a bad solution? Is there anything missing (but null-checks)?
c# async-await
I often query a database to get a batch of items to procees. I do this as long as the query returns some items. I use this pattern quite a lot so I thought I create a small helper so that I don't have to implement this logic again and again.
It's a small class that executes the query until there is nothing more left:
public static class Unfold
{
public static async Task ForEachAsync<T>
(
Func<CancellationToken, Task<IList<T>>> query,
Func<IList<T>, CancellationToken, Task> body,
CancellationToken cancellationToken
)
{
while (true)
{
var result = await query(cancellationToken);
if (result.Any())
{
await body(result, cancellationToken);
}
else
{
break;
}
}
}
}
The reason why I implemented it exactly this way is:
- all my queries are
async
- they must always return
IList<T>
(if they return a collection of course) - I always process a batch at a time that I then mark as processed
Example
The typical use-case is like this:
- get a batch of items from a repository
- process this batch
- repeat until the batch is empty
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
A test repository:
public class NumberRepository
{
private readonly IList<IList<int>> _numbers = new { new { 1, 2 }, new { 3, 4 }, new { 5 }, new int[0] };
private int _batchIndex;
public Task<IList<int>> GetNumbersAsync(CancellationToken cancellationToken) => Task.FromResult(_numbers[_batchIndex++]);
}
and the processing method:
private Task ProcessBatch<T>(T item, CancellationToken cancellationToken)
{
item.Dump();
return Task.CompletedTask;
}
What do you say? Is this a good or a bad solution? Is there anything missing (but null-checks)?
c# async-await
c# async-await
edited Dec 6 at 11:38
asked Dec 6 at 10:01
t3chb0t
34k746111
34k746111
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25
add a comment |
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
Sorry, but you definitely need a null
check here:
if (result.Any())
Else there is not much to comment.
About the usage:
I don't understand, why you create a lambda for the query argument:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
Why not just:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: numbers.GetNumbersAsync,
body: ProcessBatch,
CancellationToken.None
);
}
numbers.GetNumbersAsync
is awaitable already?
1
Right, and even to avoid thenull
check at all I definitely need to put the[NotNull]
attribute there as I always guaratee that collections are nevernull
; Unfortunatelly it does not work well withTask<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)
– t3chb0t
Dec 6 at 12:36
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
– t3chb0t
Dec 6 at 12:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209146%2fexecuting-query-until-there-is-nothing-more-left%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sorry, but you definitely need a null
check here:
if (result.Any())
Else there is not much to comment.
About the usage:
I don't understand, why you create a lambda for the query argument:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
Why not just:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: numbers.GetNumbersAsync,
body: ProcessBatch,
CancellationToken.None
);
}
numbers.GetNumbersAsync
is awaitable already?
1
Right, and even to avoid thenull
check at all I definitely need to put the[NotNull]
attribute there as I always guaratee that collections are nevernull
; Unfortunatelly it does not work well withTask<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)
– t3chb0t
Dec 6 at 12:36
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
– t3chb0t
Dec 6 at 12:47
add a comment |
Sorry, but you definitely need a null
check here:
if (result.Any())
Else there is not much to comment.
About the usage:
I don't understand, why you create a lambda for the query argument:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
Why not just:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: numbers.GetNumbersAsync,
body: ProcessBatch,
CancellationToken.None
);
}
numbers.GetNumbersAsync
is awaitable already?
1
Right, and even to avoid thenull
check at all I definitely need to put the[NotNull]
attribute there as I always guaratee that collections are nevernull
; Unfortunatelly it does not work well withTask<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)
– t3chb0t
Dec 6 at 12:36
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
– t3chb0t
Dec 6 at 12:47
add a comment |
Sorry, but you definitely need a null
check here:
if (result.Any())
Else there is not much to comment.
About the usage:
I don't understand, why you create a lambda for the query argument:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
Why not just:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: numbers.GetNumbersAsync,
body: ProcessBatch,
CancellationToken.None
);
}
numbers.GetNumbersAsync
is awaitable already?
Sorry, but you definitely need a null
check here:
if (result.Any())
Else there is not much to comment.
About the usage:
I don't understand, why you create a lambda for the query argument:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: async token => await numbers.GetNumbersAsync(token),
body: ProcessBatch,
CancellationToken.None
);
}
Why not just:
async Task Main()
{
var numbers = new NumberRepository();
await Unfold.ForEachAsync
(
query: numbers.GetNumbersAsync,
body: ProcessBatch,
CancellationToken.None
);
}
numbers.GetNumbersAsync
is awaitable already?
answered Dec 6 at 12:30
Henrik Hansen
6,7331824
6,7331824
1
Right, and even to avoid thenull
check at all I definitely need to put the[NotNull]
attribute there as I always guaratee that collections are nevernull
; Unfortunatelly it does not work well withTask<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)
– t3chb0t
Dec 6 at 12:36
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
– t3chb0t
Dec 6 at 12:47
add a comment |
1
Right, and even to avoid thenull
check at all I definitely need to put the[NotNull]
attribute there as I always guaratee that collections are nevernull
; Unfortunatelly it does not work well withTask<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)
– t3chb0t
Dec 6 at 12:36
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
– t3chb0t
Dec 6 at 12:47
1
1
Right, and even to avoid the
null
check at all I definitely need to put the [NotNull]
attribute there as I always guaratee that collections are never null
; Unfortunatelly it does not work well with Task<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)– t3chb0t
Dec 6 at 12:36
Right, and even to avoid the
null
check at all I definitely need to put the [NotNull]
attribute there as I always guaratee that collections are never null
; Unfortunatelly it does not work well with Task<T>
:-( In this example using a lambda is not necessary, true, but in my application I have some EF queries there so I wanted to simulate it as real as possible ;-)– t3chb0t
Dec 6 at 12:36
1
1
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
My first try was with observables but I failed to generate them ;-]
– t3chb0t
Dec 6 at 12:38
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
– Henrik Hansen
Dec 6 at 12:44
1
1
Oh, cool, I didn't know that but one can use the
[ItemNotNull]
on Task<T>
, see - if you're using them too.– t3chb0t
Dec 6 at 12:47
Oh, cool, I didn't know that but one can use the
[ItemNotNull]
on Task<T>
, see - if you're using them too.– t3chb0t
Dec 6 at 12:47
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209146%2fexecuting-query-until-there-is-nothing-more-left%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Oh, I see some has donvoted it... how so?
– t3chb0t
Dec 8 at 16:25