I try to track the session creation/destruction for debugging reasons. A lot of sites cite Mat DeLong's code to handle TDSSessionManager.Instance.AddSessionEvent, see, for instance, document http://mathewdelong.wordpress.com/category/rad-studio/xe2/, scroll halfway down to the "Session Management" chapter.
There it reads ...
TDSSessionManager.Instance.AddSessionEvent(
procedure(Sender: TObject; const EventType: TDSSessionEventType;
const Session: TDSSession)
begin
case EventType of
SessionCreate: (* session was created *);
SessionClose: (* session was closed *);
end;
end);
Seems like beeing used to old style Pascal I have missed some of the new language constructus OOP added.
TDSSessionManager is a type, not an actual object. How can someone call code in a type? I would have expected something like
var SessionManager : TDSSessionManager;
begin
SessionManager := TDSSessionManager.Create;
...
SessionManager.AddSessionEvent(MySessionHandler);
end;
But wait. I have meanwhile read more about the "Singleton" TDSSessionManager. There can be only one object of this type, so TDSSessionManager.Instance can point to only one real object, I named it "SessionManager", and this is how it works. Is this theory true?
the second arcane thing is how he put the code of his event handler right into the parameter section of the caller. I would have expected something like
Procedure MySessionHandler(Sender: TObject; const EventType: TDSSessionEventType;
const Session: TDSSession)
begin
case EventType of
SessionCreate: (* session was created *);
SessionClose: (* session was closed *);
end;
end;
....
Procedure StartMyServer;
begin
...
TDSSessionManager.Instance.AddEventHandler(MySessionHandler);
...
end;
Would this be possible and equivalent to DeLong's code?
Thanks for more info
Armin.
Yes, it is as you think: the anonymous method can be rewritten to an 'old-school' method.
The only thing which must be considered is that the method signature
procedure(Sender: TObject; const EventType: TDSSessionEventType;
const Session: TDSSession)
looks like it has to be a method of a class, not a normal procedure. (To verify this check the signature of AddSessionEvent). So it would look like this:
procedure TSomeOutherClass.MySessionHandler(Sender: TObject;
const EventType: TDSSessionEventType; const Session: TDSSession)
begin
...
end;