Optimistic versioning and XData

Hi Wagner,

I want to use versioning [version] on my entities then using TXDataClient display a list in a grid. I'd like to not have to close and re-open my TAureliusDataset eveytime I post to get the latest version from the server. Is there a safe way to do this? I suspect you are going to say no but I thought I would ask anyway. I would like to do something like this

procedure TdlgPicklists.adsPicklistsAfterPost( DataSet: TDataSet );
var
 old, new: TPickList;
begin
 old := adsPicklists.Current< TPickList >;
 new := Context.ClientApi.Get< TPickList, TGuid >( old.Id );
 var
 idx := picklists.IndexOf( old );
 picklists[ idx ] := new;
 adsPicklists.Refresh;
end;

procedure TdlgPicklists.adsPicklistsBeforeOpen( DataSet: TDataSet );
begin
 FreeAndNil( picklists );
 picklists := Context.ClientApi.List< TPickList >;
 TAureliusDataset( DataSet ).SetSourceList( picklists );
end;

So it looks like it's your lucky day. :slight_smile:

Actually you have the RefreshRecord which updates the internal dataset buffer with the updated properties of the underlying object. You cannot change the reference to the internal object as you are doing, but you could copy the properties between objects, either manually or using RTTI, for example:

procedure TdlgPicklists.adsPicklistsAfterPost( DataSet: TDataSet );
var
 old, new: TPickList;
begin
 old := adsPicklists.Current< TPickList >;
 new := Context.ClientApi.Get< TPickList, TGuid >( old.Id );
 CopyObjectPropertiesFromNewToOld(new, old);
 adsPicklists.RefreshRecord;
end;

Thanks again Wagner working great now, much appreciated.

class procedure RefreshEntityFromServer< E: TBaseEntityServer >( const Id: TGuid; Dataset: TAureliusDataset; Client: TXDataClient );

class procedure TTenantContext.RefreshEntityFromServer< E >( const Id: TGuid; Dataset: TAureliusDataset; Client: TXDataClient );
var
  Current, Refreshed: E;
begin
  Current              := Dataset.Current< E >;
  Refreshed            := Client.Get< E, TGuid >( Id );
  Current.Version      := Refreshed.Version;
  Current.LastModified := Refreshed.LastModified;
  Current.CreatedAt    := Refreshed.CreatedAt;
  Dataset.RefreshRecord;
end;
1 Like

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