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)