I would like to hear some input on the proper way to build classes so I can fill controls such as dropdown boxes in the proper way in C#.
Right now I have objects that can be contained by collection classes (such as Customer and Customers). The Customers object's constructor takes the ID of another object that has Customer objects (such as a Business) and fills the collection in this manner:
A. First, do a query to get a list of Customer IDs that are associated with the Business.
B. Once I have that list, I loop through, create a new Customer and add it to the collection, i.e.
Each Customer constructor queries the database to return a fully populated existing Customer.
My problem is when I want to come up with a list of Customer.Name's and Customer.ID's for every existing customer so I can use them in a bound list, such as a drop-down list box! The way I see it, I have two choices:
A. "Cheat" and query the database directly to populate the list box from the GUI code. One hit to the database, minimal overhead, but is not OOP.
B. Loop through all Customers and create a separate Customer object for each one, even though all I need is the .Name and .ID of each one. Seems like the right "OOP" thing to do, but if you have 200 Customers, that's 200 separate database hits instead of one!
I thought about passing a dataset to the Customer object via another constructor, but I'd like to keep the Customer completely encapsulated.
Am I missing another way? What is the commonly accepted OOP way to tackle this problem?
Thanks for your advice...
Right now I have objects that can be contained by collection classes (such as Customer and Customers). The Customers object's constructor takes the ID of another object that has Customer objects (such as a Business) and fills the collection in this manner:
A. First, do a query to get a list of Customer IDs that are associated with the Business.
B. Once I have that list, I loop through, create a new Customer and add it to the collection, i.e.
Code:
while (dr.Read())
{
List.Add(new Customer(customerID));
}
Each Customer constructor queries the database to return a fully populated existing Customer.
My problem is when I want to come up with a list of Customer.Name's and Customer.ID's for every existing customer so I can use them in a bound list, such as a drop-down list box! The way I see it, I have two choices:
A. "Cheat" and query the database directly to populate the list box from the GUI code. One hit to the database, minimal overhead, but is not OOP.
B. Loop through all Customers and create a separate Customer object for each one, even though all I need is the .Name and .ID of each one. Seems like the right "OOP" thing to do, but if you have 200 Customers, that's 200 separate database hits instead of one!
I thought about passing a dataset to the Customer object via another constructor, but I'd like to keep the Customer completely encapsulated.
Am I missing another way? What is the commonly accepted OOP way to tackle this problem?
Thanks for your advice...