What's the best way to refresh an object

I have a trigger that applies some business logic so I want to refresh the object after it has been saved to pick up any changes the triggers makes.  What's the best way of going about this?  I did think I could do this but it didn't work.


  FManager.Save(LoadDetail);
  LoadDetailID := LoadDetail.Id;
  FManager.Flush;
  LoadDetail := FManager.Merge<TLoadDetail>(FManager.Find<TLoadDetail>(LoadDetailID));

I don't want to destroy the object all together as it's used elsewhere just want to ensure it has the latest data from the database

There is no easy way to do that without destroying the object. This is one of the most request features for Aurelius (object Refresh) and we are looking forward to implement it.

Current the workaround would be destroying (or clearing) the manager, without destroying the object (setting OwnsObjects := false) and then doing the merge. But you have to be sure to manage all existing object instances (associations for example).

What is I used interfaced objects for my models - would that work so that I could rely on the reference counting of TInterfaceObject to clear up afterwards?  Then I could set manager's ownsobjects property to false and not worry about freeing up my model objects?

Unfortunately it's not possible.

So what is the recommended way to refresh an object from database?

You mentioned destroying the object, but I understand that usually the ObjectManager takes care of memory management?

If you want to keep the object instance, then there is just not automatic way to do so. You have to load an object, and copy the properties to the existing object. But if you just want to get a new, updated instance of an object (doesn't need to be the same instance) then you can just Clear the object manager and call Find method again.

But this clears all other objects, too, doesn't it?

Is there a way to just refresh this one particular object (or evict only this from object manager cache)?

Yes, unfortunately there is no way to evict a single object from the manager, yet.