如何在Delphi代码中使用InstantObjects框架以引用方式创建对象 [英] How can I create objects with reference using instantobjects framework programmaticaly in Delphi code

查看:98
本文介绍了如何在Delphi代码中使用InstantObjects框架以引用方式创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用InstantObject.我有两个简单的类tband和tcountry都定义为存储. tband有一个名为tcountry的属性,引用了tcountry类/表. (国家/地区是查找表).创建新乐队时,我希望用户能够从列表/组合框中选择表格中的国家/地区,然后保存乐队对象.我尝试过类似的事情:

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是一个TInstantConnector.它具有从TCountry中选择*"查询,并在DbComboBox中显示值.

CountryConnector is an TInstantConnector. It has a query "select * from TCountry" and displays values in a DbComboBox.

推荐答案

您的代码应类似于以下内容.请注意,假设连接器已连接到适当的代理,数据库连接处于活动状态,并且数据库模式已创建(您可以从Model Explorer中轻松地进行下一步).

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;

我还应该提到我不经常使用Selector,因为它不能满足我的典型应用程序要求.我的用户经常使用DevExpress Quantum网格从Firebird一次检索20k +条记录,并期望响应时间始终在2秒以下.为了满足此要求,我使用IBExpert(一种古怪但极好的工具)进行了非常仔细的设计和调整,因为我的查询通常跨越许多表并且具有复杂的过滤要求.

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.

关于IO,我可以说的是,它使数据建模变得容易,因为使用Model Explorer可以将设计简化为简单的点击过程. IO也是一个很好的数据绑定框架,因为它使您可以将复杂的对象图公开"为数据集,并将属性绑定到可视控件.我喜欢的另一部分是,我不再需要手动创建插入或更新查询以及设置列值来添加或修改持久对象而大惊小怪. IO只需很少的代码就能为您做更多的事情.

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.

大卫

这篇关于如何在Delphi代码中使用InstantObjects框架以引用方式创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