Blog
All Blog Posts | Next Post | Previous PostPersonal Finance data in the cloud
Monday, October 28, 2024
In this article, kindly written in cooperation with Softacom, we will show you how to adopt client-server architecture to our Personal Finance software using TMS Business components: RemoteDB, Sparkle, and Aurelius.
Introduction
In today's world, cloud computing has become a key factor in app conception. It enables the development of client/server multi-user architecture where client apps can consume data available in the database behind the server app. Indeed, modern apps can act as clients in a network where they share the same data and updates committed by one user and saved in the central datastore in order to be viewed or synchronized with the others.
Another aspect of modernizing our Personal Finance project is a transformation into a multi-user app that will use data in the cloud (and therefore the data can be available to other Personal Finance users). We will reach our goal by developing a server application that will be connected to the SQLite database (created in the first upgrade) and expose its data to other clients. For this task, we will transfer the whole connection code to the SQLite database from the client side to the server side. To expose the server data, we will use TMS RemoteDB.
Know your tools
TMS RemoteDB enables the creation of 3-tier database applications where clients can perform SQL operations on a remote HTTP server, through a comprehensive set of Delphi components. This will make it easy to convert existing client-server or local applications into 3-tier applications with minimal changes in the source code, as we will show you in this article.
We will rely on two components: TRemoteDBServer, and its client-part TRemoteDBDatabase.
TMS RemoteDB components palette
Creating RemoteDB Server
First, we have to create the server. To do it, we need to go to the New dialog and then:
- Delphi project -> TMS RemoteDB -> RemoteDB VCL Server
- or, Delphi project -> TMS Business -> TMS RemoteDB Server Application (then select among VCL, FMX, Windows service, or Console)
A new project is created, containing:
- GUI form (if you have chosen the VCL, or FMX, or Service module for Windows service) with buttons and code for starting/stopping the service, and a TMemo for logging server actions
- TServerContainer, a TDataModule descendant that will hold all non-visual components with no implemented code or method. These components are
- TSparkleHttpSysDispatcher: the HTTP dispatcher that will handle requests;
- TRemoteDBServer: the DB server, that will wrap the base URL and will be linked to TSparkleHttpSysDispatcher and TAureliusConnection;
- TAureliusConnection: an adapter to Delphi DB connections (such as TFDConnection)
Now, we will transfer the data-layer code from the client side:
- Add the unit generated by Aurelius data modeler DBScheme.pas to the server project and include it in the uses section (along with Aurelius.Engine.ObjectManager and Aurelius.Engine.DatabaseManager) of the server TServerContainer.
- Declare three private fields and four methods within the TServerContainer:
private FDBPath: string; FConnection: IDBConnection; FManager: TObjectManager; procedure FillCategories; procedure FillStores; procedure FillProducts; procedure FillExpenses;
- Open the TServerContainer OnCreate event:
- Cut the database connection code from the client part (DataModule OnCreate event handler) and paste it into the TServerContainer OnCreate event. The database path will be in the executable folder.
FDBPath := IncludeTrailingPathDelimiter(TPath.Combine(TPath.GetLibraryPath, 'PersonalFinance'));
ForceDirectories(FDBPath);
FDBPath := FDBPath + 'PrivateFinance.db3';
var bInit := not TFile.Exists(FDBPath);
FConnection := TSQLiteNativeConnectionAdapter.Create(FDBPath);
TDatabaseManager.Update(FConnection);
FManager := TObjectManager.Create(FConnection);
Initialize the database for the first run
if bInit then
begin
FillCategories;
FillStores;
FillProducts;
FillExpenses;
end;
- Drop a TFDConnection component on the DataModule (name it FDConnection), then add the following code to the TServerContainer OnCreate event (after the previous code):
FDConnection.DriverName := 'SQLite';
FDConnection.Params.Database := FDBPath;
AureliusConnection.AdaptedConnection := FDConnection;
AureliusConnection.AdapterName := 'Firedac';
- Open the TServerContainer OnClose event handler and write the code for destroying the FManager:
procedure TServerContainer.DataModuleDestroy(Sender: TObject);
begin
FreeAndNil(FManager);
end;
For those filling methods, just copy their codes from the client.
- Compile and run the server project. By now you will have a functional TMS RemoteDB Server that will expose data ready to be consumed by other RemoteDB clients.
The resulted GUI form of the RemoteDB Server
Setting the client side
Lets go back to the client DataModule:
- Drop a TRemoteDBDatabase component to the DataModule, and set its ServerUri property to http://localhost:2001/tms/remotedb.
- Add the unit Aurelius.Drivers.RemoteDB to the uses clauses.
- After removing all the initial filling code, open the DataModule OnCreate event handler.
- In the beginning, add code for connecting the TRemoteDBDatabase:
if RemoteDBDatabase1.Connected = False then
RemoteDBDatabase1.Connected := True;
- Change the line for creating the connection. This should be changed
FConnection := TSQLiteNativeConnectionAdapter.Create(FDBPath);
To that:
FConnection := TRemoteDBConnectionAdapter.Create(RemoteDBDatabase1, true);
- Run the client project (The server must be started before running the client).
- Now, you will get the data from the server, and you can add or edit any record by using the EditDialog.
Thats all Folks!
Conclusion
By using TMS RemoteDB components, the transition to multi-user architecture can be performed very quickly, just in a few steps as shown above. There was really no need to think about re-inventing the wheel (thinking about setting routes, middlewares, controllers, etc). It was enough just to create the project from the wizard and drop some visual components. And you got a server project that will expose data ready to be consumed by other clients.
This modernization journey can also ensure the realization of other concepts by means of other TMS components.
Aaron Decramer
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post