The project itself is written in C++ but because I know C++ does not support interposer classes so I had to created it in Delphi. When i use this unit in my project nothing happens I even tried to raise an exception instead of the actual code to check if it does anything or not. I am not so good at Delphi.
I don't wanna end up setting the AfterInsert, BeforePost events for about 50 TDataSet instances. Knowing that Some Datasets have their own OnAfterInsert, OnBeforePost events assigned.
Here is the code:
- Code: Select all
unit DataSetLogger;
interface
uses
Data.DB, System.SysUtils;
type
TDataSet = class( Data.DB.TDataSet)
protected
procedure DoAfterInsert; override;
procedure DoBeforePost; override;
end;
var
LoggerUserID : Integer = 99;
LoggerUserName : string = 'Ahmed Sayed';
CreatedByField : string = 'CreatedBy';
CreatedDateTimeField : string = 'CreatedDateTime';
ModifiedByField : string = 'ModifiedBy';
ModifiedDateTimeField : string = 'ModifiedDateTime';
//---------------------------------------------------------------------------
implementation
//---------------------------------------------------------------------------
procedure TDataSet.DoAfterInsert;
begin
// inherited;
raise Exception.Create('DoAfterInsert');
// FindField(CreatedByField).AsInteger := LoggerUserID;
// FindField(CreatedDateTimeField).AsDateTime := Now;
inherited;
end;
//---------------------------------------------------------------------------
procedure TDataSet.DoBeforePost;
begin
// inherited;
raise Exception.Create('DoAfterInsert');
// FindField(ModifiedByField).AsInteger := LoggerUserID;
// FindField(ModifiedDateTimeField).AsDateTime := Now;
inherited;
end;
//---------------------------------------------------------------------------
end.
Now is there any other proper way to do this? If this is the only way why doesn't it work as expected?
What am i doing wrong?
Any help will be appreciated. Thanks in advance.