Wiki

Clone wiki

Marshmallow / Data binding

General data binding

It is very common to bind your models with GUI elements when working with ORM framework. We recommend using DSharp data binding framework because it's easy to start with, it's not over engineered and it works even with Delphi XE. Also delphi-oop library has some extensions which helps to implement MVC in your Delphi applications using DSharp bindings. Of course, if you are using Delphi XE2 or higher you can use Delphi native LiveBindings.

Binding to DB Grids

Most Delphi developers who are dealing with databases are using DB Grids to represent and edit their data. Since Db Grids expect TDatasource (which on it's own expects TDataset) to be their data source it is impossible to use your model classes with DB Grids straight away. Luckily Marshmallow has TObjectDataset which can be attached to any TDatasource and likewise to any DB Grid.

Using TObjectDataset

Using TObjectDataset is very easy and straightforward. Suppose we have our models loaded into the generic list and have our TObjectDataset instance:

#!delphi

var
  LCustomers: IList<TCustomer>;
  LDataset: TObjectDataset;
We just need to set our list to the dataset:

#!delphi

LDataset.SetDataList<TCustomer>(LCustomers);
LDataset.Open;
And that's it! Dataset is opened and we are able to see and edit our data in the DB Grid or any other DB aware control.

Note that fields in the dataset will be created based on your model properties, containing [Column] attribute.

Advanced TObjectDataset features

  • Filtering. You can filter dataset using filter expressions, e.g.

#!delphi

LDataset.Filter := '(Age >= 18) or (Name Like '*Tom*')';
* Sorting. Sort expressions where you can specify more than one field and use different sort directions are available, e.g.

#!delphi

LDataset.Sort := 'Age Desc, Name Asc';
* Speed. Although our dataset uses Rtti to get fields data, it outperforms TADODataset in almost any operation. There isn't any bottleneck when accessing RecNo or RecordCount, also there is no slowdown when sorting filtered dataset.

Updated