What's the common approach to design applications, which strongly rely on lazy evaluation in C#
(LINQ
, IEnumerable
, IQueryable
, ...)?
Right now I usually attempt to make every query as lazy as possible, using yield return
and LINQ
queries, but in runtime this could usually lead to "too lazy" behavior, when every query gets builts from it's beginning obviously resulting in severe visual performance degradation.
What I usually do means putting ToList()
projection operators somewhere to cache the data, but I suspect this approach might be incorrect.
What's the appropriate / common ways to design this sort of applications from the very beginning?
I find it useful to classify each IEnumerable into one of three categories.
For category 1, I tend keep the concrete type when appropriate, arrays or IList etc. For category 3, those are best to keep local within a method, to avoid hard-to find bugs. Then we have category 2, and as always when optimizing performance, measure first to find the bottlenecks.
A few random thoughts - as the question itself is loosely defined:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With