RemoveOwnership

Hi,

I'm using a Multi-Model Design.
In this Design Entities could appear in different Models.

One of these Models is set as Default for an Entity.
Also I want the ObjectManagers to do the MemoryManagement.

So by creating a new Entity-Instance I automatically call AddOwnership, so that the ObjectManager for my Default Model will free this Instance on his own.

Now the created Entity could be saved with another ObjectManager, because the Parent-Entity should be saved within the other Model.

How could I "Evict" the Ownership from the ObjectManager for my Default Model?

Using Evict will not remove the Ownership till the Object is saved (or updated) with the ObjectManager.

Example:

[AbstractEntity, Automapping]
TEntity = class(TNoRefCountObject)
private
  FId: Integer;
protected
  [OnInserting] [OnUpdating] [OnDeleting] // Evict the Entity from ObjectManagerMain, if aArgs.Manager <> ObjectManagerMain
public
  constructor Create; // ObjectManagerMain.AddOwnership(Self); 
  
  function ObjectManagerMain: TObjectManager; // returns the ObjectManager for the first Model (global for User / tenancy for Contact)
  
  procedure Save; // calls ObjectManagerMain.Save(Self);
  property Id: Integer read FId write FId;
end;

[Entity, Automapping]
[Model('tenancy')]
[Model('global')]
TContact = class(TEntity)
private
  FNumber: string;
public
  property Number: string read FNumber;
end;

[Entity, Automapping]
[Model('global')]
TUser = class(TEntity)
private
  FName: string;
  FContacts: Proxy<TList<TContact>>;
public
  constructor Create; // InitialValue for FContacts
  destructor Destroy; override; // DestroyValue for FContacts

  property Name: string read FName;
  property Contacts: TList<TContact> read GetContacts write GetContacts; 
end;

procedure Dummy;
var
  hTel: TContact;
  hUser: TUser;
begin
  hUser := TUser.Create; // AddOwnership to global-ObjectManager
  hUser.Name := 'Dummy';
  hTel := TContact.Create; // AddOwnership to tenancy-ObjectManager
  hTel.Number := '1234';
  hUser.Contacts.Add(hTel);
  hUser.Save; // Now hTel is Owned by global-ObjectManager AND tenancy-ObjectManager, because tenancy-ObjectManager. Evict will not Extract it from the Ownership, because "// ignore not mapped objects"
end;

It would be nice, if there is a Method called RemoveOwnership or something else like this.
Any other Ideas?

Regards

Evict does remove the ownership from the manager, so I suggest you review your code.

I'm also a little bit concerned with your architecture. Of course you can use and build it as you want, but my advice is that a) you don't use global managers; b) you don't mix entities with the persistence mechanism; c) you don't mix models that much. Models were intended mostly to separate databases.

Also note that one entity can be present in more than one model, maybe this information could simplify what you are trying to do.

In this case Evict will not remove the Ownership, because my Entity is not Mapped in FObjects.

Anyway, I think the best way is, that only the ObjectManagerMain should manage my Entity, so the Events will evict the Entity from the ObjectManager it was saved.

Thanks for your advices for my architecture.
I think you got some parts wrong.
a) The ObjectManagerMain is just used within MainThread, so that Forms could easily handle Entities.
b) persistence mechanism is not mixed with the entities. Entities only provide a Method, that returns the ObjectManager from our ModelManager
c) There are some entities, like contacts that are used in different Models. (Same as your TSample in Aurelius-Docs: Multi-Model Design | TMS Aurelius documentation)
If there are other best practices tell me.

If your entity is not mapped in FObjects, why are you adding the object to manager ownership?
I wouldn't recommend a global, long-living manager. Create use and destroy the managers in the shortest time as possible.

Thanks for your recommends.
I'll refactor our architecture, so that this won't be necessary furthermore.

1 Like

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