Sunday, August 27, 2017

Delphi Event Bus and MVVM

In last ITDevCon, I held a speech on how to build modern applications with Delphi. In the speech I showed what are the good programming techniques and how to reach them in Delphi. The path was completed by presenting the MVVM pattern as solution. For more info about this pattern I suggest you these books:

  1. John Kouraklis, MVVM in Delphi
  2. Syromiatnikov, A., A Journey Through the Land of Model-View-* Design Patterns.

MVVM intents is to separate the domain logic from the presentation logic in three modules with distinct responsibilities:

  • handle the domain data (Model)
  • View state and business logic (View Model) 
  • Handle user interaction and rendering of the visual user interface (View)
The hardest part ( as in any software development ) is the Separation of Concerns (SOC), to keep separate layers so future changes do not interfere with each other. High cohesion and loose coupling, precisely! 
MVVM can manage UI Synchronization in two ways:
  1. Flow synchronization
    • uses direct calls between user interface components and domain. It is based on sequential command: read user input from text box A, processing it with method B, and write the result to text label C 
  2. Observer synchronization
    • the domain layer must implement a notification mechanism to which components of the user interface layer can subscribe. This allows the user interface components to update the state of the user interface when relevant changes in the domain occur
Both solutions keep the layers separated, but the Flow synchronization forces you to write an enormity of boiler plate code and write an Observer framework from zero ( why reinvent the wheel ? ) would be expensive in terms of developments.
Delphi Event Bus comes in our help! For those that doesn't know Delphi Event Bus (for short DEB), it is a publish/subscribe Event Bus framework for the Delphi platform.
DEB is designed to decouple different parts/layers of your application while still allowing them to communicate efficiently.
Simply put the Subscribe attribute on your subscriber method you are able to receive a specific event, and by specifying the TThreadMode in attribute you can choose the context where to deliver the event.
For example, if you have to interact with the UI EventBus can deliver events in the main thread or If your subscriber does long running tasks (HTTP request), EventBus can also use background threads to avoid UI blocking. Regardless where an event was posted the EventBus will manage Thread synchronization, so you can deliver an Event in the MainThread and the subscriber will receive it on a background thread and viceversa.
Look this demo (you need DMVC for server side part) to check how I resolve UI Synchronization in client/server context with REST request in background!









Sunday, June 25, 2017

Simple retry mechanism in Delphi

Hi all,
I want to share with you a nice unit that provide a retry pattern. How many times you want to execute some code and, if that method throws an exception, you want to retry executing it several times? For example when you try to connect to a service or network resource or a database. The operation was something that failed frequently enough that it was necessary sometimes to retry until it succeeded. With this unit you enable an application to handle transient failures by transparently retrying a failed operation.
Here is the RetryHelperU:
The snippet is very simple to understand: you have to specify the number of retries, the milliseconds between each retry and a TProc that represents the operation to do.
I also added this project to my Delphi Demos repository.
I hope it will be useful!

Tuesday, April 11, 2017

Delphi and Firebase: Remote configuration

Hi all,
as you know Firebase is a backend service that include a data storage service. One of common example of this kind of service is Remote Configuration, that allows you to change the behavior or appearance of your app (ex. change the welcome message of your app) without changing the app source code.
I've just updated the Firebase4Delphi repository with a new demo : RemoteConfigDemo.
In this demo I implemented a mechanism to allow to change the behavior and appearance of your app without publishing an app update.
The process is very simple, you have to:
  1. create a JSON Object in your console project that represents the app configuration
  2. Wraps that configuration in your app
  3. Check when configuration change and communicate it to your app
In this demo you have the possibilities to change the style of your application, according to OS, at run time.

Firebase Console 

AquaGraphite.style configured at run-time
RubyGraphite.style configured at run-time




ASAP I'll post a short video to show the power of this feature.