PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe...

27
PowerBuilder Foundation Classes Getting started PowerBuilder includes a powerful set of foundation classes to speed development. These classes are collectively known as the PowerBuilder Foundation Classes, or PFC. Part 1 of this article provides an introduction to PFC and shows how to use it. For the experienced PFC developer, I'll outline the more recent changes and additions to PFC in Part 2. This article is not intended as a complete professional reference for PFC - that could be (and is) the subject of an entire book on its own. Nor is it intended as a tutorial, although there are code samples to show you how to use some of the features. What you'll find here is an overview of the architecture, objects, and services of PFC so that you can envision how to use PFC to make your applications more rich, powerful, and user-friendly. Getting Started with the PFC What Is PFC? PFC is a class library of precoded objects and behavior. PFC provides a head start to application developers as an "out-of-the-box" class library solution. PFC contains no prebuilt templates. It's a set of base classes that can be extended to provide a more powerful or specialized set of classes. It is a true "foundation" library. Sybase designed PFC with the intention that it would be extended, even though many developers find the provided functionality of the PFC to be ample. PFC is based on service-based architecture (SBA) and uses simple function calls to enable services. The result is a foundation class library with a

Transcript of PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe...

Page 1: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

PowerBuilder Foundation ClassesGetting started

PowerBuilder includes a powerful set of foundation classes to speed development. These classes

are collectively known as the PowerBuilder Foundation Classes, or PFC. Part 1 of this article

provides an introduction to PFC and shows how to use it. For the experienced PFC developer, I'll

outline the more recent changes and additions to PFC in Part 2.

This article is not intended as a complete professional reference for PFC - that could be (and is) the

subject of an entire book on its own. Nor is it intended as a tutorial, although there are code samples

to show you how to use some of the features. What you'll find here is an overview of the

architecture, objects, and services of PFC so that you can envision how to use PFC to make your

applications more rich, powerful, and user-friendly.

Getting Started with the PFCWhat Is PFC?PFC is a class library of precoded objects and behavior. PFC provides a head start to application

developers as an "out-of-the-box" class library solution.

PFC contains no prebuilt templates. It's a set of base classes that can be extended to provide a

more powerful or specialized set of classes. It is a true "foundation" library. Sybase designed PFC

with the intention that it would be extended, even though many developers find the provided

functionality of the PFC to be ample.

PFC is based on service-based architecture (SBA) and uses simple function calls to enable

services. The result is a foundation class library with a great deal of functionality, flexibility, and

reusability. It's worth noting, however, that the implementation of PFC uses layers of inheritance that

tie many of the objects and references together and prevent individual objects and services from

being used without including the majority of PFC objects in your application.

PFC provides basic windows, menus, and user objects. There are also additional user objects that

provide services for the developer to use to enhance the functionality of any PFC application.

PFC is more than just the code; it's a complete package. As shipped with PowerBuilder 9, PFC

includes:

● PFC Classes● PFC Help● PFC Sample Database● Example program (located in the pfcexamples folder)

Page 2: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

● PFC Start Wizard● Tutorial● Utilities: Library Extender, PFC Message Manager

What Is SBA?PFC uses service-based architecture (SBA), which is a way of building relatively lightweight objects

that delegate a great deal of functionality to service objects. Windows and user objects create

instances of these nonvisual service objects and request functionality and processing from them.

This maximizes the developer's ability to reuse and extend the service objects without needing to

modify existing objects and without having to carry around the overhead of fat ancestor objects. See

Figure below for how objects and services relate to each other.

The traditional inheritance-based model of object construction utilized the capability of object-

oriented (OO) languages to extend a base object's functionality by successively adding code in the

descendants. This was an improvement over the older "copy-book"- based reuse model of non-OO

Page 3: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

languages, but there were some significant drawbacks. As the inheritance tree grew and different

objects with slightly different capabilities sprouted, it became increasingly difficult to change

behaviors, since where to put the new behavior in the inheritance tree was a critical question. In

