Blog

All Blog Posts  |  Next Post  |  Previous Post

Visual Data Binding using TAureliusDataset

Bookmarks: 

Friday, March 22, 2013

When dealing with Aurelius and any ORM framework, one common task is to build a graphical user interface to edit/display the data. Delphi users are used to the TDataset component, which not only retrieves data from the database but also act as middle layer between the data and visual controls. When using Aurelius, you don't use any TDataset descendant to directly retrieve data - all business data are objects that are retrieved by Aurelius itself.

To bind your objects to visual controls, you could use the new Visual Live Bindings feature. But Aurelius also provides an additional way of doing that - you can use TAureliusDataset, a TDataset descendant which behaves as any other TDataset - the only difference is that entity objects are the "data" for this dataset.

Consider the following code:
var
  Customers: TList<TCustomer>;
  Dataset: TAureliusDataset;
{...}
  Customers := Manager.Find<TCustomer>.List;
  Dataset.SetSourceList(Customers);
  Dataset.Open;
The first line retrieves a list of TCustomer objects from the database. The second line tells the dataset that its data is coming from Customers list, and then third line just opens the dataset. Now if we want to display our data in a TDBGrid control, for example, we just do it the way we are used to: link the grid to a TDatasource, then link the datasource to the TAureliusDataset. Your customers will be displayed in the grid.

TAureliusDataset automatically maps each property to a field in dataset. So if your customer is declared like this:
type
  TCustomer = class
  {...}
    property CustName: string read FCustName write FCustName;
you will be able to read/write the property using this code:
CurrentName := Dataset.FieldByName('CustName').AsString;
Dataset.Edit;
Dataset.FieldByName('CustName').AsString := CurrentName + ' - sufix';
Dataset.Post;
You could also edit a single object directly, without needing to retrieve a list. This will also work if you just want to edit properties of a single object:
SpecificCustomer := Manager.Find<TCustomer>(CustomerId);
Dataset.SetSourceObject(SpecificCustomer);
Dataset.Open;
Some could say that you could use the new Visual Live Bindings. Yes, of course, you can. This is just another option, with pros and cons. But it has some interesting/different things:

1. You can use existing data-aware controls. Delphi is now 18 year-old. There are numerous existing controls that support TDataset, but not live bindings. Data-aware grids, planners, controls, etc.. All of those can be used and be bound to the objects.

2. TDataset provides a temporary cache/buffer. This means that until you effectively Post, objects are not changed. Remember this acts as a TDataset. While the dataset is being edited and field contents are updated, only the internal dataset buffer is updated. Data is effectively saved in the objects (the "data") only after Post. This gives you great flexibility when you need to build user interfaces where user can cancel changes, or only update data when clicking "Ok". If you use live bindings, you would have to do something else to achieve such behavior.

Not only that, TAureliusDataset is not just a property->field mapper. It's really powerful. Here is a list of many things TAureliusDataset can do and features it supports (I might write about these in a future post):

  • Fetch-on-demand (will talk about this in a future post)
  • Offline, paged fetch-on-demand (same as above)
  • Sub-properties (properties of associated entities)
  • Entity fields (fields representing an association)
  • Dataset fields (master-detail)
  • Supports inheritance/polymorphism (list of objects of different classes)
  • Enumerated types
  • Lookup fields
  • Locate/Lookup methods
  • Filtered data
  • Calculated fields
  • Design-time support
To conclude, here are two screenshots that illustrate how you can use TAureliusDataset at design-time:








Wagner Landgraf


Bookmarks: 

This blog post has received 2 comments.


1. Tuesday, January 26, 2016 at 11:33:51 AM

Wagner bom dia!

Somos de uma empresa de tecnologia e estamos querendo adquirir o Aurelius estamos fazendo teste e não estamos conseguindo publicar no Load Field as classes criadas. Pode ser que estamos deixando de fazer algo se puder nos mandar um exemplo como feito acima para vermos o que tem de errado.

Obrigado pela atenção

Anderson

Anderson Gonçalves


2. Tuesday, January 26, 2016 at 11:55:02 AM

Olá Anderson, nos envie um e-mail diretamente (pela aba suporte) informando exatamente o que está fazendo. É sempre bom também registrar as classes com RegisterEntity(TNomeDaClasse);

Wagner Landgraf




Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post