How can I use "$expand" with service operations (Client)

Hello,

First, I have all (nearly all) business logic in the server.
In many cases I use service functions. They provide some TList<entity-Type>

Now, I want use the $expand parameter.
In our testtool Postman it woks fine.

for example
function search( APara: TJSONObject): TList<customer> (not a real example)

and "customer" has an association "invoice" and "invoice" has "position"

I call: http://..xDataServer/Functions/../search?$expand=invoce/position

But, how can I call the service function in my client application?

xClient := TXDataClient.Create( TXDataAureliusModel.Get( AModel));
xServiceFunc := xClient.Service<IMyFunc>;

FMyResultlist := xServiceFunc.Search( JSONPara);

Thank you

Addition:

The resultlist goes directly into an AureliusDataset and a FNCGridAdapter.

I think, I have found the problem

Aurelius documentation: TAureliusDataset

It's important to note that sub-property fields are not created by default when 
using default fields. In the example of TCustomer class above, only field "Country" 
will be created by default, but not "Country.Name" or any of its sub-properties. To 
use a sub-property field, you must manually add the field to the dataset before 
opening it. Just like any other TDataset, you do that at design-time, or at runtime:

with TStringField.Create(Self) do
begin
  FieldName := 'Country.Name';
  Dataset := AureliusDataset1;
end;

Is this right?

I have tried "AureliusDataset.SubpropsDepth := 2", but it doesn't work.

Those are different things. One thing is to have the field defined in TAureliusDataset if you want it to display its value. If you are using TAureliusDataset to show the object properties and you want to display the value of property Invoice.Position, then indeed you should either:

  1. Manually add a field named Invoice.Position to your dataset; or
  2. Set SubPropsDepth property to 2 (or 1, in this case), which will tell the dataset to create the fields.

Another thing is to ask the XData client to retrieve the property values expanded. In the case of XData client this is not always needed because it will automatically retrieve the property value when needed. Thus it only matter for performance reasons.

So, creating the field should be enough.

First things first, after you do this:

FMyResultlist := xServiceFunc.Search( JSONPara);

Are you able to read the value you want from code? For example, does the following code:

Position := FMyResultlist[0].Invoice.Position;

Returns a valid and expected value?

Yes, this works. I get a valid result.

I think I'll create the whole record (fields) manually. This has other advantages.

But the property SubpropsDepth doesn't work with AureliusDataset and FNCGridAdapter automatically.

It shouldn't make any difference what code is using the dataset. It's just a regular field definition initialization. If you could provide us with a way to reproduce the issue, please open a new topic and let us know how to reproduce this specific issue with TAureliusDataset and FNCGridAdapter.

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