views:

183

answers:

2

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.

A: 

Does the CountrySelector has value other then nil?

birger
CountrySelector is actually active. Connected in the RAD way and populating items in the DBComboBox thru DataSource
+1  A: 

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

David Taylor
20+K is an eccentric amount. My main approach is not to use Delphi's Object Inspector too much. However, since Country actually refers to a lookup table with few records I use it this way. I'm still experimenting with IO, and I managed to progress as much as I expected. Using "stored" I have the table design I desire. And the ease of persistence gets 10 out of 10. Now I'll try to design my own validation on top of IO. I'll try using datamodules and form inheritance. Time to move onward :)