Make Proxy key value writeable

If i have field
FProduct:proxy;

I can provide

[XdataProperty]
Property ProductId: string
  read getProductid
...
function TOrderRow.GetProductId: String;
begin
  result := FProduct.value;
end;

But setter is "harder" to implement cause it requires one actually find Object first. In xdata server you can do find using ObjectManager trough TXdataOperationContext but if using pure aurelius code it's not working.
Of course one must be sure that id is valid.

What do you mean by "using pure aurelius code". I don't understand the context of this request.

I mean you are working with simple vcl app without xdata.
Forgot setter in my example

procedure SetProductId (aValue: string);
begin
  om := TXdataOperationContext.Current.GetObjectManager;
  fProduct.value := om.find<tProduct>(aValue);
end;

IMO the om.find the is an extra task which slows down. Checking of validuty is nice in some cases, but sometimes it's not requied.
In that vcl code if try to use the setter like productid := importeddata['productid];
if will fail.
Would be nice to use routines which just assing id to field and do nothing else with that.
So what I wan't is possibility assign keyvalue in my code like loading of orderRow would do, if product is lazy loaded.
.

Just an small tip, if you do not have or need any other info on the Product (except it's Id) there is a 'trick' it will work for some cenários:

procedure SetProductId (const AValue: string);
var
  AProduct: TProduct;
begin
  AProduct := TProduct.Create;
  AProduct.Id := AValue;
  FProduct.Value := AProduct; // No need to use Find to Load the Product
end;

Important: You need to make sure you have apropriate Cascade options on the Association attribute. Example, if after setting productId the TOrderRow object is saved with Flush() than MAKE SURE TO REMOVE TCascadeType.Flush from cascade options (or the Product will also be updated).

HTH

1 Like

Thanks, I'll check this .