Mega Code Archive

 
Categories / Delphi / OOP
 

Building a business object

Title: Building a business object Question: Each business object should encapsulate queries, rules or calculations autonom in the class and independent from GUI or data-access. Parameters came from the GUI and with Delphi's TQuery or TDataModule you can change the data-access, as long you handle with SQL. Answer: You need a InterBase DB with the standard demoDB employee.gdb for example: d:\Programme\Gemeinsame Dateien\Borland Shared\Data\employee.gdb This new demo is designed to show the use of InterBase and business rules with objects. How do you build a business object? ----------------------------------- What's a business object ? To provide business services, a business object works in collaboration with data storage objects and interface objects. A business object transforms data into information with queries or calculations. The data may be acquired from data services or file services. Business objects are sometimes referred to as conceptual objects, because they provide services which meet business requirements, regardless of technology. The idea of this article is in fact dedicated to the development of such a business object, following example with InterBase. In a data modules unit file, you can write methods, including event handlers for the components in the module, as well as global routines that encapsulate business rules. For example, you might write a procedure to perform fee calculation in a bank you could call such a procedure from an event handler for a component in the module or from any form that uses the module. In a simple business object (without fields in the class), yo do have at least 4 tasks to fulfill: 1. The Business-Class inherits from a Data-Provider 2. The query is part of the class 3. A calculation or buiness-rule has to be done 4. The object is independent from the GUI, the GUI calls the object 1. We build the class, Superclass can be TQuery or TDataModule type TBusinessObj = class(TQuery) private function open_QueryFee(qryID: integer):boolean; function calcFee(fee: double):double; public procedure changeFee(amount: double); procedure changeLimit(amount: double); end; 2. We define a parametrised query function TBusinessObj.open_QueryFee(qryID: integer):boolean; begin result:= false; try SQL.Clear; SQL.add('SELECT * from ACCOUNT'); SQL.add('WHERE acc_no =:pClient_acc'); params[0].name:='pClient_acc'; params[0].dataType:=ftInteger; params[0].AsInteger:= qryID; Open; result:= true; finally //garbage or something end; end; 3. Before we save the amount straight, we calculate something procedure TBusinessObj.changeFee(amount: double); begin edit; FieldByName('FEE').asFloat:= calcFee(amount); post end; // like a business rule function TBusinessObj.calcFee(fee: double): double; begin result:= (2 * fee) / BANK_FACTOR //just a calc end; 4. Let's go to the client, which has a business object as his member: private ... tblAccount: TBusinessObj; end; procedure TForm1.btnFeeClick(Sender: TObject); begin with tblAccount do begin if open_QueryFee(strToInt(edtAccount.text)) then changeFee(strToFloat(edtFee.text)); end; end; As long as we don't have fields in the business object in order to map a relational database in a OO-manner, we can simple typecast the instance, without a constructor. procedure TForm1.FormCreate(Sender: TObject); begin tblAccount:= TBusinessObj(Query1); //typecast end; Outlook: --------------------------------------------------------- When time comes, you want to map the attributs from a database to the fields of the corresponding class. That means, all SQL-statements are encapsulated, so you got a real OO-access, which I will describe in a second part. Let's see an example of a highlight in object database programming, which is similar by Boldsoft with BOLD or gs-soft with MetaBASE: Person:=TPerson.Create; oAddressList:=TAddressList.Create; try oPerson.Person_ID:=30; oAddressList:=TAddressList.DBReadAllRelatedToObject(oPerson); for i:=0 to oAddressList.Count-1 do S_MBox(oPerson.LastName+' '+oAddressList.Adresses[i].Town); finally oPerson.Free; oAddressList.Free; end;