Assume I have a method that performs some I/O operation that asynchronously returns data as some type implementing IAsyncEnumerable<T>
example:
class MyDataStream: IAsyncEnumerable<int> {
//Code omitted for brevity
}
class Bla {
MyDataStream GetData() {
//Code omitted for brevity
}
}
I want to keep it as an IAsyncEnumerable
for as long "upstream" as possible, passing it through a variety of "middleware" eventually streaming the result down an HTTP API as json elements.
example
var data = new Bla().GetData();
IAsyncEnumerable<string> stringData = ToStringMiddleware(data); //performs await foreach
//..other "middleware"
return stringData;
Now in some cases, some middleware may decide to iterate over the whole stream (i.e. to perform some aggregation-function on the data.
var data = new Bla().GetData();
double sum = await Sum(data); //-> this will iterate over the whole stream.
//do something with Sum (i.e. log, or whatever)
//..other "middleware"
return data;
But other middleware down the pipeline doesn't know that. That other middleware then may chose to do a similar thing.
var data = new Bla().GetData();
double sum = await Sum(data); //-> this will iterate over the whole stream.
double average = await Average(data); //-> another iteration..
//..other "middleware"
return data;
I may end up with multiple iterations over the stream (each performing the underlying I/O operation). I don't like that.
I can implement the IAsyncEnumerable
interface in such a way that it only evaluates once, so the first await foreach
will basically keep the data in a private collection for the next iterations. Easy enough. No, if the data is only evaluated at the end by the serializer that writes it to the HTTP response, we've only accessed the data once, and if any middleware iterates over the stream, we also access the data just once.
The big problem I have now is: What do I call that implementation? It's not really a cache, I think. Nor is it a buffer.
I know, this is not so much a technical question as it is a naming question. But I'd like to have my code as understandable as possible and I don't want to name it "MyDataStreamThatOnlyGetsEvaluatedOnce" because it feels stupid and nobody in my team has come up with a better name yet :D
So any ideas or input would be appreciated
Thank you
EDIT: I can understand why this question has been closed as opinion-based. Nevertheless I would like to thank the contributors. The discussion in the comments as well as the provided answers have indeed helped me solve my "problem". THANKS!