How to use Delphi RAD with cross-platform in mind

How to use Delphi RAD with cross-platform in mind

So far, I have yet to meet a single developer who challenges me when saying that “Delphi is the best software development tool to develop native Windows applications using the Visual Component Library (VCL)”. Even if developers have moved on from Delphi, as long as people have learned Pascal, have a background writing database applications, you can be certain Delphi was their job in their CV at some point.

What makes me sad that I find out that they left for other products because they thought that Delphi would not offer the efficiency when they had the demand for multiple platforms or mobile platforms.

That is simply not the case as Delphi can generate native executables for Microsoft Windows, Linux, Apple macOS, Apple iOS, and Android.

They key is to build the software with this in mind right from the start even if somebody asks you to “build a Windows client for now to start with“. If I had a penny for each time I have heard this. About the time of the first alpha version, the statement changes to “We need a mobile version as well” and the chosen limiting architecture and UI framework does not work anymore. So, right from the start, stay true to the following.

Separate your UI from your “business logic”

Delphi makes it a joy to separate your forms from the actual code that is the engine of your application. Right with its inception, Delphi offered data modules in addition to forms. And guess what, those are available for all platforms as well. Using data modules and removing your database controls from forms is already a good step to make it easier to migrate to another platform as well. Also, do not write code in event handlers on your form but write classes that are called from your events. This way you will also be able to make changes to your user interface that will not affect your data model.

Computer scientists focusing on architecture will probably scream out at these two basic descriptions as insufficient. However, you need to start and take baby steps. At some point it will become natural and you will be able to look into more complex architectural elements like focusing on using interfaces or elements as Dependency Injection.

Use a visual component framework that is available for all platforms

This is where a lot of Delphi developers make the wrong call in my opinion. When I get the task to build a “quick Windows test case UI”, I always pick a VCL application. You might say: “Doesn’t that not only limit you to VCL but also to all the VCL controls on the form which are specific for Windows?”

Nope!

There is a component set available for Delphi that remedies that fact. It has the wonderfully obscure name of “Framework Neutral Components”. These components and controls have been developed with multiple frameworks and operating systems in mind. All components are available for the VCL, FireMonkey, and TMS WEB Core. Thus, you can use the very same control on each platform. The only thing that does change is the form that you drop these forms on.

Let’s say after I build my Windows VCL application and I need to build the same UI in a mobile Android application, I can create a FireMonkey application but use the same forms with the same controls in FireMonkey as the FNC controls are available there as well.

You learn once and you can cover all the platforms and frameworks!

So, looking back at the example, I posted a few days ago about the Apple Music API. Even though I created a VCL application, I am able to use TMS FNC UI controls to make sure that I can use the same visual controls in FireMonkey. Even moving this application to the Web would be possible. Truly sufficient for building a Web client quickly to open up your application to a wider audience. Long term, you could also pick a more CSS-specific approach using HTML templates instead for the Web version, of course. But time and money are always a deciding factor. Being able to present a solution that does not require different development for all platforms is not only making more sense economically but is also feasible thinking of the amount of different things we as developers need to learn.

Looking at the example

The Apple Music API example that I presented in a previous blog post is a VCL application. How much work does it take me to make sure the controls can be used in all platforms that Delphi supports? Do I have to change how the UI is connected to my data?

No, it is a very straight forward process.

Just replace all the VCL-specific controls with their FNC counterparts and introduce the FNC data adapter. Think of the data adapter like a pump that moves your data from a dataset to an FNC control. The data adapter allows extensive customization what data to transfer. It also contains information how to present each field to all visual controls. This way, the data adapter becomes your centralized component that your can access at design-time and at run-time for anything that deals with your data and how it is linked to your user interface.

At run-time, the application now looks like this.

Only one change in code was necessary at the FNC listbox offers a modern approach to handling its items. Instead of a list of strings, it uses a class to represent its items which makes it easy to tie your data to an item.

In order to populate the list box, the following code is used, for example:

procedure TFrmMain.OnSuccess(Sender: TObject);
var
  LArtist: TArtist;
  LItem: TTMSFNCListBoxItem;

begin
  Names.Items.Clear;

  for LArtist in FApi.Artists do
  begin
    LItem := Names.Items.Add;
    LItem.Text := LArtist.Name;
    LItem.DataObject := LArtist;
  end;
end;

And when selecting an item, the is a separate event for it instead of using OnClick:

procedure TFrmMain.NamesItemSelected(Sender: TObject;
  AItem: TTMSFNCListBoxItem);
begin
  LoadArtist( AItem.DataObject as TArtist );
end;

That’s it. No more changes needed. This form design can now be transferred to FireMonkey or TMS WEB Core.

You will hit your limits learning multiple frameworks and component sets

No matter how efficient or young you are, you will simply not be able to keep track of all different platforms at the same time and develop for all these platforms at the same time. It’s simply impossible considering how quickly software evolves these days.

Apart from picking the Web right away as a starting point and providing your desktop clients with a solution like TMS Miletus, the cross-platform, cross-framework approach from TMS using Framework Neutral Controls (FNC) is truly worth looking into. In my opinion, it will improve your development process significantly when catering for multiple platforms.

Tags: , , , , , , , , ,

Partnerships




Top