Skip to content

Lazy Async

Sponsor: Interested in learning more about Distributed Systems Design? Enter for a chance to win a 5 day Advanced Distributed Systems Design course led by Udi Dahan founder of Particular Software, and creator of NServiceBus.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Lazy AsyncI’ve been moving some data access code to to use Lazy<T> whenever I want to defer actually fetching the data until it’s first needed.  What I’ve always found interesting is there is no Lazy Async. In the exact use case it’s when an application loads, there is some static data loaded from a database.  This data is the cached for later use. However, in many situations it’s not actually needed until much later if at all.  Why take the performance hit of fetching the data on app startup if that data is often times not needed? That’s the whole purpose of Lazy<T>.  But as mentioned, why is there no Lazy Async?  If i want to defer IO, that’s generally async. Here is more info on Lazy Initialization if it’s new to you.

LazyAsync

I’ve just created a small class that extends Lazy<T>.  The usage is how you can expect.  Here is a small sample that fetches data from an HTTP service.

Source

All the source code for my demo is available on GitHub if you want to try it yourself. If anyone has any other suggestions or recommendations about blog topics, please leave comment or let me know on twitter.