addition, the sheer size of the objects gave rise to the term "fat ancestor." A fat ancestor always

carries along the overhead of all of its underlying code whether it needs it or not. It loads slowly and

(especially in the 16-bit environment of the time) can cause the compiler to fail if it was too fat. Also,

the developer was forced to choose an object out of the hierarchy to fit whatever specialized use

was required. If the developer wanted to change the use of the object, it was necessary to replace it

with another object from the hierarchy, and painstakingly recode all of his changes.

SBA allows you to do more with less. The ancestor simply carries "hooks" that allow it to make use

of functionality contained in smaller, more efficient "service" objects. The service objects can be

selectively enabled so that only those services that are actually needed are instantiated. This

reduces load time and memory usage. "Object Choice" syndrome is eliminated - the developer uses

a single base object and instantiates services as needed to provide desired functionality.

PFC's implementation of SBA uses instance variables in the PFC objects to provide references to

service classes. The service classes are instantiated by calling a function, of_set, that creates an

instance of the service and points the reference variable to the instance. The general format for the

naming of these reference variables is inv_ where is an "English" name or shortcut for the service.

For example, the instance variable declared in pfc_u_dw for the linkage service is named

inv_linkage. To instantiate the linkage service on a u_dw control placed on a window, you would

write the following statement in the window open event:

dw_1.of_setLinkage(TRUE)

Placing the code for the linkage service in the window open event ensures that all objects on the

window have been fully instantiated. In cases other than the linkage service (which depends on

multiple objects being fully instantiated) you would place the code to create a service in the client

object's constructor or open event.

PFC ArchitecturePFC is built as a set of visual and nonvisual objects with a layered design. PFC is based on two

layers: the PFC and the PFE. Every object in the PFC layer has a descendant in the PFE layer. The

PFC layer is where the actual code that implements PFC functionality is placed. The PFE layer is left

empty for the developer to modify and extend as necessary. Objects in the PFE layer are named (for

example, n_cst_dwsrv_linkage). Objects in the PFC layer are named pfc_ (for example,

pfc_n_cst_dwsrv_linkage). Figure below shows the PFC inheritance structure for a typical object,

n_cst_ wnsrv_statusbar, the window status bar service. The PFC inheritance chain includes a

Page 4: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

"pfc_"-prefixed object and a non-prefixed object for every step from the base object (pfc_n_base) to

the statusbar service (n_cst_wnsrv_statusbar).

There are a few rules that go with PFC's layered design:

● Never modify the PFC objects. Let me repeat, never modify the PFC objects! If you do this, you're simply creating migraine headaches for yourself and anyone unlucky enough to inherit your application. Keep all of your modifications in the PFE layer objects.

● Inherit from PFE-layer objects, not PFC-layer objects.● Instantiate PFE-layer objects, not PFC-layer objects.

These rules allow the PFC-layer objects to be replaced as newer

versions and bug fixes are released without (for the most part)

necessitating the recoding of the developer's modifications and

extensions. Note that over time, especially at the transition

between version 5 and version 6, the PFC modifications were

extensive enough that some action on the part of the developer

was required to properly migrate applications to the new

versions. There have also been some changes to both the

underlying operating system and the PowerBuilder compiler that

have caused behavior changes in the underlying objects, once

again requiring minor changes to existing developer code. But

these slight hiccups have been relatively few, and the PFC-

layered architecture has proven to be quite stable as newer

versions have been released.

PFC is divided into 10 PBLs:

● pfcapsrv: PFC-layer application services● pfcdwsrv: PFC-layer DataWindow services● pfcmain: PFC-layer main objects and controls● pfcwnsrv: PFC-layer window services● pfcutil: PFC-layer utility and global objects and services● pfeapsrv: PFE-layer application services● pfedwsrv: PFE-layer DataWindow services● pfemain: PFE-layer main objects and controls● pfewnsrv: PFE-layer window services● pfeutil: PFE-layer utility and global objects and services

The two-layer approach can be modified to suit your enterprise's needs. If you have extensions that you want to distribute at a departmental level (or across the enterprise),

