14 · Architecture, programming style and configurable SQL

◀ Back to contents

This chapter summarises the technical rules used to evolve Factuzam. It is intended for development, advanced support and implementers. The complete repository reference is maintained in LIBRO_DE_ESTILO_DELPHI.md, LIBRO_DE_ESTILO_BBDD.md, PLAN_SOLID.md and MANUAL_SQL_PERFILES.md.


1. Programming style

Factuzam is written in Object Pascal/Delphi VCL and prioritises readable, predictable code that is compatible with its installed base.

Main rules:

Database changes are delivered as idempotent scripts under DESARROLLOS EN CURSO/. The factuzam_original.sql dump is not modified.


2. SOLID applied progressively

Factuzam is migrating in instalments from a legacy core towards a SOLID architecture. This is not presented as a completed rewrite: every extraction first locks down behaviour with tests and reduces coupling without mixing in functional changes.

Principle Application in Factuzam
SRP — single responsibilityThe form coordinates the interface, the data module persists data, and libraries execute business rules. Large responsibilities are extracted into TGestor* collaborators or specific services.
OCP — open/closedPurchase, sale, printing and document variations are modelled through configuration and strategies, avoiding copies of complete forms for every case.
LSP — substitutionBase classes publish coherent contracts and hooks; a base is not extended when its descendants would need to cancel the behaviour with empty methods.
ISP — small interfacesEach consumer receives only the capability it needs. Project interfaces are small, have a GUID, and are grouped only when they share the same implementation.
DIP — dependency inversionThe domain depends on inLib*Intf contracts; UniDAC implementations are created externally and injected from the composition root.

The sequence for an instalment is: a test that fixes the behaviour, extraction of one responsibility, Win32/Win64 compilation, the DUnitX suite, and verification of automatic architectural ratchets.


3. Layers and dependency direction

fzam.dpr / Core (composición)
            |
            v
Forms / Modals (presentación y coordinación)
            |
            v
UniData* (persistencia) ---> inLib* (dominio y colaboradores)
                                  |
                                  v
                            inLib*Intf (contratos)
Layer Responsibility
Core / compositionCreates connections, repositories, services and forms; connects implementations to contracts.
Forms / ModalsDisplays data, requests confirmation, coordinates tabs and translates business results into visual actions.
UniData / DataModulesQueries and persists through UniDAC, controls datasets and transaction boundaries.
inLibCalculations, validations, transformations, orchestrators and reusable collaborators.
**inLib*Intf**Stable interfaces and types without dependencies on VCL, forms or persistence implementations.

A domain library does not create a repository, know about a form or obtain a global connection. Its constructor or parameter receives the contract it needs. Connections, identity and credentials have an explicit owner and life cycle.

Writes affecting several tables are atomic: they respect an existing transaction or perform Commit/Rollback at the same level. Threads do not share datasets or connections with the interface.


4. Configurable and queryable SQL

The SQL catalogue by profile allows certain read queries to be corrected without recompiling or replacing fzam.exe. The domain requests a business operation from a repository; the persistence implementation chooses between:

  1. The base SQL, included and tested with the executable.
  2. Active custom SQL in fza_usuarios_perfiles.

The domain does not receive SQL text and does not expose a generic Ejecutar(SQL) method. Each operation retains a stable key in this form:

KEY_USUPER    = SQL_REPOSITORIOS
SUBKEY_USUPER = SQL__Repositorio__Operacion

Activation by screen

The oGetSQLFromDB profile property enables the catalogue for each consuming form. When the screen opens:

  1. Factuzam loads the definitions from the shared catalogue.
  2. It publishes any missing base operations without overwriting an existing customisation.
  3. It resolves each read against the active profile or base SQL.

Disabling the switch on one form does not change other consumers of the same operation.

The inventory of units that read the switch, publish profiles or provide catalogue definitions, together with the historical traversal of data modules, is in MANUAL_SQL_PERFILES.md.

Validation and safe fallback

Before executing a customisation, Factuzam verifies that it:

If validation fails, the custom query is discarded, the cause is logged and the base SQL runs. If it passes validation but fails to open or returns an incorrect structure, Factuzam retries once with the base SQL. If the base also fails, the error is displayed normally.

Writes do not use this automatic retry: any future write customisation must be protected by a transaction and perform Rollback before changing implementation.

Review, audit and rollback

The catalogue administrator can publish, review and export base and profile definitions. The review displays status, policy, version, hashes, validation and the latest fallback cause. Each row also stores its modification time and user.

To return immediately to the behaviour included in the executable, you can:

  1. Disable an operation by changing its status from S to N.
  2. Delete only its custom row.
  3. Set oGetSQLFromDB=False for the whole screen.

There is no need to deploy another executable. Before editing a query, make a copy of its SQL and metadata; never change parameters or aliases required by the contract.


5. Tests and non-regression rules

The aim is for every improvement to leave an automatic barrier that later code cannot cross again.


◀ Mobile applications · Contents · Next ▶ PrestaShop integration