• Stars
    star
    106
  • Rank 324,022 (Top 7 %)
  • Language Pascal
  • License
    Mozilla Public Li...
  • Created almost 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fluent, declarative query API for Collections/Containers and Enumerations in Delphi

What is FluentQuery?

FluentQuery allows you to operate on containers of items in Delphi in a fluent, declarative fashion.

Huh?

OK, I agree, that was not very helpful.

Let's say you have a TListbox of names, and you want to display the first two that start with 'Miss' (case-insensitive) and contain a hyphen.

The standard solution in Delphi and AppMethod would be something like this:

var
  LName : String;
  LFoundCount : Integer;
begin
  LFoundCount := 0;
  for LName in Listbox1.Items do
    if (LName.StartsWith('Miss', True)) and
       (LName.Contains('-')) then
    begin
      Inc(LFoundCount);
      ShowMessage(LName);

      if LFoundCOunt = 2 then
        break;
    end;
end;

There's nothing incorrect about that code, but it's unclear both in terms of what items in the listbox we're acting on, and unclear what we're doing to those items. This is because the code that selects the strings we want to display, and the code that actually displays them, are all mixed in together.

FluentQuery lets you represent the same thing like this:

var
  LName : String;
begin
  for LName in StringQuery
                 .From(ListBox1.Items)
                 .StartsWith('Miss')
                 .Contains('-')
                 .Take(2) do
    ShowMessage(LName);
end;

Yes, it's less code, but more importantly, it is much clearer. The statement of what items we're going to act on (in the for..in statement) reads fairly naturally, and the code that acts on the items is seperate and self contained.

You can of course include the same operation multiple times in your query, eg:

StartsWith('Miss').Contains('-').Contains('VII').Take(2)

Not just for strings

FluentQuery is not just for strings in Listboxes though. Want all the char's in a string that are either letters or digits? Use:

for LChar in StringQuery.From(FMyString).IsLetterOrDigit do

If FluentQuery does not contain a query operation you need, no problem. You can use the Where operation which takes a TPredicate<T> to do your custom querying. This example finds all TPerson objects in your TObjectList<TPerson> that are over 18:

var
  LPerson : TPerson;
  LOver18 : TPredicate<TPerson>;
begin
  LOver18 := function (Person : TPerson) : boolean
             begin
               Result := Person.Age > 18;
             end;

  for LPerson in ObjectQuery<TPerson>
                   .Select
                   .From(FPersonCollection)
                   .Where(LOver18) do
  begin
      // do something here with your adult LPerson
  end;
end;

Not just for..in loops

FluentQuery is not just for for..in loops though. In the prior example, if you only want the First TPerson object over 18, your query could look like this:

  LPerson := ObjectQuery<TPerson>
                   .Select
                   .From(FPersonCollection)
                   .Where(LOver18)
                   .First;

You can export the results of a query as another collection, such as:

var
  LStrings : TStrings;
begin
  LStrings := StringQuery
                .From(FStrings)
                .Skip(4)
                .Take(3)
                .ToTStrings;

You can also use FluentQuery to define a TPredicate<T> for use in other classes, such as this example filtering a listbox:

Listbox.FilterPredicate := StringQuery.EndsWith('e').Predicate;

The goal is to define a declarative query language for any Delphi container.

Containers

FluentQuery currently supports querying over a bunch of different containers, including:

  • Anything with a TEnumerator<T>, such as TList<T>, TObjectList<T>, etc
  • Strings in a TStrings, such as TStringList, etc
  • Chars in a String
  • Integers in a generic container, such as a TList<Integer>, etc
  • Pointers in a TList
  • JSON Objects in a JSON document
  • Records in a TDataset
  • Components on a Form
  • Files/Folders in a folder/subfolders

A larger list is on the wiki, but ultimately, check the source.

Query Operations

FluentQuery determines which query operations are available to you by looking at the type for which you are querying. For example, if you are querying for strings, you have operations such as StartsWith, EndsWith, Contains, etc.

For a larger list of operations available for different types, check the wiki or even better, check the source

Back Story

This issue of mixing Selection code and Action code has bugged me for awhile, but I've never really found a solution I'm happy with. I tried some experiments but was never satisfied that the cure was better than the disease. A series of functional languages sessions at the recent Yow Conference in Australia was the light bulb moment that led me to this.

More Repositories

1

TStateMachine

Generic State Machine framework for Delphi
Pascal
91
star
2

menialtasks

Simple MVVM in Delphi example from my Intro to MVVM session from CodeRage 7
Pascal
40
star
3

generics.tuples

Generic Tuples support for Delphi and AppMethod
Pascal
29
star
4

delphi-samples

Sample code from presentations and www.malcolmgroves.com
Pascal
24
star
5

reaper_csi

CSI Templates for Reaper
Lua
22
star
6

drawerform

Using FireMonkey Layouts, Animations and Gesture support to create a Facebook-style layout for your mobile app.
Pascal
17
star
7

FluentLiveBindings

Simple library to make creating LiveBindings in code much easier.
Pascal
15
star
8

delphi-experiments

Proof of concept hacks from www.malcolmgroves.com
Pascal
11
star
9

servicelocator

Delphi implementation of the Service Locator pattern
Pascal
7
star
10

firemonkeymessaging

Example of using TMessageManager and custom TMessages as a simple in-process Application Service Bus on Windows, iOS and OSX
Component Pascal
7
star
11

crmvvm

Example Application from Pragmatic MVVM course
Pascal
6
star
12

menupalette

OpenTools addin for Delphi and C++Builder to add menu items to the Tool Palette
Pascal
5
star
13

livebindings

Livebindings examples
Pascal
5
star
14

tethering_series_delphi

Delphi sample code from the App Tethering series at www.malcolmgroves.com
Pascal
4
star
15

ESDPlugin

A simple plugin for the Elgato Stream Deck written in object pascal
Pascal
4
star
16

bandapp

Temporary home for the Delphi XE4 iOS sample app, BandApp
Component Pascal
2
star
17

apptetheringsamples

Samples from my App Tethering presentations
Pascal
2
star
18

dockerintro

Files from Practical Introduction to Docker session
PLpgSQL
1
star
19

ibsite

CodeSite logging in Interbase SQL
Pascal
1
star