Page 5: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

you can extend the PFC by adding a MIDDLE layer (often called the PFD layer). Always add extra layers in the middle of the inheritance hierarchy. The functioning of the PFC objects depends on the highest level objects (PFE) having the name without a prefix. What this means is that you'll actually interpose a new layer between the PFC layer and the PFE layer. The objects in the PFE layer will no longer inherit from the PFC-layer objects, but will instead inherit from your new "PFD" objects. You (or some utility under your direction - see the discussion of the PFC Library Extender below) will need to modify the source of the PFE objects to change the inheritance chain. Keep in mind, though, that this introduces additional size and complexity to all of your applications and that you can often accomplish a similar result by creating new services and implementing those services in a standard way throughout the enterprise.If you do choose to add a new layer in the middle, you can use the PFC Library Extender to do the hard work of inserting new objects into the inheritance hierarchy. The PFC Library Extender is one of the utilities provided on the "Tool" tab of the PowerBuilder "New" dialog. The Library Extender uses a wizard to prompt you through the steps involved in adding a new layer.

PFC Major Objects and Services

The PFC objects and services can be broken down into the following major categories.

PFC Objects● Application objects ● Window objects ● PFC controls ● DataWindow objects

PFC Services● Application services ● Window services ● DataWindow services ● Global services

The following sections provide an overview of the objects and how they interact.

PFC ObjectsPFC Application Object

Page 6: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

The single most important PFC object is the application manager (n_cst_appmanager.) Each PFC application must have a global variable (gnv_app) declared as a reference to the application manager.n_cst_appmanager gnv_AppThis global declaration should be the only variable declaration of any type in the application object. The application manager is used to contain variables and code that might ordinarily be placed in the application object. This provides a benefit in the reuse and flexibility of the code. An application object cannot be inherited. This prevents a developer from reusing any functionality that is placed in the application. The application manager is a user object, so it has no such restriction. Figure below 3 illustrates the interaction between the application, the application manager, and PFC service objects.

PFC Window ObjectsPFC Windows are precoded with basic functionality that allows you to use other PFC services easily and to provide your target users with rich functionality with a minimal amount of code. All PFC windows are descendants of the base window, pfc_w_master. The primary PFC windows include:

● w_master: You don't usually use w_master directly in your applications, but this is where you insert code that you would like to apply to all windows across your application. W_master includes code to use the Resize service and the Preferences service.

● w_main: A window of type "main," inherited from w_master. ● w_child: A window of type "child," inherited from w_master. ● w_sheet: Use w_sheet for MDI sheets. This window, also inherited from w_master,

contains additional functionality for automatic display of microhelp. ● w_frame: Use w_frame (or a descendant) as the frame window in MDI applications. This

window contains code to use the Sheet Manager, Status Bar, and Toolbar services. ● w_response: A window of type "response," inherited from w_master. W_response

contains events that can be coded to implement functionality for response-window CommandButtons.

● w_popup: A window of type "popup" inherited from w_master.

Page 7: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

You will also find additional, special-purpose windows, such as w_logon and w_sqlspy, in the PFC hierarchy. Figure below is a clip from the PowerBuilder object browser showing part of the PFC Window hierarchy.

PFC ControlsAll of the common controls (such as single-line edit, multi-line edit, and listbox) have pfc_equivalent standard user objects.Standard PFC controls implement a variety of functionality, including:

● Cut, Copy and Paste: Editable controls only. ● Popup (right-mouse button) menu: Enables RMB commands including cut, copy, and

paste. ● Autoscroll: Scrolls the list automatically as the user types. Implemented on

DropDownListBox and DropDownPictureListBox. ● Autoselect: Selects the text within an editable control when the control receives focus.

Page 8: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

● MicroHelp: Displays a tag value in the MDI microhelp area when the control receives focus.

Table below lists the standard visual user objects for PFC versions of common controls.

In addition to the standard controls, PFC gives you some advanced controls:● u_dw - the DataWindow control: This control provides enhanced capability,

