How to do “Delphi RAD” with Web services
Some developers claim that RAD is only possible for simple things. This post will show you that even complex things like Web services can easily be bound to your form controls with a few clicks!
Delphi bundles its own mapping control that allows the same approach using a dataset. However, I am showing an approach that is explained in detail in my latest video course about TMS XData web services which is available for purchase at a 20% discount right now:
TMS XData offers a bridge of its Web services with the TAureliusDataset control. It allows you to bind object instances and lists containing objects to it. That way, each element on the list is interpret as a record. The property names become field names. You can select at design-time if only certain properties should be mapped to fields. You may even define additional calculated fields.
Of course, we need to add a few lines of run-time code that request the data from the Web service and assign it to the dataset. Mind though, as XData interfaces can be used in the client, you will get type safety for your Web service calls at compile time this way! However, even for non-XData Web services like the result of the Apple Music API shown here this holds true. If you design your own classes for the Web service results, these can be used with TAureliusDateset as well. I discuss this in all of my trainings, but TAureliusDataset is in strong contention for the worst, misleading component name ever. It is not bound to Aurelius or any TMS-specific technology at all! It is a dataset that allows you to bind any list of objects to have the comfort that you are used to from other database query results.
dsAlbums.Close; dsAlbums.SetSourceList( LArtist.Albums, False ); dsAlbums.Open;
In the source code above, LArtist.Albums is of type TObjectList<TAlbum>.
TAlbum = class private // ... public property Genres[ Index: Integer ]: String read GetGenre write SetGenre; property GenresCount: Integer read GetGenresCount; published property Id: Integer; property Href: String; property Name: String; property Url: String; property ArtistName: String; property IsSingle: Boolean; property IsComplete: Boolean; property TrackCount: Integer; property IsMasteredForItunes: Boolean; property ReleaseDate: String; property RecordLabel: String; property Upc: String; property Copyright: String ; property IsCompilation: Boolean; property Notes: TAlbumNotes read FNotes; property Artwork: TArtwork read FArtwork; property Tracks: TTracks read FTracks; end;
Take a close look at the design-time field list that I only bind a selection of the properties. The component also takes care of memory management if needed and offers extensive events for interaction that also allows you to implement application logic to send data changes to the Web service.
If video is not your favorite medium, I would like to point your attention to two of my books that show hands-on examples for working with Web services: