Inject code in TDataSet Events for all datasets in a project

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Inject code in TDataSet Events for all datasets in a project

Postby Ahmed Sayed » Fri Feb 08, 2019 9:59 am

Hi, I am trying to inject a logging mechanism to identify the user created the record from a Users table in the database and set them on the AfterInsert, BeforePost Events for the fields (CreatedBy, CreatedDateTime, ModifiedBy, ModifiedDateTime) if they exists in the dataset instance. The (LoggerUserID, LoggerUserName ) vars are set after the user login but set them here just to test the code.

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.
Ahmed Sayed
Active Poster
Active Poster
 
Posts: 24
Joined: Thu Nov 08, 2018 4:12 pm

Re: Inject code in TDataSet Events for all datasets in a pro

Postby HsiaLin » Sun Feb 10, 2019 8:07 pm

If what you are trying to do is mod an existing component to override some functions, i think you need make your code into a package itself and add the .bpi of the main component you want to integrate with to the "Requires" section of your modded component.
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 310
Joined: Sun Jul 08, 2007 6:29 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 17 guests

cron