![104laptopipadbondi | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/104laptopipadbondi-scaled.jpg?resize=1140%2C694&ssl=1)
One topic I get increasingly asked about Delphi Programming when developers are migrating to the latest version of RAD Studio is “quick ways to improve their applications”. So in this post, I am going to summarise 3 favorites by focusing on the libraries in RAD Studio. If you are new to RAD Studio, then I would also recommend reading these, as they are very easy to implement and provide quick wins to new code too.
As with any actively developing and improving language and IDE, RAD Studio / Delphi is improving all the time. This summary is based on the questions I receive weekly focusing on what is in the box so to speak. While I barely scratch the surface here, I also want to point out that there is a great 3rd party community of developer tools offering even more options that you can explore.
Table of Contents
1) Use the Parallel Programming Library
The Parallel Programming Library in RAD Studio is one speed up that is easy to add and provides multiple approaches for speeding up code; from simplistic multi-threading to asynchronous programming. It’s very easy to retrofit with limited risk to existing code, enabling you to make better use of modern hardware capabilities.
e.g. I recently worked with a project that had a few hundred images to download and cache, originally this was done in sequence. (note this code has been updated to use inline variables to make it easier to read here for illustration). TSimpleFileDownload is a class in my code, not expanded here, but useful for illustration.
1 2 3 4 5 6 7 |
For I := 0 to Pred(Length(ImagesToCache) do begin var Img := ImagesToCache[I]; try TSimpleFileDownload.DownloadFile(Img.URL,Img.TempFile); except Img.TempFile := ''; end; end; |
This is a great candidate for swapping to the TParalell.for loop.
1 2 3 4 5 6 7 8 9 10 |
TParallel.&For(0, Pred(Length(ImagesToCache)), procedure (I : Integer) begin var Img := ImagesToCache[I]; try TSimpleFileDownload.DownloadFile(Img.URL,Img.TempFile); except Img.TempFile := ''; end; end); |
With each download running in parallel, this changed slashed the original download time from around 50 seconds to under 4.
Another Parallel Programming option is to run different tasks in parallel. Especially useful when multiple actions need to happen together. e.g. A new customer creates an order, and you then need to push out multiple actions to different systems.
The great thing about all features of the Parallel Programming Library is they are 100% cross platform already! That means they work on desktop, server or mobile applications!! – They really are multi-threading programming, the easy way!
Click below for more blogs detailing the features of Parallel Programming in Delphi.
- Introduction and TParallel For
- ITask – Running tasks in Parallel
- IFuture – Enabling a task to run in the background that requires a result before you continue later in a block of code
- ThreadPool – You shouldn’t need to tweak this, but it has some handy info allowing you to control how much of the device you use.
2) Discover FireDAC
FireDAC probably deserves a top tips list of its own, but I will quickly highlight a few of the very nice features! Originally written as a replacement for the BDE, it provides full bi-directional dataset access (as the BDE did) with a host of new features that make working with data from databases and even external files at the same time a breeze. For starters, it will provide improved database performance, (as described later) and also works across all supported platforms, perfect for getting 64bit and mobile support into your code.
Migrating Delphi code to FireDAC? Look at reFind!
When migrating to FireDAC you will find the fastest way to do that is with reFind, which has excellent automated code conversion example scripts in the RAD Studio samples folder. Alternatively, 3rd party tools like Delphi Parser are great, especially if you are using C++Builder.
FireDAC has so many speed improvements. Swapping ADO for FireDAC (when using SQLServer) will mean a much faster and more stable connection, as I am constantly told by developers who made the jump.
Record Set Fetching and Caching
FireDAC offers advanced Rowset Fetching, Rowset Paging and Delayed Fetching options. This means you can delay fetching records and blobs until they are needed. This reduces network traffic and also speeds up loading.
ArrayDML
For database, such as InterBase, that supports batch inserting of records. ArrayDML is a great feature to speed up inserting records. You may also like the video on using ArrayDML
Type Mapping
Finally, more a speed up in terms of migration and time to market, FireDAC has the great ability to map data types that are often different in databases. (e.g. TSQLDateTime to TDateTime). This is especially handy when migrating from older data storage, or when supporting multiple databases engines. FireDAC allows the management of mapping types at the TFDDataSet or TFDConnection level. Opting for a connection-level setup enables all connected components to inherit the config. If you need to override, you can always override at a component level the mappings.
To add new data type mappings, open the FireDAC Connection Editor (double click on a TFDConnection in your project at design time) and under Options, Format Options, Data Mapping Rules, tick Ignore Inherited Rules and Add Rules.
Read about FireDAC Data Type Mappings
There is so much more to FireDAC…
There is so much more to FireDAC, including LocalSQL, BatchMove, SQL Monitoring tools and more… Oh.. and don’t forget Enterprise connectors, that enable rapid connection to 70+ Enterprise systems, with FireDAC, and provide the data as a standard TDataSet to work with!
I will give however a special shout out to the Monitoring tools. Using them to check your SQL being used at run time is a great way to identify if you have bottlenecks in the code around data access, but that’s an entire article on its own.
3) AppTethering
Unlike my first two choices, which are about updates to existing code, my final choice is about the first steps to expanding existing applications beyond the Desktop. Other options I considered when writing this article were the excellent:
- REST & JSON support, which are ideal for expanding into the microservices that exist today.
- Extended RTTI that gives true object reflection at runtime, very useful, and used widely in new platforms like RAD Server, for example, to power documentation generation through attributes
While both the above would be great areas to look at, they are more about refactoring existing code. Additionally, looking into WebBroker, DataSnap and RAD Server bring a much steeper learning curve, hence my ultimate choice of AppTethering.
Rather than talk too much about AppTethering here, I will share a link to a previous blog on the topic which has a great video introducing sharing of data. https://delphiaball.co.uk/2014/06/05/apptethering-leaderboard-passing-objects-remotely-and-using-resources/
I have seen many great examples of AppTethering, and its a great way to grab mobile device functionality (such as grabbing a photo, location co-ordinate, or anything else from a phone sensor) and sharing it back to a desktop app. One personal favorite was a Medical CRM system that Doctors are using where they were able to capture images of patients back onto their records on their existing desktop app to improve documentation. AppTethering acted as a bridge between the phone and the existing code, with a simple receive event used to patch in the bitmap!
This is a great way to bring extra value to existing code and solutions in little time, and help justify the work of migration. Its also a great way to share data between apps on the same network in live time, as the blog shows.
What else to focus on?
Well, that depends on the path you want to take? If you want to just keep old apps ticking over, or if you want to use a low-risk approach to supercharge your monetization and return on investment you already have made.
There are many improvements and new core unit libraries that are especially worth investing time into learning if you want to move multi-platform. They can dramatically reduce the amount of coding needed, and make the single code base reality a lot simpler to work with.
The chance to migrate to newer versions isn’t just about benefiting from some great libraries you might not have had access to in the past, but for many is about ensuring you are truly Windows 10 ready, and set for the future. To that end, I would also suggest understanding the power of the Windows Desktop Bridge (something RAD Studio was the first IDE in the world to support). This is basically sandboxing of Windows apps and enabling them for distribution via Windows App Stores. – Happy coding!
The post 3 Libraries for improving Existing Applications built with Delphi appeared first on Stephen Ball’s Technical Blog.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition