C # Where to load source data into an object?

Similar to this question: C # Constructor Designer , but this question is slightly different.

I have a Customer class and a CustomerManager class. When an instance is created from the CustomerManager class, I want to load all customers. And here I am stuck. I can do this in several ways:

  • Load all clients into the constructor (I donโ€™t like this because it may take some time if I have many clients)
  • In each method of the CustomerManager class that performs database-related actions, checks to see if the local customer list 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 that loads all clients. This method must be called before calling methods that perform database-related actions:

    In the class:

     public LoadData() { // some code which will load the customers list } 

    In the shape of:

     CustomerManager manager = new CustomerManager(); manager.LoadData(); Customer customer = manager.FindCustomer(int id); 

What is the best way to do this?

EDIT:

I have a feeling that they misunderstood me here. Perhaps this is because I was not clear enough. In the CustomerManager class, I have several methods that depend on the local list (_customers). So my question is: where should I fill out this list?

+6
c # winforms
source share
3 answers

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.

+9
source share

Use the property to access clients. Check if clients are loaded.

+5
source share

Well, it depends. All your options have advantages and disadvantages.

The good thing about options 1 and 3 is that the user has full control over the execution of the (long) data download. Whether option 1 or 3 is better depends on whether it makes sense to create a Manager and upload data later or not. Personally, I prefer the separate LoadData method if this is a lengthy operation, but it may be a matter of taste.

The good thing about option 2 is that the data will not load if it is not needed. The disadvantage is that (long) loading occurs as a side effect of the first access, which makes your program "less deterministic."

Basically, all the options that you have presented are accurate and valid. It really depends on your requirements.

+2
source share

All Articles