Show/Hide Toolbars

TMS Aurelius Documentation

Navigation: Events

Using Events

Scroll Prev Top Next More

Events in Aurelius are available in the Events property of the TMappingExplorer object. Such property refers to a TManagerEvents (declared in unit Aurelius.Events.Manager) object with several subproperties, each to them related to an event. For example, to access the OnInserted event of the default TMappingExplorer:

 

uses {...}, Aurelius.Mapping.Explorer, Aurelius.Events.Manager;

 

TMappingExplorer.Default.Events.OnInserted.Subscribe(
  procedure(Args: TInsertedArgs)
  begin
    // Use Args.Entity to retrieve the inserted entity
  end

);

 

TMappingExplorer.Default.Events.OnUpdated.Subscribe(
  procedure(Args: TUpdatedArgs)
  begin
    // Use Args.Entity to retrieve the updated entity
  end

);

 

In a less direct way, using method reference instead of anonymous method:

 

uses {...}, Aurelius.Mapping.Explorer, Aurelius.Events.Manager;
 

procedure TSomeClass.MyInsertedProc(Args: TInsertedArgs);

begin

  // Use Args.Entity to retrieve the inserted entity
end;

 

procedure TSomeClass.MyUpdatedProc(Args: TUpdatedArgs);

begin

  // Use Args.Entity to retrieve the updated entity
end;

 

procedure TSomeClass.RegisterMyEventListeners;

var
  Events: TManagerEvents;
begin
  Events := TMappingExplorer.Default.Events;
  Events.OnInserted.Subscribe(MyInsertedProc);

  Events.OnUpdated.Subscribe(MyUpdatedProc);
end;

 

The events are available in the TMappingExplorer object so the listeners will receive notifications about any event fired by any TObjectManager created that references the specified TMappingExplorer object. In other words, the events are "global" for that mapping explorer.

 

Listeners are method references that receive a single object as a parameter. Such object has several properties containing relevant information about the event, and differ for each event type. Names of event properties,  method reference type and arguments follow a standard. The event property is named "On<event>", method reference type is "T<event>Proc" and parameter object is "T<event>Args". For example, for the "Deleted" event, the respective names will be "OnDeleted", "TDeletedProc" and "TDeletedArgs".

 

All events in Aurelius are multicast events, which means you can add several events handlers (listeners) to the same event. When an event occurs, all listeners will be notified. This allows you to add a listener in a safe way, without worrying if it will replace an existing listener that might have been set by other part of the application. You should use Subscribe and Unsubscribe methods to add and remove listeners, respectively. Note that since listeners are method references, you must sure to unsubscribe the same reference you subscribed to:

 

var
  LocalProc: TInsertedProc;
begin
  LocalProc := MyInsertedProc;
  Events.OnInserted.Subscribe(LocalProc);
  {...}
  Events.OnInserted.Unsubscribe(LocalProc);
end;

 

Passing just the method name doesn't work work:

 

Events.OnInserted.Subscribe(MyInsertedProc);
{...}
Events.OnInserted.Unsubcribe(MyInsertedProc); // this will NOT unsubscribe the previous subscription!