Example Business Object (Customer)

User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Example Business Object (Customer)

Post by James Bott »

Here is an example of a method for the customer class that returns all the customer's invoices.

Instead of returning just an array of invoice numbers, it returns an array of invoice objects. This allows us to use any of the data and/or methods of the objects. Very useful. You could, for example, use the invoice number, date and total in a report.

Note that it is fully encapsulated--it opens it's own copy of the invoice file, uses it, then closes it.

Code: Select all

Method GetInvoices() Class TCustomer
   Local oInvoices := TInvoices():New()
   Local aInvoices := {}
   oInvoices:setOrder( "CUSTNO" )
   oInvoices:setScope( ::CustNo, ::CustNo )
   oInvoices:gotop()
   do while .not. oInvoices:eof
      aadd( aInvoices, TInvoice():New( oInvoices:InvNo ) )
      oInvoices:skip()
   enddo
   oInvoices:end()
Return aInvoices
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Example Business Object (Customer)

Post by nageswaragunupudi »

Mr. James

I do appreciate your concept and approach.

But in the above example, if there are 200 invoices, the Invoices table is opened 200 times with 200 different aliases and kept open till each of the TInvoice object is explicitly Ended. And it is very likely that these objects are not Ended. Is my understanding correct?
Regards

G. N. Rao.
Hyderabad, India
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Example Business Object (Customer)

Post by James Bott »

Nages,

Good observation.

I have several different record classes. One you just pass the table object, and another you pass the ID.

In this example I am passing the ID. The record class opens the table object, loads the variables, and closes it. Thus the objects only exist in memory so once they are out of scope the memory is recovered.

I another version of a record class, you pass the table object and the record class is created using the current record. Thus no additional table objects are created.

The thought did come to me right after I posted the example, that it would require a lot of opening and closing of table objects to create all the invoices. It probably would be better to use the second type of record object in this case since only one copy of the table is required.

In the first case a maximum of two customer table objects are open at one time. In the second case only one customer table object would be open.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Post Reply