Wiki
Clone wikiMarshmallow / 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;
#!delphi LDataset.SetDataList<TCustomer>(LCustomers); LDataset.Open;
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*')';
#!delphi LDataset.Sort := 'Age Desc, Name Asc';
Updated