including integration with the PFC menus, the linkage service, and the Logical Unit of Work (LUW) service for managing the save cycle.

● u_lvs - service-based listview: This control enhances and simplifies the use of the ListView control. The DataSource service provides the capability for display and update of data in a ListView, and the Sort service lets you provide column-header sorting capability in the report view.

● u_tvs - service-based TreeView: Enhances and simplifies the use of the TreeView control. U_tvs uses the LevelSource service to display and update database data in the control.

● u_tab - Tab Control: Includes code to use the Resize service. ● u_tabpg - tab page control: Use this user object as the ancestor for your tab page

controls. ● u_calculator - a calculator control: Can be used as a dropdown calculator in a

(u_dw) DataWindow column or an editmask, or as a standalone calculator with an editmask.

● u_calendar - a calendar control: Can be used with date columns on a u_dw-based DataWindow, or with a date value EditMask.

Page 9: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

● u_st_splitbar - a SplitBar control: Place this between two other controls to allow the user to resize the surrounding controls simultaneously.

● u_progressbar: A programmable vertical or horizontal progressbar control.

PFC ServicesInstantiating PFC ServicesInstantiate PFC services within a program by coding a statement of the formof_set(TRUE)in the object where you want to use the service. Replace in this statement with the name or shorthand that designates the service. Exceptions to this rule are the services that are defined as autoinstantiate objects. Instantiate these objects by declaring a variable of the type of service object either as an instance variable or as a local.Where is the best place to place the call to of_set? Table below lists the service categories, your intended scope, and the place to instantiate the service.

PFC Application ServicesPFC application services are designed to be scoped at the the application level, and should be instantiated in the application manager, gnv_app. The application services are:

● DataWindow Caching: Allows often-used static data to be cached for quicker display. The cached data comes from a single source and is retained, which eliminates repeated retrievals. This is especially useful for dropdown DataWindows containing reference data (such as a table of U.S. states).

Page 10: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

● Debugging: A service that allows logging, SQL debugging (SQL Spy), and application logging.

● Security: A comprehensive, object- and control-based security system. PFC Security gives you atomic control of security - down to the level of individual window controls DataWindow columns, and even menu items.

● Error Message Processing: A service to manage your application's error messages. The error service allows you to format and display error messages, log messages, and generate automatic notifications via e-mail.

● Transaction Registration: A service to keep track of transactions for applications that use more than one transaction.

Page 11: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

PFC Window ServicesThe window services included in PFC are summarized in Table below

PFC DataWindow ServicesPFC provides DataWindow services that greatly enhance the richness of functionality available to the user and developer. Some of the PFC DataWindow services are summarized in Table 4.PFC Global ServicesSome of the services provided by the PFC aren't categorized in any particular way. They can be used in a variety of situations and perform useful utility functions. These are called the PFC Global functions. Some of the global functions provided by PFC are listed in Table below.

Page 12: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions
Page 13: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

Using the PFCThe PFC Start WizardThe easiest way to create a PFC application is through the use of the PFC Start Wizard. Make sure you have a valid workspace open and select "new" from the menu. This will bring up the PowerBuilder New dialog. Select the Target tab and choose "Template Application." Figure below shows the New dialog with the Target tab selected.

