What you are describing is lazy loading.
A simple approach is to have a private property like this:
private Lixt<Customer> _customers; private List<Customer> Customers { get { if(_customers == null) _customers = LoadData(); return _customers; } }
You then refer to Customers internally. Customers will be downloaded the first time they are needed, but not before.
This is such a generic template that .Net 4.0 has added the Lazy<T> class, which does this for you.
In this case, you simply define it as private:
private Lazy<List<Customer>> _customers = new Lazy<List<Customer>>(LoadData);
Then you just refer to your clients in code:
_customers.Value
The class initializes the value using the LoadData() method.
If you are not already in .Net 4.0, the Lazy<T> class is very easy to implement.
Brian genisio
source share