How can I create objects with reference using inst

2019-06-13 02:09发布

问题:

I am a experimenting with instantobjects. I have two simple classes tband and tcountry both are defined as stored. tband has a properts named tcountry referencing the tcountry class/table. (country is the lookup table). When creating a new band I want the user to be able to select the country in the form from a list/combo and then save the band 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 an TInstantConnector. It has a query "select * from TCountry" and displays values in a DbComboBox.

回答1:

Your code should look something more like the following. Please note it is assumed the Connector is connected to an appropriate Broker, the database connection is active and that the database schema has been created (you can do the later step from Model Explorer quite easily).

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's 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;

I should also mention that I do not use Selector very often since it does not address my typical application requirements. My users often retrieve 20k+ records from Firebird at a whack using DevExpress Quantum grids and expect response times consistently under 2 seconds. To meet this requirement took some very careful design and tuning using IBExpert (a quirky but otherwise excellent tool) since my queries often span many tables and have complex filtering requirements.

What I can say about IO is that it makes data modeling easy since the design is reduced to a point and click process using Model Explorer. IO is also a good data binding framework since it allows you to "Expose" even complex object graphs as data sets and bind the properties to visual controls. The other part I like is that I no longer need to fuss with manually creating insert or update queries 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



回答2:

Does the CountrySelector has value other then nil?