taking advantage of classes in a db intensive application

J

JohnR

Assume you have a grocery store application. An order consists of an order
header with the "customer" info, and multiple "item" records (one for each
thing they ordered). Now, in a regular DB application this is straight
forward stuff. However, I'd like some advice as to how to best take
advantage of classes to make a more flexible application.

I'm thinking about having a "order" class which would contain a "customer
class" instance and multiple "item class" instances. Each class would have
some good properties and methods associated with them. For example, a
method of the "item" class might be to display the total amount of that item
that has sold year-to-date.

What I envision is to create a runtime context menu that would appear when
you right click an instance of one of these classes. The context menu would
then give the user a choice of some nifty things they could do, based on the
methods available for the class.

The problem is that the easiest way to present data is to bind a datagrid to
the database. But what I'd like to do is somehow create the class instances
for the database records so that the datagrid actually contains the class
instance. I can then do a HitTestInfo and if the user clicks on a cell,
find out what class is in the cell and create the appropriate context menu
as described above.

Hopefully I was clear in my vision... Any ideas how one might organize
something like this???

Thanks.
 
M

Michael C#

I recently had to do something similar for a class. I basically created a
Customer Class, an Order Class and an Item Class. An Item was just simply a
single item that could be bought (i.e., lettuce, celery, etc.), the Order
had order header properties and contained a list of Items (exposed as an
ArrayList of Items property), and each Customer had an associated list of
Orders. I made the classes self-contained, in that you could create an
Order Object and add it to a Customer via a Customer.CreateNewOrder()
method, etc.

As far as Display, I created publicly accessible functions that returned
DataTables. For instance, Customer.GetAllOrders() returned a DataTable of
all Orders that a customer placed. Then I bound the DataTable to a
DataGrid. It's a little more work than just Binding a DataGrid directly to
the data source, but it made the job of implementing everything else much
easier, and overhauling the UI later was a snap.

Hope that helps
 
J

JohnR

Hmmm. methods that returned a datatable.. that seems like a good idea.
But what technique did you use to go from the actual Customers Table in the
database to a customer class. Or did you just have some sort of binding to
the customer table, and when the user selected a customer record, you then
loaded it into the customer class? You see, that's my missing link.
Trying to find a reasonable way to get from the database tables into the
various class instances that will be defined in the actual program. Clearly
one way is to just bind a datagrid to the dataset and when the user selects
a row, create instances of the appropriate classes from the column. Another
way is to bypass the dataset alltogether and do a SQL Select to return the
rows and create the class instances as the data is read from the database.
Then just stuff the class instances in an arraylist or something. Then I
could bind the datagrid to the arraylist to expose all the properties as the
columns.

Any feel for what might work best? Or any other technique you might
suggest?

Thanks again...
 
M

Michael C#

Actually what I did was just create a LoadCustomer() method that loaded a
customer based on his index_number in the database. I basically did a SQL
SELECT on the database and assigned the returned values to the object
properties. Really simplistic in design, but it worked well, and helped me
keep the whole thing self-contained. I also added Shared methods to the
Customer Class, such as a FindByName() function to locate a customer by name
in the database and return his/her index_number. So, it might look like
this:

Dim c as New Customer
Dim i as Integer
i = c.FindByName ("Lee, Bruce")
c.LoadCustomer (i)
' Now the Customer object c contains all properties of the customer
' Lee, Bruce from the database

There might be a better way to do it, but I'm sort of old school when it
comes to that :) Plus I wanted to make all my classes as self-contained as
possible. Also, when I created the DataTable, I basically did it one row at
a time - which worked out well since I had to do some calculations and
validation on different values as they were read in also; and assignment of
values read in to properties (for instance, you can't assign a DBNull.Value
to a string variable or property... So I had to validate for that before
assigning the database value to a Customer property). One of my
requirements was that all the data in the database had to be exposed as
public properties of the various classes, so I assigned the properties as I
read in the records.

Like I said, there might be better ways to do it, but this one worked well
for me at the time :)
 
J

JohnR

Thanks so much for your help. You've given me some really good stuff to
think about....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top