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.

internal class PageObject {
    private readonly AutomationElement _mainWindow;
    private readonly Lazy<AutomationElement> _password;
    private readonly Lazy<AutomationElement> _serverUrl;
    private readonly Lazy<AutomationElement> _project;
    private readonly Lazy<AutomationElement> _domain;
    private readonly Lazy<AutomationElement> _username;
    private readonly Lazy<AutomationElement> _connectButton;
    private readonly Lazy<AutomationElement> _installButton;
    private readonly Lazy<AutomationElement> _statusMessage;

    public PageObject (AutomationElement mainWindow) {
        _mainWindow = mainWindow;
        _serverUrl = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("ServerUrlField"));

        _project = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("ProjectField"));
        _domain = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("DomainField"));
        _username = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("UserField"));
        _password = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("PasswordField"));
        _connectButton = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("ConnectBtn"));
        _installButton = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("InstallBtn"));
        _statusMessage = new Lazy<AutomationElement> (() => mainWindow.FindChildById ("StatusMessage"));
    }

    public AutomationElement Password { get { return _password.Value; } }
    public AutomationElement Username { get { return _username.Value; } }
    public AutomationElement Domain { get { return _domain.Value; } }
    public AutomationElement Project { get { return _project.Value; } }
    public AutomationElement ServerUrl { get { return _serverUrl.Value; } }
    public AutomationElement ConnectButton { get { return _connectButton.Value; } }
    public AutomationElement InstallButton { get { return _installButton.Value; } }
    public AutomationElement StatusMessage { get { return _statusMessage.Value; } }
}

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:

public AutomationElement StatusMessage {
    get { 
        if (_statusMessage == null) 
        {
            _statusMessage = _mainWindow.FindChildByCondition ("StatusMessage"); 
        }
        return _statusMessage; 
    }
}

Time for a nap.