有没有办法在派生类中实现更改跟踪而不覆盖基类的 getter 和 setter?
我有一个基于实体框架的多层项目正在开发中,数据访问和业务逻辑最终转移到基于 C# 的服务器应用程序。(数据访问已经迁移)客户端在 Delphi 2010 中。
我使用数据传输对象在客户端和服务器之间传递信息,但是有必要从 Delphi 实现我自己的更改跟踪。我最初是通过从 wsdl 中的 Dtos 继承并“覆盖”getter 和 setter 来实现的。wsdl 导入中的基类(对此没有真正的控制):
// ************************************************************************ //
// XML : DtoCONTAINER_JNL, global, <complexType>
// Namespace : http://k3scs.com/WCF
// ************************************************************************ //
DtoCONTAINER_JNL = class(DtoBase)
private
FJNL_ID: Integer;
FJNL_TYPE_ID: Integer;
FJNL_DATE: TXSDateTime;
published
property JNL_ID: Integer read FJNL_ID write FJNL_ID;
property JNL_TYPE_ID: Integer read FJNL_TYPE_ID write FJNL_TYPE_ID;
property JNL_DATE: TXSDateTime read FJNL_DATE write FJNL_DATE;
派生类:
TDtoCONTAINER_JNL = class(DtoCONTAINER_JNL, IDto)
private
FName : string;
FReferenceCounted : boolean;
_isNew : boolean;
FModified : boolean;
FJNL_ID : integer;
FJNL_TYPE_ID : integer;
FJNL_DATE : TDateTime;
public
property JNL_ID : integer read FJNL_ID write SetJNL_ID;
property JNL_TYPE_ID : integer read FJNL_TYPE_ID write SetJNL_TYPE_ID;
property JNL_DATE : TDateTime read GetJNL_DATE write SetJNL_DATE;
典型的setter方法:
procedure TDtoCONTAINER_JNL.SetJNL_DATE(const value : TDateTime);
begin
ChangedProperties:= DtoGenerics.ChangeTracker('JNL_DATE', value, inherited JNL_DATE, self.IsLoaded, ChangedProperties);
inherited JNL_DATE := DtoGenerics.GetXsDate(value);
end;
典型的getter方法:
function TDtoCONTAINER_JNL.GetJNL_DATE : TDateTime;
begin
Result := DtoGenerics.GetDate(inherited JNL_DATE);
end;
每当设置属性时,更改跟踪器都会将此属性名称添加到更改的属性列表中以返回给服务器。这允许更新语句具有针对性和效率。问题是每个 getter/setter 实际上使派生类与基类不兼容,也就是说,多态性被破坏,并且转换不再按预期工作。
上面的类是根据相应的数据库表/实体从 C# 中的 t4 模板生成的,因此更改 100 多个类应该不是问题。
有人对这个有经验么?
任何建议将不胜感激。
编辑
Wsdl 导入器主要由常量组成,这些常量使用字符串格式或类似的东西来创建复杂类型的 Delphi 等价物。我进行了以下更改,使用类似的常量名称来轻松识别我的更改,以及将它们放置在与预先存在的代码相关的位置。理想情况下,我会完全摆脱继承,只在 wsdl 中使用我的类的修改版本,因为即使我将修改后的 Tdto 转换回 Dto 类,我仍然无法提交它,因为 Web 服务会使用消息拒绝它就像“期待 DtoObject,但得到了 undtoUnit.TdtoObject”。因此,我使用继承来处理 ArrayOfObjects 到 TList 之间的转换,反之亦然。据我所知,以下是更改:
// In WSDLImpConst
// I changed sRemoteClassDeclPas constant value of 'private' to 'proctected'
// Then I added these after SUnitInit
sTrackerDec = sTrackerProcPrefix+ sTrackerProcArgs + sLineBreak;
sTrackerProc = sTrackerDec + 'begin' + sLineBreak +
' Result:= nProps;' + sLineBreak +
' if (_loaded) and (oVal <> pVal) then' + sLineBreak +
' begin ' + sLineBreak +
' Result := TrackChange(pName, nProps);' + sLineBreak +
' end;' + sLineBreak +
'end;' + sLineBreak ;
sTracker2Dec = sTrackerProcPrefix+ sTracker2ProcArgs + sLineBreak;
sTracker2Proc = sTracker2Dec + 'begin' + sLineBreak +
' Result:= nProps;' + sLineBreak +
' if (_loaded) and (oVal <> DateTimeToXSDateTime(pVal)) then' + sLineBreak +
' begin ' + sLineBreak +
' Result := TrackChange(pName, nProps);' + sLineBreak +
' end;' + sLineBreak +
'end;' + sLineBreak ;
sTracker3Dec = sTrackerProcPrefix+ sTracker3ProcArgs + sLineBreak;
sTracker3Proc = sTracker3Dec + 'begin' + sLineBreak +
' Result:= nProps;' + sLineBreak +
' if nProps = '''' then' + sLineBreak +
' nProps := pName' + sLineBreak +
' else' + sLineBreak +
' nProps := pName + '','' + nProps;' + sLineBreak +
' Result:= nProps;' + sLineBreak +
'end;' + sLineBreak ;
sTracker4Dec = sTrackerProcPrefix+ sTracker4ProcArgs + sLineBreak;
sTracker4Proc = sTracker4Dec + 'begin' + sLineBreak +
' Result:= nProps;' + sLineBreak +
' if (_loaded) and (oVal <> pVal) then' + sLineBreak +
' begin ' + sLineBreak +
' Result := TrackChange(pName, nProps);' + sLineBreak +
' end;' + sLineBreak +
'end;'
//Added my own setters:
sRemoteClassSetterImplPas = 'procedure %0:s.Set%1:s(const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
'end;' + sLineBreak;
sRemoteClassSetterImplPas2= 'procedure %0:s.Set%1:s(const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
' F%1:s_Specified := True;' + sLineBreak +
'end;' + sLineBreak;
sRemoteClassSetterImplPas3 = 'procedure %0:s.Set%1:s(const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' ChangedProperties:= TrackChange(''%1:s'', A%2:s, F%1:s, self.IsLoaded, ChangedProperties);' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
'end;' + sLineBreak;
sRemoteClassSetterImplPas4 = 'procedure %0:s.Set%1:s(const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' ChangedProperties:= TrackChange(A%2:s, F%1:s, ''%1:s'', self.IsLoaded, ChangedProperties);' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
'end;' + sLineBreak;
// And for indexed properties
sRemoteClassSetterImplPasIdx2=
'procedure %0:s.Set%1:s(Index: Integer; const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
' F%1:s_Specified := True;' + sLineBreak +
'end;' + sLineBreak;
sRemoteClassSetterImplPasIdx3 =
'procedure %0:s.Set%1:s(Index: Integer; const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' ChangedProperties:= TrackChange(''%1:s'', A%2:s, F%1:s, self.IsLoaded, ChangedProperties);' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
'end;' + sLineBreak;
sRemoteClassSetterImplPasIdx4 =
'procedure %0:s.Set%1:s(Index: Integer; const A%2:s: %2:s);' + sLineBreak +
'begin' + sLineBreak +
' ChangedProperties:= TrackChange(A%2:s, F%1:s, ''%1:s'', self.IsLoaded, ChangedProperties);' + sLineBreak +
' F%1:s := A%2:s;' + sLineBreak +
'end;' + sLineBreak;
// Added these before the SImplDecl constant:
sIntfFactoryDecl = '_di_%0:s Get%0:s(bool useWSDL=false, AnsiString addr="", THTTPRIO* HTTPRIO=0);' + sLineBreak + sLineBreak;
sTrackerProcPrefix = 'function TrackChange';
sTrackerProcArgs = '(const pName: string; pVal, oVal: variant; _loaded: boolean; nProps: string): string; overload';
sTracker2ProcArgs = '(const pName: string; pVal: TDateTime; oVal: txsdateTime; _loaded:boolean; nProps: string): string; overload;';
sTracker3ProcArgs = '(const pName: string; nProps: string): string; overload;';
sTracker4ProcArgs = '(pVal, oVal: TByteDynArray; const pName: string; _loaded: boolean; nProps: string): string; overload';
sIntfTrackDecl = sTrackerProcPrefix + sTrackerProcArgs+ sLineBreak;
sIntfTrack2Decl = sTrackerProcPrefix + sTracker2ProcArgs+ sLineBreak;
sIntfTrack3Decl = sTrackerProcPrefix + sTracker3ProcArgs+ sLineBreak;
sIntfTrack4Decl = sTrackerProcPrefix + sTracker4ProcArgs+ sLineBreak;
// Then in WSDLPasWriter I added my new constant arrays:
SetterImpl2:array[Boolean] of string = (sRemoteClassSetterImplPas2, sRemoteClassSetterImplPasIdx2);
SetterImpl3:array[Boolean] of string = (sRemoteClassSetterImplPas3, sRemoteClassSetterImplPasIdx3);
SetterImpl4:array[Boolean] of string = (sRemoteClassSetterImplPas4, sRemoteClassSetterImplPasIdx4);
// then modified the setter section in 'WriteComplexTypeClass'
{ Setter }
if UseSetGets or GenSpecifiedSupport(Member) then
begin
if GenSpecifiedSupport(Member) then
begin
WriteFmt(SetterImpl2[HasIndexDecl(Member)],[WSDLType.LangName,
Member.LangName,
Member.DataType.LangName])
end
else
begin
if (ContainsStr(BaseName, 'Dto')) and (not ContainsStr(Member.DataType.LangName, 'XS'))
and (not ContainsStr(Member.DataType.LangName, 'ArrayOf')) then
if (ContainsStr(Member.DataType.LangName, 'TByteDynArray')) then
WriteFmt(SetterImpl4[HasIndexDecl(Member)],[WSDLType.LangName,
Member.LangName,
Member.DataType.LangName])
else
WriteFmt(SetterImpl3[HasIndexDecl(Member)],[WSDLType.LangName,
Member.LangName,
Member.DataType.LangName])
else
WriteFmt(SetterImpl1[HasIndexDecl(Member)],[WSDLType.LangName,
Member.LangName,
Member.DataType.LangName]);
end;
// at WriteInterfaceEnd I added
WriteFmt(sIntfTrackDecl, []);
WriteFmt(sIntfTrack2Decl, []);
WriteFmt(sIntfTrack3Decl, []);
WriteFmt(sIntfTrack4Decl, []);
// at WriteInterfaceBegin I added
WriteLn(sTrackerProc, []);
WriteLn(sTracker2Proc, []);
WriteLn(sTracker3Proc, []);
WriteLn(sTracker4Proc, []);
// finally I changed the wsdlImp dpr and replaced 'AnsiString' (or 'widestring' I can't remember) with 'string'