Lazy Loading for the Lazy Coder


Lazy loading in properties is very common.  It is a relativity simple concept of only loading the data you once you actually need it, instead of front loading it all.  Though the problem with doing this all over the place is that you end up copying and pasting code a lot to do it.  And we all know how much copying and pasting code can lead to bugs and convoluted code.

In order to allow for the benefits of lazy loading but also to make the code a bit cleaner I made a class to help with it.  The class is slightly under developed in the fact that it can't be used in all cases, but it is more a proof of concept of what can be done to help.  As I use the class more and need it to do more things I'm sure I will expand it and adapt it to my needs.  I strongly encourage everyone to see if something like this could help their code.

Below I have the code for the class.  Then following that is code to show an example of code that doesn't use it and the same code that does use it.

 

public class LazyLoader<T>
{
    public delegate T LazyLoaderDelegate();
    private bool? isInit = null;
    private T _value;
    private LazyLoaderDelegate _valueCalculator;

    public LazyLoader(LazyLoaderDelegate valueCalculator)
    {
        _valueCalculator = valueCalculator;
    }

    public T Value
    {
        get
        {
            if (isInit != true)
            {
                _value = _valueCalculator();
                isInit = true;
            }
            return _value;
        }
        set
        {
            _value = value;
            isInit = true;
        }
    }

    public void Clear()
    {
        isInit = false;
    }

} 
Code that doesn't use the lazy loading class:
public class WidgetCounter
{
    private int _numberOfWidgets;
    private bool _numberOfWidgetsInit;
    public int NumberOfWidgets 
    {
        get
        {
            if (_numberOfWidgetsInit == false)
            {
                _numberOfWidgets = GetValue();
            }
            return _numberOfWidgets;
        }
        set
        {
            _numberOfWidgets = value;
            _numberOfWidgetsInit = true;
        }
    }

    private int GetValue()
    {
        // Do some really time consuming operation
        Thread.Sleep(1000);
        return 5;
    }

}
Code uses the lazy loading class:
public class WidgetCounter
{
    private LazyLoader<int> _numberOfWidgets;

    public WidgetCounter()
    {
        _numberOfWidgets = new LazyLoader<int>(delegate { return GetValue(); });
    }
    public int NumberOfWidgets
    {
        get { return _numberOfWidgets.Value; }
        set { _numberOfWidgets = value; }
    }

    private int GetValue()
    {
        // Do some really time consuming operation
        Thread.Sleep(1000);
        return 5;
    }

}
Comments (1)
  1. Donkblog says:

    I was fiddling with some WPF stuff the other day and I needed the handle to the form.&#160; Windows in

Comments are closed.

Skip to main content