Is there a late reading class in the .Net framework?

5

I would like to know if there is any class in .Net that allows me to tell it how to read a data, but that DO NOT read immediately ... only later when requested, this class it 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 prevent me from having to do this:

var dadoDoBanco = LerAlgoDoBancoDeDados();

var resultado = dadoDaMemoria != null ? dadoDaMemoria :
                dadoDoBanco != null ? dadoDoBanco :
                null;
    
asked by anonymous 31.01.2014 / 22:26

2 answers

5

So, I do not know this .NET functionality very well, but by doing a search you find Lazy<T> doing the same work as Lazy Loading of Entity Framework , but with that you can Lazy Loading in their classes.

A article with an example of this.

Well, an example of how to use this class:

public class Proprietario
{
    public int ID { get; set; }
    public string Nome { get; set; }

    public Lazy<IEnumerable<Propriedade>> Propriedades = null;

    public Proprietario()
    {
        this.Propriedades = new Lazy<IEnumerable<Propridade>>();
    }
}

At this time, the owner class is created and all of its properties are properly registered, in its constructor, Lazy Loading has been instantiated and is ready for use. To use it, you instantiate the class Proprietário and, finally, you IEnumerable<Propriedade> propriedades = Poprietario.Propriedades.Value .

    
31.01.2014 / 22:35
2

As it has been said that the Lazy<T> class is available in the versions only from .Net 4 , here is an implementation for anyone who needs something like that, which is what I was using previously , and that works with .Net 2.0 or higher.

This is not a clone of the Lazy class, it's an implementation of mine. The mechanism of the class is thread-safe , with penalty only at first reading using the Read method, or when the ReadRefresh method is used. After the first use, it is not possible to discard the internal value of the cache, because of being thread safe ... so there is no ClearCache method. To get rid of the cache the variable must be gathered by the garbage collector.

public static class Delayed
{
    public static Delayed<T> Create<T>(Delayed<T>.Getter getter)
    {
        return new Delayed<T>(getter);
    }
}

public class Delayed<T>
{
    public delegate T Getter();

    private bool hasValue;
    private T value;
    private Getter getter;

    /// <summary>
    /// </summary>
    /// <param name="getter"></param>
    public Delayed(Getter getter)
    {
        if (getter == null) throw new ArgumentNullException("getter");
        this.getter = getter;
        this.value = default(T);
        this.hasValue = false;
    }

    /// <summary>
    /// Reads the value of the Delayed object, using the value from the cache if available, storing the value in a permanent cache.
    /// </summary>
    public T Read()
    {
        if (!this.hasValue)
        {
            lock (this.getter)
            {
                if (!this.hasValue)
                {
                    this.hasValue = true;
                    this.value = this.getter();
                }
            }
        }

        return this.value;
    }

    /// <summary>
    /// Reads the value of the Delayed object, refreshing the value in a permanent cache.
    /// </summary>
    public T ReadFresh()
    {
        lock (this.getter)
        {
            this.hasValue = true;
            this.value = this.getter();
        }

        return this.value;
    }
}
    
01.02.2014 / 18:58