Similar to this question:
C# Constructor Design but this question is slight different.
I have a class Customer and a class CustomerManager. When an instance is created of the CustomerManager class I want to load all the customers. And this is where I got stuck. I can do this several ways:
- Load all the customers in the constructor (I don’t like this one because it can take a while if I have many customers)
-
In every method of the CustomerManager class that performs database related actions, check the local list of customers is loaded and if not, load the list:
public method FindCustomer(int id) { if(_customers == null) // some code which will load the customers list } -
Create a method which loads all the customers. This method must be called before calling methods which performs database related actions:
In the class:
public LoadData() { // some code which will load the customers list }In the form:
CustomerManager manager = new CustomerManager(); manager.LoadData(); Customer customer = manager.FindCustomer(int id);
What is the best way to do this?
EDIT:
I have the feeling that I am misunderstood here. Maybe it is because I wasn’t clear enough. In the CustomerManager class I have several methods which depends on the local list (_customers). So, my question is, where should I fill that list?
What you are describing is “lazy loading”.
A simple approach is to have a private property like this:
Then, you refer to
Customersinternally. The customers will be loaded the first time they are needed but no earlier.This is such a common pattern that .Net 4.0 added a
Lazy<T>class that does this for you.I that case, you just define it as a private like this:
Then, you simply refer to your customers in code:
The class will initialize the value with your
LoadData()method.If you are not on .Net 4.0 yet, the
Lazy<T>class is very easy to implement.