Following from our introduction to the by operator and delegates, this post looks at the first of five built-in delegates, lazy. Lazy property evaluation allows you to set the initial value of a property the first time you try to get it. This is really useful for scenarios where there is a high cost of getting the data. For example, if you want to set the value of a username, which requires a call to a microservice, but since you don't always need the username, you can use this to initial it when you try and retrieve it the first time.
Setting up the context for the example, let us look at two ways you can do this now. The first way is you call the service in the constructor, so you take the hit immediately — regardless if you ever need it. It is nice and clean though.
from DZone.com Feed https://ift.tt/2Q1o4lu
No comments:
Post a Comment