DEV Community

Peter + AI
Peter + AI

Posted on

๐Ÿš€ Mastering Uniface Operations: A Complete Guide to ProcScript Modules

Operations in Uniface are the backbone of component-based development, allowing you to create reusable, modular code that can be invoked across different components. In this comprehensive guide, we'll explore everything you need to know about declaring and using operations in Uniface 10.4. ๐Ÿ“š

Note: This article is based on the official Uniface Documentation 10.4, and I had assistance from AI in structuring this content.

๐Ÿ”ง What Are Uniface Operations?

An operation is a ProcScript module that encapsulates specific functionality within a component. Think of it as a method or function that can be called internally or externally, depending on how you declare it.

๐Ÿ“ Basic Operation Syntax

{public | partner} operation OperationName | PredefinedOperationName
   {public soap}
   {public web | partner web }
   {ScopeBlock} ; DSPs only
   {ParamsBlock}
   {VariablesBlock}

   Your ProcScript
{end}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Operation Qualifiers Explained

Access Control

  • public ๐ŸŒ - Default qualifier. Makes the operation available in the component signature for external activation
  • partner ๐Ÿ”’ - Restricts the operation to internal use only within the component

Web Integration

  • public soap ๐ŸŒ - Enables SOAP client access for Service components, DSPs, and USPs
  • public web ๐Ÿ“ก - Allows web browser, RESTful service, or web client access
  • partner web ๐Ÿ”— - DSP-specific qualifier for internal web operations

โšก Predefined Operations

Uniface provides several predefined operations that handle specific component lifecycle events:

Operation Purpose Trigger
exec ๐Ÿƒโ€โ™‚๏ธ Default operation executed when component is activated Component activation
init ๐Ÿš€ Executed when component instance is created newinstance/activate
cleanup ๐Ÿงน Executed when component is removed Component removal
attach ๐Ÿ“Ž Executed when DSP is attached to parent DSP attachment
detach ๐Ÿ”“ Executed when DSP is detached from parent DSP detachment

๐Ÿ’ก Practical Example: Discount Calculator

Here's a real-world example of a service operation that calculates customer discounts:

operation DISCOUNT
params
  string CUSTID : IN
  numeric AMOUNT : INOUT
  numeric PERCENTAGE : OUT
endparams

; Initialize discount
PERCENTAGE = 0

; Apply business rules
if ( CUSTID == "ufbv" ) PERCENTAGE = 20
if ( CUSTID == "acme" ) PERCENTAGE = 15

; Calculate final amount
AMOUNT = AMOUNT * ( 100 - PERCENTAGE) / 100
end
Enter fullscreen mode Exit fullscreen mode

To call this operation from another component:

activate "SERV1".DISCOUNT (ID.CUST, TOTAL.INVOICE, $DISCOUNT$)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Advanced Example: Recursive Operations

Operations can even call themselves recursively. Here's a factorial calculator:

operation FACTORIAL
params
 numeric N : IN
 numeric F : OUT
endparams
variables
 numeric W
endvariables

if ( N > 1 )
 W = N - 1
 activate "calculator".FACTORIAL (W, F)
 F = N * F
else
 if ( N = 1 )
  F = 1
 else
  F = 0
 endif
endif
end
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Web Operations Best Practices

For DSPs (Dynamic Server Pages):

  • Use public web for browser-accessible operations ๐ŸŒ
  • Use partner web for internal operations that still need scope data ๐Ÿ”ง
  • Always define scope blocks to control data exchange ๐Ÿ“Š

For Service Components:

  • Use public soap for SOAP web services ๐Ÿงผ
  • Use public web for RESTful services ๐Ÿ“ก

โš ๏ธ Important Guidelines

Naming Conventions:

  • Maximum 32 bytes length ๐Ÿ“
  • Use letters (A-Z), digits (0-9), and underscores (_) only
  • Must begin with a letter
  • Avoid reserved names: accept, quit, abort, complete โŒ

Declaration Order:

  1. Scope block (DSPs only) ๐ŸŽฏ
  2. Parameters block ๐Ÿ“
  3. Variables block ๐Ÿ“Š
  4. ProcScript logic ๐Ÿ’ป

๐ŸŽ‰ Conclusion

Uniface operations provide a powerful way to structure your application logic into reusable, maintainable modules. Whether you're building traditional desktop applications or modern web services, understanding operation qualifiers and lifecycle events is crucial for effective Uniface development.

Remember to choose the right qualifier based on your access requirements and always follow proper naming conventions for maintainable code! ๐Ÿš€


Have questions about Uniface operations? Drop them in the comments below! ๐Ÿ’ฌ

Top comments (0)