#1 2021-11-09 23:51:30

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

SOA - Pass unknown record

I would like to pass different record types as a result of procedure. Is that possible, or any suggestion how to deal with it?

type
  TVehicleType = (vtBus, vtCar);

type
  TCommonAttributes = packed record
    wheelCount: integer;
  end;

  TCar = packed record
     common: TCommonAttributes;
     power: integer;
  end; 

  TBus =  packed record
    common: TCommonAttributes;
    seats: integer;
  end;

type
  TVehicles = class(TInterfacedObject,IVehicles) 
     procedure GetVehicle(const VehicleType: TVehicleType; out outputVehicle: pointer);  --> pointer is not accepted by mORMot
  end;
implementation

procedure TVehicles.GetVehicle(const VehicleType: TVehicleType; out outputVehicle: pointer); 
begin
   case VehicleType of
     vtBus: begin
                  var busInfo: TBus;
                  busInfo.common.wheelCount := 6;
                  busInfo.seats := 40;
                  outputVehicle := @busInfo;
               end;
end;

I hope I won't have to create the separate methods for all vehicle kinds such as:

procedure GetBus(out outputVehicle: TBus);

 

Any smart solution for such issue?

Offline

#2 2021-11-10 12:42:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: SOA - Pass unknown record

You could use:

1. Several "out" parameters, only one actually with data at output. In mORMot 2, the default values won't be serialized, so unused "out" parameters would not add anything huge at the transmission level.
2. Using a TDocVariantData and a "out variant" parameter.
3. Using RecordSaveJson() and a "out RawJson" parameter.
4. Using RecordSaveBinary() and a "out RawByteString" parameter (if a lot of blob content within).
5. A method-based service.
6. ... ?

But I would not go into this direction, because it may became a very complex API, non self-documented any more.
Any duplicated methods, one per kind of result, seems better for me as API definition.

Offline

Board footer

Powered by FluxBB