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