我正在尝试实现一个接口,以将数据集中的记录转换为 Delphi 的预泛型版本中的 Delphi 记录。我目前不喜欢这个界面,因为它总是需要调用 Supports,如果可能的话,我想避免这种调用,并且想知道是否有更好的方法来做我错过的。
到目前为止,我已经定义了一个导航界面和数据检索界面:
IBaseRecordCollection = interface
procedure First;
procedure Next;
function BOF: boolean;
... // other dataset nav stuff
end;
IRecARecordCollection = interface
function GetRec: TRecA;
end;
IRecBRecordCollection = interface
function GetRec: TRecB;
end;
基本上我有一个具体的基类,它包含一个私有数据集和实现IBaseRecordCollection
每个 RecordCollection 接口的具体类,该接口派生自一个抽象类,实现IBaseRecordCollection
(由implements
属性处理)并实现记录检索例程:
TAbstractTypedRecordCollection = class(TInterfacedObject, IBaseRecordCollection)
private
FCollection: IBaseRecordCollection;
protected
property Collection: IBaseRecordCollection read FCollection implements IBaseRecordCollection;
public
constructor Create(aRecordCollection: IBaseRecordCollection);
end;
TRec1RecordCollection = class(TAbstractTypedRecordCollection, IRecARecordCollection);
public
function GetRec: TRecA;
end;
现在,要使用它,我不得不有一个返回 aIRecARecordCollection
然后搞乱的构建器,Supports
我不喜欢它,因为它总是以这种方式使用。
IE
procedure GetMyRecASet;
var
lRecARecordCollection: IRecARecordCollection;
lRecordCollection: IBaseRecordCollection;
begin
lRecARecordCollection := BuildRecACollection;
if not supports(lRecARecordCollection, IBaseRecordCollection, lRecordCollection) then
raise exception.create();
while not lRecordCollection.EOF do
begin
lRecARecordCollection.GetRec.DoStuff;
lRecordCollection.Next;
end;
end;
尽管这可行,但我并不热衷于supports
通话并将我的 lRecordCollections 和我的 lRecARecordCollections 像这样混合。我原本希望能够做类似的事情:
IBaseRecordCollection = interface
// DBNav stuff
end;
IRecARecordCollection = interface (IBaseRecordCollection)
function GetRec: TRecA;
end;
TRec1RecordCollection = class(TInterfacedObject, IRecARecordCollection)
private
FCollection: IBaseRecordCollection;
protected
property Collection: IBaseRecordCollection read FCollection implements IBaseRecordCollection;
public
function GetRec: TRecA;
end;
但不幸的是,Delphi 不够聪明,无法意识到 IRecARecordCollection 的实现IBaseRecordCollection
在 Collection 属性implements
调用和 TRec1RecordCollection 对象的基础上进行了拆分。
对于实现这一目标的更简洁的方法,还有其他建议吗?
-- 编辑以对@David 的回答做出(更长的)回复,而不是评论中的回复
建议的解决方案:
IBaseRecordCollection = interface ['{C910BD0A-26F4-4682-BC82-605C4C8F9173}']
function GetRecNo: integer;
function GetRecCount: integer;
function GetFieldList: TFieldList;
function EOF: boolean;
function BOF: boolean;
...
end;
IRec1RecordCollection = interface (IBaseRecordCollection) ['{E12F9F6D-6D57-4C7D-AB87-8DD50D35DCA2}']
function GetRec: TRec1;
property Rec: TRec1 read GetRec;
end;
TAbstractTypedRecordCollection = class(TInterfacedObject, IBaseRecordCollection)
private
FCollection: IBaseRecordCollection;
protected
property Collection: IBaseRecordCollection read FCollection implements IBaseRecordCollection;
public
constructor Create(aRecordCollection: IBaseRecordCollection);
end;
TRec1RecordCollection = class(TAbstractTypedRecordCollection, IRec1RecordCollection, IBaseRecordCollection)
private
function GetRec: TRec1;
public
property Rec: TRec1 read GetRec;
end;
没有编译。它抱怨TRec1RecordCollection
找不到与IBaseRecordCollection
. 我还尝试将Collection
属性从 Abstract 移动到Rec1RecordCollection
并重新声明属性,TRec1RecordCollection
结果相同
再深入一点,似乎直接继承类实现IBaseRecordCollection
会起作用,但 Delphi 无法通过使用implements
.