How can I create objects with a link using the instantobjects framework program in Delphi code

I am experimenting with instant objects. I have two simple classes: tband and tcountry, which are defined as saved. tband has properties called tcountry that reference the tcountry class / table. (country - lookup table). When creating a new group, I want the user to be able to select a country in the form from the list / combo, and then save the group object. I tried something like:

Band.Create(nil); Country.Create(nil); Country := CountrySelector.CurrentObject as TCountry; // here I get an exception Band.Country := Country; Band.Store; 

CountryConnector is a TInstantConnector. It has a query "select * from TCountry" and maps the values ​​to a DbComboBox.

0
source share
2 answers

Your code should look something like this. Please note that it is assumed that Connector is connected to the appropriate broker, the database connection is active and that the database schema has been created (you can easily take a later step from the Model Explorer).

 var Band : TBand; begin // Query the database for all TCountry instances or descendants CountrySelector.Close; CountrySelector.Command.Text := 'SELECT * FROM ANY TCountry'; CountrySelector.Open; if ContactSelector.ObjectCount > 0 then begin Band := TBand.Create(Connector); try // Take currently selected Country and assign to Band Country property // Reference counting is handled automatically Band.Country := CountrySelector.CurrentObject as TCountry; Band.Store; finally Band.Free; // Free reference to Band so we do not leak an object end; end; end; 

It should also be mentioned that I do not use the selector very often, since it does not satisfy my typical application requirements. My users often retrieve 20k + records from Firebird using DevExpress Quantum gratings and expect a response time of less than 2 seconds. To satisfy this requirement, a very careful design and configuration was made using IBExpert (an unusual but excellent tool), since my queries often cover many tables and have complex filtering requirements.

What I can say about IO is that it simplifies data modeling because the design boils down to point and click processing using Model Explorer. IO is also a good data binding structure, as it allows you to β€œexpose” even complex object graphs as data sets and associate properties with visual controls. The other part that I like is that I no longer have to fuss around manually, creating insert or update requests and setting column values ​​to add or modify persistent objects. IO does this and more for you behind the scenes with very little coding.

David

+1
source

Does CountrySelector have a non-zero value?

-1
source

Source: https://habr.com/ru/post/890324/


All Articles