Lazy day

Today we’re not having a post on the soothing music of Enya but the Lazy<T> class in .Net 4.0. This is probably old news to most out there but I just found it recently (having learned that lazy loading is usually something to avoid, temporal dependencies and all that).

It’s nothing complicated really but it reads quite nicely so I’ve used it a few times now and thought I’d bump it.

As an example, I’ve got a simple PageObject which would make sense to not load all elements into, and especially wait to load them until we need them because before then, they might not even exist on the page.

Each property is backed by a Lazy<AutomationElement> field, created with a Lambda to retrieve the specific field from the main page.

It’s as simple as that. And of course the Lazy<T> class implementation would be simple enough to write yourself but isn’t it nice when we can all just come together and use the same classes. Plus it reads a lot nicer, and makes obvious what is happening, than:

Time for a nap.

Lazy... loading?