Delphi
Table of Contents generated with DocToc
Getting Started
Download and Install
To learn and get started with Delphi you can use the Delphi Community Edition.
Getting Started by Example
-
Learn to Program with Delphi Community Edition: Part 2 - Building and Debugging - Learn basic Delphi by writing a simple calculator.
-
Samples Repositories - Information about the samples repositories on Embarcadero RAD Studio.
Next Things to Read and Do
-
Delphi Basics - introduction to Delphi for newcomers, and ready reference for experienced programmers.
-
Learn to Program with Community Edition - Learn basic Delphi and C++ Builder by writing a simple calculator.
-
Delphi Boot Camp Recording - 5 videos introducing Delphi development.
-
Developer Skill Sprints - Fast programming tips, tricks and techniques.
- Help for RAD Studio 10.4 Sydney - Official documentation wiki for Delphi.
-
YouTube: EmbarcaderoTechNet - The “Embarcadero Technologies” YouTube channel.
- GitHub: Embarcadero - Embarcadero on GitHub.
At the end of the page you can find further references including the Books section.
The Object Pascal Handbook is a good read [1].
Improving Daily Work
Language References
-
RTL - Reference about the standard libraries.
-
System - Contains the Delphi run-time library (RTL) units, classes, functions, types, variables, and constants.
-
Date and Time Support - Delphi types, routines, and variables that add Date and Time support.
Keyboard Shortcuts
-
Default Keyboard Shortcuts - a table showing all shortcuts.
-
RADStudio IDE Keyboard Shortcuts Guide - with printable keyboard cheat sheet.
Changing the Default Window Layouts
Static Code Analysis
-
Robust and Powerful Pascal Analyzer for Delphi on Windows - Product presentation for Pascal Analyzer (PAL).
-
New Major Release for Delphi Code Analyzer v2.4 with Free Download - Product presentation for a DelphiParser, a static code analyzer.
Know How, Tips and Tricks
Good Practices
- From every constructor call the base constructor first. This ensures that all members are initialized when a derived constructor expects them to be [1], p. 389
- Every destructor should be virtual. This ensures that all memory allocated by derived classes gets freed even if you call
Free
for a base type [1], p. 389 - From every destructor call the base destructor last. This ensures that all memory allocated by parent classes gets freed after depending memory has been freed in derived classes [1], p. 389
- In a destructor never assume the object has been fully initialized, because the constructor might throw an exception while still allocating objects. Thus, always check
assigned(object)
beforeobject.Free
in the destructor [1], p.390
Tips and Tricks
- Create line breaks in string messages:
'Hello'#13#10'World'
- Path and file name operations:
TPath
,TFile
(cross platform) - Path of the current executable:
TPath.GetLibraryPath
(on iOS this may be the ZIP file containing the executable) - Implementing Smart Pointers is described in [1], pp. 436
Memory Management Insights
While learning Delphi I have encountered the following questions:
Q: Does Delphi have a Garbage Collector? Why do I have to call <MyObjectInstance>.Free()
in a TForm.FormDestroy
method? (see section “Wiring the Buttons to the Code” in Learn to Program with Delphi Community Edition: Part 5 - Putting the Calculator Together)
A: If you use an interface as the variable type, then reference counters are used and the instance is freed automatically. If you don’t use an interface as the variable type, then the instance is must be cleaned up. The entire story about memory management with ownership, weak references and reference counting is told in chapter “13 Objects and Memory” in Maco Cantú: Object Pascal Handbook [1].
Note that in chapter “Smart Pointers in Object Pascal” [1], Marco Cantú introduces a Smart Pointer implementation for Delphi < 10.4 and another one for Delphi >= 10.4. Erik Bilsen disucsses the latter solution in his blog entry Custom Managed Records for Smart Pointers.
Coding Support: Components and IDE Extensions for Delphi RAD Studio
Open Source OR Free for Personal Use
- CodeSite - logging system with specialized log viewers.
- https://bitbucket.org/sglienke/dsharp - MVVM and mocks for Delphi.
- JEDI Component Library JCL - thoroughly tested and fully documented utility functions and non-visual classes.
- JEDI Visual Component Library - over 600 Delphi visual and non-visual Delphi/C++Builder components.
- LockBox 3 - cryptography library.
- MMX Code Explorer - refactoring Browser with Delphi Pascal support.
- Spring4d - collections, enumberables, dependency injection, encryption, reflection, inversion of control / IoC container.
- Virtual Treeview - treeview control.
License Required
- madExcept - exception reporter facilitating crash analysis.
Visual Studio Code Plugins
Visual Studio Code is an alternative to using the Embarcadero RAD Studio code editor.
The following extensions are useful:
- Pascal - Note the extension’s instructions on installing dependencies.
- Pascal-Formatter - installs automatically together with Pascal.
Check the Pascal plugin GitHub page on how to install the GNU Global source code tagging system dependencies. For Windows there is a GNU Global mingw package as part of the MSYS2 / MinGW suite. On macOS, GNU Global it is distributed via Homebrew and MacPorts.
Apart from that, refer to the language agnostic extensions in Visual Studio Code Plugins
Techniques
Unit Testing
Test Insight
Test Insight is a unit testing IDE Plugin for Delphi which eases unit test development.
After installation you can access TestInsight in the IDE by selecting View → TestInsight Explorer.
Bug: TestInsight Explorer does not consider --include
and --exclude
filters.
Stefan Glienke has provided a workaround here: TestInsights seems to not recognize runParam to exclude DUnitX Tests by category.
Apply the suggested fix in the file(s)
C:\Users\<your-user-name>\AppData\Local\Programs\TestInsight\Source\TestInsight.DUnit*.pas
matching your test
framework.
Adapting your Project
In order to make TestInsight work with your project it needs to have the TESTINSIGHT
compiler directive
defined in the project. You can do that quickly with the context menu in the project manager.
In your application you need to use the TestInsight.<framework> unit for the framework you are using. Just call the RunRegisteredTests routine or manually register the Listener/Logger to your test framework.
Continously Testing (Watch Tests)
In Tools → Options / Fremdhersteller (3rd party providers?) → TestInsight check the Run Continous Tests Silently checkbox.
Other Topics
- Tools → Options / Fremdhersteller (3rd party providers?) → TestInsight allows changing the ports used by TestInsight at a later point of time.
DUnitX
- Von 0 auf 100 - DUnitX - Ein Testframework für Delphi - German video introduction to DUnitX and Test Insight.
- Delphi-PRAXIS: Von 0 auf 100 - DUnitX - Ein Testframework für Delphi - Forum to discuss questions regarding DUnitX.
Installing the DUnitX Expert into Delphi
DUnitX includes a UI component.
The following installation steps are taken from the video Von 0 auf 100 - DUnitX - Ein Testframework für Delphi:
- Clone the DUnitX master branch:
git clone https://github.com/VSoftTechnologies/DUnitX.git
- Open the Delphi project matching your IDE from
DUnitX\Expert
- Right click the
DUnitX_IDE_Expert_...bpl
project and select Install - File → New → Other now contains Delphi Projects → DUnitX → DUnitX Project and DUnitX Test Unit
- Select Tools → Options / Environment Options → Environment Variables and add a User Environment Variable named DUnitX with the path to the checked out sources as its value.
Notes:
-
When installing the expert, I received an exception about the expert being registered twice. I ignored it … Is it possible that the DUnitX Expert is already present in Delphi 10.4.2 Sydney?
-
DUnitX can create NUnit compatible output for use in a build server and test result monitoring environment.
Mock Frameworks
Spring4D (Spring.Mocking)
Attention
If you are using the Community Edition of Delphi, then you need to compile the Spring4D projects from within the Delphi IDE. Otherwise, the Build.exe
will not create .dcu
files in the Library
subfolder.
- In the Delphi IDE create all projects in
...\spring4d\Packages\Delphi10Sydney\Spring4D.groupproj
- Run
Build.exe
with build configurationDebug
andUpdate Delphi Registry
checked for your Delphi version
Spring4D Links
- GitHub: Spring4D
- docs.spring4d.org - Spring4D reference documentation.
-
Mocking in Spring4D - Getting started with Mocking in Spring4D.
- Quickly Learn Mocking In Delphi With Spring4D - Video introduction to mocking with Spring4D
Delphi.Mocks
-
GitHub: Delphi.Mocks - GitHub project of Delphi.Mocks.
-
Introducing Delphi Mocks - What is an automatic mock framework?
-
Von 0 auf 100 - Delphi.Mocks ein Mock-Framework für Delphi - video introduction to Delphi.Mocks.
-
Delphi-Mocks Parameter Matchers - place tighter controls on verifying that a mocked interface/object method is called.
Delphi Code Coverage
-
Delphi Code Coverage - creates code coverage reports based on detailed MAP files.
-
Delphi Code Coverage Wizard - wizard to setup and build scripts for Delphi Code Coverage.
Further Information on Unit Testing
- Unit Testing Overview - Getting started with DUnit based unit testing.
- DUnitX Overview
- GitHub: DUnitX - Test framework, taking ideas from DUnit, NUnit and other test frameworks.
- GitHub: DUnitX / Examples - Examples for using DUnitX.
- Delphi-Praxis: DUnitX Forum - Disucss DUnitX topics.
- Test Insight - Delphi plugin simplifying unit test development.
Services and Microservices
Links Regarding Services and Microservices
- Überblick über RAD Server - Overview on RAD Server for developing service oriented architectures.
- RAD Server Deep Dive Webinar - Detailed introduction into service oriented architecture development in Delphi.
- Demo: RAD Server Development - Starting point of “Develop a RAD Server application package” in the first video. Uses a “Retail Store” business scenario - users check-in via mobile app and receive special offers of the day by their location.
- Demo: Embedded Auto Documentation API - Starting point of Swagger API documentation generator.
- DockerHub: radstudio/paserver - Docker container to host RAD Server services.
- RAD Server Deep Dive Webinar - Detailed introduction into service oriented architecture development in Delphi.
- Docker-Bereitstellung für RAD Server - Overview on hosting Delphi services in Linux based Docker containers.
FireDAC Database Access
- Quickly Learn How To Connect And Manage A SQLite Database For Delphi/C++ Builder With SQLite Sample App - Blog Article.
- Defining Connection (FireDAC) - Shows how to create a connection programmatically.
- FireDAC - Overview page regarding FireDAC.
- General (FireDAC) - Overview on FireDAC architecture.
- Executing Commands (FireDAC) - ExecSQL.
- Using SQLite with FireDAC - Introduction and overview on using SQLite databases with FireDAC.
- DLL-Entwicklung (FireDAC) - Considerations for using FireDAC within a DLL (Wait Cursor, FFDGUIxSilentMode, prevent TFDGUIxWaitCursor exception)
Accessing a SQL Lite Database via FireDAC
- GitHub: Repository Pattern - Programmatically using a SQLite DB via FireDAC.
Step by Step: Using FireDAC from the Visual Designer
Configure the DB Connection
- Drag a
FireDAC Connection
onto the designer of your DataSource (Repository) class. Note that the right (grey) side of the FireDAC configuration dialog shows the default settings. - Select Driver ID
sQLite
- Configure Locking Mode
Normal
(Exclusive
mode will hinder development because the running application will lock the DB. HoweverExclusive
mode may be the best for a production ready application) - Configure Database file path
- Caveat: JournalMode facilitates logging all activities on the DB. If you are using many insert / delete operations, the DB will grow quickly. Also inserting many records at once will take much time, if JournalMode is
Persist
. If you don’t need the Journal, then selectDelete
here. - In the FireDAC Object Inspector set LoginPrompt to
False
. SQLite does not provide username / password authentication, but you may want to consider encrypting the DB.
Create a Query
- Drag a
FireDAC Query
onto your DataSource (Repository) class designer. - From the Object Inspector dialog open the
FireDAC Query Editor
- Configure your SQL query with parameters and design time defaults for the parameters
Prepare the Application at Runtime
- In the
BeforeConnect
event handler check whether the DB file exists and then assign the path toConnection.Params.Database
.
Miscellaneous Programming Libraries
-
MVVM Starter Kit - part 1 of 3 blog posts introducing MVVM.
-
System.Assert (RTL) - Create an assertion, i.e. make an expectation (invariant) explicit.
Links and References
Link Collections on Delphi and Pascal
- Awesome Delphi - A snapshot of the Awesome Pascal list just before it changed its scope from Delphi to Pascal.
- Awesome Pascal
Books
[1] Marco Cantú: Object Pascal Handbook - A compendium introducing object pascal and Delphi to beginners.
- ObjectPascalHandbook104 - GitHub Code Repository accompanying the book.
- Object Pascal Handbook Delphi 10.4 Sydney Edition - Official homepage of the book.
[2] Nick Hodges: Coding in Delhpi
[3] Nick Hodges: More Coding in Delhpi
[4] Huw Collingbourne: The Little Book Of Delphi Programming: Learn To Program with Object Pascal
Blogs and Forums
-
Delphi-PRAXIS - German forum on Delphi and associated development tools.
-
Delphi Sorcery - blog by Stefan Glienke, the developer behind TestInsight and Spring4D).
-
Uwe Raabe - by the author of the MMX Code Explorer.
-
tmssoftware.com - software development company providing Delphi components, project work and training services.
-
grijjy - software development company.
-
VSoft Technologies Blog - the authors of Delphi Mocks.
Magazines
Criticism and Troubleshooting
After having used Delphi 10.4 for several weeks, the following issues are bothering me:
Delphi 10.4 IDE
- The refactoring tool seems unreliable. Sometimes the refactoring is incomplete and I have to rename types or methods by hand.
- Refactoring → Inline Variable does not work.
- Introducing folders into an existing project is not intuitive. When trying to do so I had issues with removing an obsolete file from the project - it turned out that the project view just didn’t refresh properly.
- In some cases, the code editor does not refresh compiler warnings once they have been cured.
DUnitX and TestInsight
- TestInsight sometimes does not refresh test status correctly. Sometimes I need to clear the TestInsight window, re-run all tests and then only see that a test has turned from red to green.