请教给位高手指点,关于如何将Treeview控件界面和业务分离的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 nowbelle
  • 开始时间 开始时间
N

nowbelle

Unregistered / Unconfirmed
GUEST, unregistred user!
看了刘艺老师DELPHI面向对象编程思想后,很有感触,想把自己的程序改进一下,但是在关于Treeview界面和业务分离上出现了问题,
原来的代码是在FORMSHOW里面创建Treeview里的内容具体如下:
//初始化TREE
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
dm.ADOQuery1.Open;
while not dm.ADOQuery1.Eof do
begin
treetext:=dm.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=dm.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=Tree.Items.Add(Tree.TopItem,treetext);
dm.ADOQuery2.Close;
dm.ADOQuery2.SQL.Clear;
dm.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
dm.ADOQuery2.Open;
while not dm.ADOQuery2.Eof do
begin
treetext:=dm.ADOQuery2.FieldByName('itemdesc').AsString;
Tree.Items.AddChild(node,treetext);
dm.ADOQuery2.Next;
end;
dm.ADOQuery1.Next;
end;

将这块分离后创建了三个模块,一个DM(DataModule),一个FORM1(界面层),一个U_Maint(业务层,这个是新建的一个Unit,不是FORM)。
DataModule里面放了一个ADOConnection,两个ADOQuery
U_Maint里的代码如下:
unit U_Maint;

interface

uses
Windows, Messages, SysUtils, Variants, Classes,ComCtrls,ADODB,Unit_DM;

type
TMaint= class(TObject)
private
UseDM:TDM;
public
function FieldTree(TopNode:TTreeNode):TTreeNodes;
constructor create;
destructor Destroy;override;
end;

implementation

{ TMaint}

constructor TMaint.create;
begin
UseDM:=TDM.Create(nil);
end;

destructor TMaint.Destroy;
begin
FreeAndNil(UseDM);
inherited;
end;

function TMaint.FieldTree(TopNode:TTreeNode): TTreeNodes;
var
treetext,tabname:string;
nodes:TTreeNodes;
node:TTreeNode;
begin
//初始化
UseDM.ADOQuery1.Close;
UseDM.ADOQuery1.SQL.Clear;
UseDM.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
UseDM.ADOQuery1.Open;
while not UseDM.ADOQuery1.Eof do
begin
treetext:=UseDM.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=UseDM.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=nodes.Add(TopNode,treetext);
UseDM.ADOQuery2.Close;
UseDM.ADOQuery2.SQL.Clear;
UseDM.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
UseDM.ADOQuery2.Open;
while not UseDM.ADOQuery2.Eof do
begin
treetext:=UseDM.ADOQuery2.FieldByName('itemdesc').AsString;
nodes.AddChild(node,treetext);
UseDM.ADOQuery2.Next;
end;
UseDM.ADOQuery1.Next;
end;
Result:=nodes;
end;
end.

最后在FORM1中拖入一个Treeview控件起名tree,在private下添加objectTree:TMaint;
在FORMSHOW里添加
tree.Items.Assign(objectTree.FieldTree(tree.TopItem));

编译成功,但运行后会出错,不知道什么原因怎么修改,请各位高手指教~顺便想请教各位高手有没有更好的分离方法,小弟不胜感激~
 
看了刘艺老师DELPHI面向对象编程思想后,很有感触,想把自己的程序改进一下,但是在关于Treeview界面和业务分离上出现了问题,
原来的代码是在FORMSHOW里面创建Treeview里的内容具体如下:
//初始化TREE
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
dm.ADOQuery1.Open;
while not dm.ADOQuery1.Eof do
begin
treetext:=dm.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=dm.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=Tree.Items.Add(Tree.TopItem,treetext);
dm.ADOQuery2.Close;
dm.ADOQuery2.SQL.Clear;
dm.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
dm.ADOQuery2.Open;
while not dm.ADOQuery2.Eof do
begin
treetext:=dm.ADOQuery2.FieldByName('itemdesc').AsString;
Tree.Items.AddChild(node,treetext);
dm.ADOQuery2.Next;
end;
dm.ADOQuery1.Next;
end;

将这块分离后创建了三个模块,一个DM(DataModule),一个FORM1(界面层),一个U_Maint(业务层,这个是新建的一个Unit,不是FORM)。
DataModule里面放了一个ADOConnection,两个ADOQuery
U_Maint里的代码如下:
unit U_Maint;

interface

uses
Windows, Messages, SysUtils, Variants, Classes,ComCtrls,ADODB,Unit_DM;

type
TMaint= class(TObject)
private
UseDM:TDM;
public
function FieldTree(TopNode:TTreeNode):TTreeNodes;
constructor create;
destructor Destroy;override;
end;

implementation

{ TMaint}

constructor TMaint.create;
begin
UseDM:=TDM.Create(nil);
end;

destructor TMaint.Destroy;
begin
FreeAndNil(UseDM);
inherited;
end;

function TMaint.FieldTree(TopNode:TTreeNode): TTreeNodes;
var
treetext,tabname:string;
nodes:TTreeNodes;
node:TTreeNode;
begin
//初始化
UseDM.ADOQuery1.Close;
UseDM.ADOQuery1.SQL.Clear;
UseDM.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
UseDM.ADOQuery1.Open;
while not UseDM.ADOQuery1.Eof do
begin
treetext:=UseDM.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=UseDM.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=nodes.Add(TopNode,treetext);
UseDM.ADOQuery2.Close;
UseDM.ADOQuery2.SQL.Clear;
UseDM.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
UseDM.ADOQuery2.Open;
while not UseDM.ADOQuery2.Eof do
begin
treetext:=UseDM.ADOQuery2.FieldByName('itemdesc').AsString;
nodes.AddChild(node,treetext);
UseDM.ADOQuery2.Next;
end;
UseDM.ADOQuery1.Next;
end;
Result:=nodes;
end;
end.

最后在FORM1中拖入一个Treeview控件起名tree,在private下添加objectTree:TMaint;
在FORMSHOW里添加
tree.Items.Assign(objectTree.FieldTree(tree.TopItem));

编译成功,但运行后会出错,不知道什么原因怎么修改,请各位高手指教~顺便想请教各位高手有没有更好的分离方法,小弟不胜感激~
 
你上当了 DELPHI语言来说 是没有必要搞个业务层的 = 脱裤子放屁
 
这也是各人所见不同,有些人认为是脱裤子,而另一些人则认为脱得还不彻底,我以为还是不要被这个表象所迷惑,虽然你把代码都贴了出来,但你还没告诉大家,你的代码运行时,出的是什么错呢????
从代码上面来看,在你不用OOP的代码上,这条语句中
node:=Tree.Items.Add(Tree.TopItem,treetext); //原来代码 Tree因该是个实际已存在的控件实体。
而在你的OOP代码中,这个nodes可仅是定义,而没有创建哦。会不会就是这个原因呢???
node:=nodes.Add(TopNode,treetext); //OOP代码中
 
接受答案了.
 
后退
顶部