Page 3 of 3

Re: Example Business Object (Customer)

PostPosted: Fri Nov 30, 2018 2:23 am
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  Expand view
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

Re: Example Business Object (Customer)

PostPosted: Fri Nov 30, 2018 2:52 am
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?

Re: Example Business Object (Customer)

PostPosted: Fri Nov 30, 2018 4:31 am
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