2

I was wondering what the purpose of separating the instantiation logic and the data loading logic of a class that loads data into memory if the class is a one time use.

Here is an example of what I mean.

DataLoader loader = new DataLoader("serverName", "databaseName");
loader.LoadData();

//Do Something with the loader never calling the LoadData function again

The loader itself is used once as the data is loaded into the global variables in the class.

class DataLoader {
    private DataSet _data;

    public DataLoader(string server, string database){}

    public void LoadData(){
        //load _data with sql data.
    }

    //Other functions that allow controlled access to the _data variable
}

Is there a valid reason for doing this or is it just flawed design?

gnat
  • 21,442
  • 29
  • 112
  • 288
tt9
  • 611
  • 7
  • 19
  • I think there's a good question in here, but I'm not exactly certain what you're getting at. Why isn't `loader.LoadData();` wrapped into the constructor for `DataLoader`? –  Dec 21 '15 at 21:16
  • @GlenH7 I'm pretty sure "Why isn't loader.LoadData(); wrapped into the constructor for DataLoader?" is the exact question he's asking us. – Ixrec Dec 21 '15 at 21:19
  • In this particular case it, a good reason is that it decouples the data from the source of data. If for instance you want to run the same code against a CSV file, wouldn't it be nice if you could instantiate a CsvDataLoader without having a "server" and "database". – ArTs Dec 22 '15 at 04:57

0 Answers0