Click OK and the Wizard will start. You'll see a confirmation screen. Click Next, then you'll see an explanatory dialog that tells you what steps the wizard will take. Click Next. In the next screen, type a name for your application, such as "pfc_newapp". Be sure to check that the paths for the application library and target have been set correctly. A good way to organize your applications is to keep the workspaces and targets in a "Workspaces" folder and keep the application libraries in a folder under the Workspaces folder. Name the application folder "pfc_newapp" as well. Click Next.The next screen presents three choices: MDI application, SDI application, or PFC-based application. Choose PFC-based application and click Next. The next screen requires you to add the PFC libraries to the target's library list. Simply point to the PFC libraries under the PowerBuilder folder, or make your own copies, possibly in a PFC folder under the Workspaces folder. If you are developing in a networked environment, it's important that you use a local copy of the PFC PBLs (as well as all of your application's PBLs). See the

Page 14: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

section entitled "The Need for Speed" for further information on this point. You must include all 10 PFC libraries in the list. Do notinclude the library called pfc_app.pbl. This library is used by the wizard but is not required in your application. Once you've selected the PFC libraries, click Next.The next screen lets you choose whether or not to create a project for the executable. If you choose Yes, you'll need to:

● Name the project. ● Provide a name for the executable application. ● (Optional) Provide the name of the .pbr (resources) file for the application.

This is the file where you add names of images and such that you want included in the executable.

● Choose build options. Overwrite executable, full versus incremental build. (For the best stability, choose full build. For build speed, choose incremental build.)

● Choose whether to generate machine code. Except in very special, compute-bound circumstances, you should not generate machine code.

● Build Dynamic Libraries (PBD). This is usually a very good idea. Check this box. ● Enter application identification and version information. This is for your use

and should include identifying information for your company and the product as well as copyright and version information.

If you choose to not create a project you can skip this step and create one at a later time.Once you've chosen your project options, you'll see the final screen for the wizard. If you want PowerBuilder to provide you with a quick start TODO list, leave "Generate TODO List" checked. This will give you a list of scripts and objects that need to be added to your application. It's a good idea to use the TODO list at first. Figure below shows the TODO list produced by the PFC Start Wizard.

Page 15: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

Notice that some precoded scripts are provided in the pfcwizsrce folder. Use these scripts as directed by the items in the TODO list to populate events in the application manager. In addition, the TODO list directs you to create a few windows and objects. Believe it or not, once you've done the steps in the TODO list, you'll have a working PFC application. Of course, that application won't accomplish much - that awaits your artful use of the other PFC objects.

. . .

This article is based on PowerBuilder 9: Advanced Client/Server Development by various authors (ISBN 0672325004), published by Sams Publishing. Also look for PowerBuilder 9 Internet and Distributed Application Development.

Page 16: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

The 15-Minute PFC Learning Curve

by Terry VothAs Originally Published in PowerBuilder Developer’s Journal

For years, people have said that PowerBuilder Foundation Classes (PFC) has a steep learning curve. Drives me crazy. Don't believe it for a minute.While it's true that there is a lot to learn about PFC, the same can be said of cars, but few of us learn the intricacies of ignition systems before learning to drive. The fact is, there are three things you need to know before building your application with PFC; the rest can be learned later. We'll discuss the basic benefits of starting with a PFC architecture with minimal understanding later, but for those readers with attention issues, let's get started on the 15-minute PFC learning curve.

Setting Up the Application ObjectFirst create an application directory and copy in everything from the PFC folder, but not the subdirectories. Then create an application with either

1. The New Application Wizard○ Create the application object and application PBL in the directory you've created.○ The wizard finishes.○ Right-click on the new target, select Properties, and add all the PBLs you copied from

the PFC folder to the Library Search Path.○ Create your own application manager by creating an empty descendant of

(PfeApSrv.pbl) n_cst_AppManager in your application PBL, naming it something like n_cst_AppMgr_YourAppName.

○ Export (PfcApp.pbl)Generic_Pfc_App and copy its contents to your application object, starting with the global variable, but with the following exception:

1. When declaring the global variable gnv_App (an important magic key in PFC), make it of type n_cst_AppMgr_YourAppName as created above instead of type n_cst_AppManager.

2. The New Application Template Wizard in PB8 or PB9, or the Wizard downloaded from the PFC - Open Source project (http://pfc.codexchange.sybase.com) in PB10

○ Create the application object and application PBL in the directory you've created.○ Select the PFC-based application option when prompted.○ Add to the application Library List all the PBLs you copied from the PFC folder when

prompted.○ Other options: yada, yada, yada.○ The wizard finishes.○ Create your own application manager, by creating an empty descendant of

(PfeApSrv.pbl) n_cst_AppManager in your application PBL, naming it something like n_cst_AppMgr_YourAppName.

○ Change the generated global variable declaration for gnv_app from n_cst_AppManager to n_cst_AppMgr_YourAppName.

After creating the application object with either of these methods, proceed with the following steps:● In the application object, go into Additional Properties, Variable Types. The message object

should already be set to n_Msg and the transaction object to n_Tr. Change the error object to

Page 17: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

n_Err, the dynamic description area (SQLDA) to n_Dda, and the dynamic staging area (SQLSA) to n_Dsa.

● Remove PfcApp.pbl from your library list and application directory (it was only there for the application model used in the New Application Wizard technique).

● When you think you want to add code to the application object, instead add it to the corresponding event in your application manager. For example, one of the first things you'll want to do is add an Open(<window>) line to the pfc_Open event of your application manager, not the Open event of your application.

Inherit Everything from Objects in the PFE Layer1. Whenever you create a new object, find a PFE object (Pfe*.pbl) to inherit it from.

○ Windows: Inherit from windows in PfeMain.pbl (w_Main, w_Sheet, w_Frame, w_Response, w_Child, w_Popup), but never from w_Master.

○ Custom Classes: If you don't know of a closer class, inherit from n_Base in PfeMain.pbl.

○ Custom Visual: If you don't know of a closer class, inherit from u_Base in PfeMain.pbl.○ Standard Class/Standard Visual: Inherit from the appropriate class in PfeMain.pbl (the

comments identify the class of each object if you can't discern this from the naming convention).

○ Structures can't be inherited, but consider a descendant of n_BaseAttrib with instance variables instead of structure elements. These can be used instead of structures pretty much everywhere structures can be used except external function calls, and hold the opportunity for extension through their object-oriented characteristics.

○ Global functions can't be inherited, but consider grouping (encapsulating) functions of a similar type in an autoinstantiate custom class. Again, it gives you opportunities such as function overloading or overriding if the functional group is ever brought to another application.

2. Whenever you add a new control or embed a new object in a class, find the PFE standard object to use (usually in the PfeMain.pbl).

○ Eventually you'll want to customize your painter toolbars to have many of those PFE objects available directly from the toolbar.

3. Whenever you declare a variable of a standard class data type (e.g., DataStore), use its PFE equivalent instead (e.g. n_Ds).

4. You should never have to directly use or inherit from something in the PFC layer (Pfc*.pbl).

Logical Unit of Work Basics● There is one primary functionality that is forced on you unless you do something to stop it: the

Logical Unit of Work (LUW) service's Save functionality● The LUW will burrow through any window and find any objects that it considers updateable,

including DataWindows, TreeViews, and ListViews. If you know enough about PowerBuilder, these last two shouldn't make sense to you yet, since the native versions of these controls aren't updateable, but that's okay for now. It will bundle all these objects together and treat them as one logical unit of work. In very basic terms, this means that they will all be saved at the same time, and if any of the updates on these objects fail, none of the updates will be saved.

● When you want to save, call the pfc_Save event on the window.● When the user tries to close the window without saving, the LUW will prompt to save changes

Page 18: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

if outstanding changes exist. A failed save will cancel the window closing, giving the user an opportunity to fix the data and have it saved.

● If you want to customize the save process, put code in one of the following events, in the most appropriate object (e.g., window, DataWindow, tabpage).

○ pfc_AcceptText: Performs AcceptText() and similar functionality.○ pfc_UpdatesPending/pfc_UpdatesPendingRef: Returns > 0 if there are updates

pending; determines if the rest of the functionality proceeds.○ pfc_Validation: Checks the data. This is the place you'll most likely want to code, to

put save-time validations. Not all validations are appropriate to be checked on ItemChanged, such as validations that involve more than one field.

○ pfc_UpdatePrep: Any work needed to prepare the data for updating to the database or storage.

○ pfc_Update: The sending of the changed data to the database or storage.○ pfc_PostUpdate: Work that needs to be done after the update.

● If you want to bypass this functionality, either for the entire window or by excluding individual updateable objects, windows and all updateable objects have an of_SetUpdateable(<Boolean>) function.

That's it! That's all you need to know to start programming in PFC. My word processor tells me that's between 900 and 1,000 words, which even includes duplication in branched logic you didn't need to read. At the internationally accepted average reading speed of 250 words per minute, I've got 11 minutes of your time left. Let's delve into some more details.

Why This Is Good EnoughSo, you're confused. You still don't know what the error service is. You're not sure if what you're about to write is correct, PFC-wise or not. That's okay. Even if you start coding with the above information and do everything else "wrong," you've got the following.

Good ArchitectureAfter over 10 years doing PowerBuilder support online, you'd be surprised how often the following conversation occurs:"I've found the following bug and need a workaround/need the following feature.""Use this code.""But I have 30,000 windows/DataWindows/CommandButtons!!""Put the code in your ancestor window/DataWindow/CommandButton.""What? But my grandfather has never even used PowerBuilder!!"Having ancestors to everything saves you more often than you can imagine. Not only is it good for bug fixes and feature implementation, it's good for consistency. I was brought in to fix an application that had no inheritance in it at all. As I walked through the code in about 80 windows, I could see they had written their "save" functionality, started copying it to new windows, then fixed it and started copying that code (without fixing the previous instances), then fixed it again and started copying that version…. When I re-created all those windows (which were for the most part identical except for the dataobject), I put the save code in the ancestor, giving them the opportunity to fix/change their entire system in minutes instead of weeks.The same way a picture is worth a thousand words, a good set of well-architected code is worth a thousand design patterns books. As you have the opportunity to explore PFC as you develop, you'll see all sorts of design patterns that will make much more sense to you than reading a theoretical

Page 19: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

monologue and trying to dig through the examples in LISP. (If you don't know what LISP is, ask your grandfather.)

Good Transaction ManagementI had written a pretty good framework. I was quite proud of it, especially the transaction management. Then I saw PFC's Logical Unit of Work service. It made me completely and utterly miserable. "How could I have missed feature X. Why didn't I anticipate and respond to scenario Y?" It took me a while to realize this is one of the advantages of using a professionally built framework: to be able to start from and build on experts' knowledge and experience. I've seen a lot of transaction management algorithms from both professional frameworks and in-house development, and none have even come near the job that PFC does. Not only is it simple to implement (so simple that you implement it in your windows without knowing it), but it's incredibly extensible, robust and flexible. If I had to start a system with one element from any code or framework I'd ever seen before, I would start with the Logical Unit of Work service.You can make a mess of the GUI and get away with it. You can make the disk footprint too large with little more than a hand slap (in fact, in dollar per gigabyte days, when PowerBuilder's competition uses frameworks over 20M, I personally think disk footprint issues are a bygone issue). But if you screw up the data, you might as well start checking the newspapers for security guard jobs, because you won't be working in IT any more.

A Running StartThere are lots of issues to focus on when starting a project. You want to bypass as much as you can and get development started. Bypass the framework issues and start with PFC. You can have a working application up and running as fast as you can drop DataWindows onto windows and provide access to open them.

OpportunitiesAfter reading this article, you can start coding a PFC application. With some good search tools and good habits using the help files and manuals, you might find functions and services you can use right away. You might not, but that won't stop you from some day going back and adding calls to them, or even from adding the service calls to the ancestors of the objects you're building today (hint: that's what the empty PFE layer is for, but you can learn that another day). Will you make "mistakes" by violating a PFC rule, philosophy or principle? Probably. But the correct answer is, despite what was drilled into you in school: who cares? Your primary goal is to deploy an application that meets your business needs. If you can succeed at that, you've won. And, if in the process of developing that application, you've come a few steps closer to more fully realizing the power of a PFC application, so much the better. Beyond PFC, you've got an infrastructure that makes sharing code easier. Designing generic functionality is a lot easier when you can make assumptions about your environment and architecture. While code that I may write that depends on w_Anc_Sheet.ib_ReadyToUpdate may not be useful to too many people, code that calls LUW functionality on w_Master can be ported to any PFC-based application. You're going to have a lot more opportunities to share code between applications, both within your company and with the outside world. PFC is architected with extensions and customizations in mind. Many of the old frameworks and many of the existing custom frameworks are architected so customizations have to be built into the objects that implement the framework's functionality. If a patched version of the framework came out, you had to reimplement your changes over top of the new framework objects. PFC is built so that you can implement customization and

Page 20: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

extensions yourself or from third parties, and you don't have to worry about them when implementing an upgrade.

Moving ForwardWith seven minutes left, you're wondering what to do with yourself now. I'm going to throw a few quick tips at you to help you on your way.

Your New FriendsGet to know the help file and the documentation. If you haven't, spend 20 minutes (some other day) learning the search tools that come with the help engine; the investment will be returned several times over in productivity.Also, get to know a good code search tool. While PowerBuilder's search is now much more adequate than it was in the early days, PBL Peeper (www.techno-kitten.com) will get you a step further. If you learn the difference between PBL Peeper's rich text search output and DataWindow search output, you'll be able to leverage the tool better. (Hint: The former can bold your match term; the latter can search, sort, and filter your results, and put you a button press away from the actual code.) Learn what PBL Peeper's Lists can give you (particularly the obscured View/Show menu option), as searching them can often be more efficient. And saving the output from PBL Peeper's Reports like the Comment Report can also leave you with a valuable resource.

Thin, Not DeepStarting off, avoid the temptation to dig deep into any one service. Some developers I've met feel a compulsion to read and understand the code before using it. This is counterproductive with respect to frameworks in general. The point of a framework is that you don't need to understand a functionality's contents, only the usage of the implementation. Your first objective should probably be to read a couple of lines of description on each service and object, then review a couple of lines on each function. Sure, the code is there to examine and learn from. Just don't make it an activity that will prevent you from using it.

Avoid MysticismI can't believe the "superstitions" I've heard regarding PFC over the years. "It makes your application crash." "It makes migrations harder." "It makes it harder to debug." Ridiculous! PFC is nothing more than PowerBuilder objects with PowerScript code. There is nothing that makes it special (other than the level of the coders and the volume of testing) any more than any other framework that someone would implement sitting at your workstation. It will make your application crash no more or less than using any other framework, if you ignore the fact that it has been tested by thousands and bug-fixed for years. Migrating a PFC application is no more difficult than migrating any other set of code. (And, no, upgrading PFC is not a required step for migrating, but that's a topic for another day.) Debugging is no more difficult than dealing with any other large framework. PFC is not mystical monkey mojo. It is PowerBuilder code written by developers. Now, I believe those developers had mystical monkey mojo, but that's just a personal theory.

HopscotchThere are times in your life when it pays to force yourself to read from start to finish. It builds character. At least that's what I tell my son when it's chin-dents-in-your-chest boring. PFC reading shouldn't be

Page 21: PowerBuilder PFC Tutorial.docxpowerbuilder.us/misc/PowerBuilderPFCTutorial.docx  · Web viewThe two-layer approach can be modified to suit your enterprise's needs. If you have extensions

like that. After reading the one or two line descriptions on each object and service, make yourself a list of the ones that you think you'll most likely to use (hint: if the DataWindow and DataStore aren't high on your list, you might want to drop PFC and do some Introductory PowerBuilder material) or most interested in implementing. As you'll notice from PBL Peeper (or maybe you won't, the functionality has become so naturally expected from all Windows applications), I'm in love with the Resize service, which resizes and repositions controls as the window resizes. When I implemented that, I got incredible satisfaction, as I think this is important to a user interface design. In my case, this was high on my first-to-learn list. Jump around the manuals. Keep it interesting. Keep your learning time from becoming your nap time.

"And They're Off!"Steep learning curve, my foot!! You're off to the races, and I have four minutes to spare. I'm going to get a coffee. You sit and admire the pretty colors the publisher used.