Executing query until there is nothing more left
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25
add a comment |
$begingroup$
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
$endgroup$
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 '18 at 11:38
t3chb0t
asked Dec 6 '18 at 10:01
t3chb0tt3chb0t
34.1k746116
34.1k746116
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25
add a comment |
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
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?
$endgroup$
1
$begingroup$
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 ;-)
$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
$begingroup$
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
$endgroup$
– t3chb0t
Dec 6 '18 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
$begingroup$
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?
$endgroup$
1
$begingroup$
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 ;-)
$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
$begingroup$
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
$endgroup$
– t3chb0t
Dec 6 '18 at 12:47
add a comment |
$begingroup$
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?
$endgroup$
1
$begingroup$
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 ;-)
$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
$begingroup$
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
$endgroup$
– t3chb0t
Dec 6 '18 at 12:47
add a comment |
$begingroup$
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?
$endgroup$
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 '18 at 12:30
Henrik HansenHenrik Hansen
6,8611824
6,8611824
1
$begingroup$
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 ;-)
$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
$begingroup$
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
$endgroup$
– t3chb0t
Dec 6 '18 at 12:47
add a comment |
1
$begingroup$
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 ;-)
$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
$begingroup$
Oh, cool, I didn't know that but one can use the[ItemNotNull]
onTask<T>
, see - if you're using them too.
$endgroup$
– t3chb0t
Dec 6 '18 at 12:47
1
1
$begingroup$
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 ;-)$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
$begingroup$
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 ;-)$endgroup$
– t3chb0t
Dec 6 '18 at 12:36
1
1
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
My first try was with observables but I failed to generate them ;-]
$endgroup$
– t3chb0t
Dec 6 '18 at 12:38
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
$begingroup$
@t3chb0t: Ah, yes, I remember those things :-). But this one is simple and easy to use.
$endgroup$
– Henrik Hansen
Dec 6 '18 at 12:44
1
1
$begingroup$
Oh, cool, I didn't know that but one can use the
[ItemNotNull]
on Task<T>
, see - if you're using them too.$endgroup$
– t3chb0t
Dec 6 '18 at 12:47
$begingroup$
Oh, cool, I didn't know that but one can use the
[ItemNotNull]
on Task<T>
, see - if you're using them too.$endgroup$
– t3chb0t
Dec 6 '18 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.
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
$begingroup$
Oh, I see some has donvoted it... how so?
$endgroup$
– t3chb0t
Dec 8 '18 at 16:25