Sunday, September 3, 2017

Delphi Entities Validators

How many of you use business objects in your applications?
How many of you use an ORM ?
In this case... how do you validate entities in your applications ?
  1. In a not very elegant way 
  2. The classic is IsValid() method on BO
  3. Something else?
  4. Validation inside or outside entities?
  5. How about different context?
Entity validation can be a tricky beast! From bit Time Professionals Lab... Delphi Entities Validators ! It is a small framework that provides an easy and ready-to-use interface for entities validation.
You can achieve Entities validation in three ways:

  1. Simple validation
  2. Attribute validation
  3. Container validation
The innovative solution of this framework is the Context: in the most case when you have to validate an Entity you have a class explosion for different situations. For example if you have to validate an entity that represents a User you have different Object classes for different context: one class for Login, one for registration etc... because the field that you have to validate are different in these different situations. With Context, the things going to get easier because you can maintain one Object Class and choose wich fields and when to validate them.
So let's go see the code!

Simple Validation

In Simple Validation mode you have to implements the IValidator interface, that use generics, replacing the placeholder with your class. The interface provides Validate method that you must implement, and here you have to write your validation logic. This method returns an IValidationResult wich is composed by:
  • IsValid - returns a boolean that represents the success or failures validation
  • BrokenRules - returns an array of string that represents the broken rules



Attributes Validation

In Attributes Validation you can use a ready-to-use attributes to validate the field of your BO (or create yours). This is first example where Context avoids you a lot of code:


In this case the affected fields are the only that have the context 'AttributesValidation'.

Container Validation

In Container Validation you can use the Simple Validation mechanism adding all the benefits of using a container and context mechanism:


Delphi Entities Validation can be a great ally in writing code, saving you time and code!

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!