Event after an entity has been created by the ObjectManager

Hello,

I have an entity which needs to be initialized. During initialization I need to access some child elements of a one-to-many association. Unfortunately this association cannot be accessed within the constructor of the entity as it has not been properly processed by the ObjectManager.

Or is there any event that is fired when an object becomes a managed object?

Hello @Stephan_Munz, welcome to TMS Support Center!

What do you mean by "initialize" and by "processed by the ObjectManager"?

And entity is just a regular Delphi class, it doesn't have any information about the entity or its persistence. A class can be created by the manager automatically when it's loaded from the database, or it can be created directly by the user and then "added" to the manager when the object is saved (inserted), or even updated.

Hello Wagner,

assume I have a delphi class "TTest" and I retrieve it through ObjectManager.Find. In this case the ObjectManager will create an instance of TTest and populate its fields with the content loaded from the database. I would like to have an event which is fired after an entity has been intantiated (in case of using Find only when it is not present in the cache). Similar to the possibility of using events with OnUpdating and OnInserting (either as a method call from the object manager or as a method within TTest using an annotation).

This would have two benefits for me:

  1. I can run code after creation of an entity I cannot add to the constructor (e.g. code which needs access to the populated data which is set by the objectmanager after the constructor has been called).

  2. I can store a reference to the ObjectManager within my instance of TTest. In this case my object knows which ObjectManager it belongs to.

It could be an event which is fired when a transient object becomes persistent.

Regards
Stephan

What you can do right now is to make your entity classes implement IObjectWithManager interface.

If they do, then the object manager will call the following methods when the entity is added to or removed from the manager.

  IObjectWithManager = interface(IInterface)
    ['{A8006ECE-9F8B-4615-BDD8-F880407CB3AA}']
    procedure SetObjectManager(aObjectManager: TObjectManager);
    procedure RemoveFromObjectManager(aObjectManager: TObjectManager);
  end;

Hi Wagner,

this looks awesome. I implemented the interface and it seems to work like a charm :+1:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.