Skip to content

Latest commit

 

History

History
127 lines (97 loc) · 2.34 KB

tldr-xdata.md

File metadata and controls

127 lines (97 loc) · 2.34 KB

TLDR XData

interface

uses
  System.Generics.Collections, Aurelius.Types.Nullable;

type
  // [Entity] not needed for non-database objects
  TDetail = class
  private
    FDateAndTime: Nullable<TDateTime>;
    FSomething: Nullable<Int64>;
  public
    property DateAndTime: Nullable<TDateTime> read FDateAndTime write FDateAndTime;
    property Something: Nullable<Int64> read FSomething write FSomething;
  end;

  TOverall = class
  private
    FDetails: TList<TDetail>;
    procedure SetDetails(const Value: TList<TDetail>);
  public
    constructor Create;
    property Details: TList<TDetail> read FDetails write SetDetails;
  end;

Un-free

implementation

constructor TOverall.Create;
begin
  FDetails := TList<TDetail>.Create;
end;

procedure TOverall.SetDetails(const Value: TList<TDetail>);
begin
  FDetails := Value;
end;

// destructor TOverall.Destroy;
// begin
//   FDetails.Free; // don't do this!
// end;

Point to the end

uses
  XData.Service.Common,
  MyEntities;

type
  [ServiceContract]
  [Route('/v1/SomeEndpoint')]
  ISomeEndpoint = interface(IInvokable)
    ['{guid}']

    [HttpPost]
    function Calculate(const Input: TSomething): TOverall;
  end;

implementation
initialization
  RegisterServiceType(TypeInfo(ISomeEndpoint));
end.

Implement the answer

type
  [ServiceImplementation]
  TSomeEndpoint = class(TInterfacedObject, ISomeEndpoint)
  public
    function Calculate(const Input: TSomething): TOverall;
  end;

implementation

function TSomeEndpoint.Calculate(const Input: TSomething): TOverall;
begin
  Result := TOverall.Create;

  var Detail := TDetail.Create;
  Detail.DateAndTime := Date;
  Detail.Something := 42;

  Result.Details.Add(Detail);
end;

initialization
  RegisterServiceType(TSomeEndpoint);
end.

Out of control

  • You .Create it
  • You give it to the XData layer
  • Which sends it over HTTP/TCP
  • And then frees it after it's been sent
  • And if you got it from XData as a parameter
  • It also handles freeing

Tricks allowed

  • Raising exceptions will return errors to API clients
  • You can change return content-types
    • TXDataOperationContext.Current.Response.Headers.SetValue('content-type', 'text/html');
  • You can read custom headers (contextual per call)
    • TXDataOperationContext.Current.Request.Headers.Get('my-custom-header')