5
I wonder if in . Net there is some class that allows me to tell her how to read a data, but that DO NOT read immediately... only later when requested, this class reads and stores the value, in case several readings are taken.
Something like:
var delayedDb = DelayedReader.Create(() => LerAlgoDoBancoDeDados());
var resultado = dadoDaMemoria != null ? dadoDaMemoria :
delayedDb.Read() != null ? delayedDb.Read() :
null;
To keep me from having to do that:
var dadoDoBanco = LerAlgoDoBancoDeDados();
var resultado = dadoDaMemoria != null ? dadoDaMemoria :
dadoDoBanco != null ? dadoDoBanco :
null;
Great article you indicated. Could you bring more information to your answer so that it is more detailed? For example how to use the class
Lazy
. Thanks! =)– Miguel Angelo
@beta, important you include that this class is only available from version 4 of the framework.
– Cleiton
So, I was busy here at the company, I answered the basic same, but I will edit the answer with more information.
– BetaSystems - Rodrigo Duarte
For this particular example, it was necessary to clarify that
Propriedades.Value
will create aIEnumerable<Propriedade>
empty...I mean, without anything else, the default constructor ofT
inLazy<T>
is invoked. If the default constructor has in you expensive work, then, all right. But usually you seeLazy<T>
used with delegate doing expensive work. Using original example:Lazy<Collection<Dados>> DelayedReader = new Lazy<Collection<Dados>>(() => LerAlgoDoBancoDeDados());
Then, the reading (expensive work) happens only in the invocation ofDelayedReader.Value
– mdisibio