Show/Hide Toolbars

TMS XData Documentation

Navigation: Service Operations

Service Operations Tutorial

Scroll Prev Top Next More

This chapter describes basic steps you need to follow to implement service operations at server side and invoke them from Delphi side. For detailed information about the steps, please refer to Service Operations main topic. The presented source code samples are not complete units and only display the relevant piece of code for understanding service operations usage. Some obvious code and keywords might have been removed to improve readability. Here we will implement two server operations, one using scalar types (Sum), and another using entities (FindOverduePayments). This tutorial also assumes that you will create the HTTP server and the TXDataServerModule accordingly, with the proper IDBConnectionFactory to point to the database you want the operations to work on.

 

The XData Service Wizard automates steps 1 and 2 for you.

 

1. Define a service interface and declare the operations (methods) in it

 

unit MyServiceInterface;
 
uses
  {...}, XData.Service.Common;
 
type
  [ServiceContract]
  IMyService = interface(IInvokable)
  ['{F0BADD7E-D4AE-4521-8869-8E1860B0A4A0}']
    function Sum(A, B: double): double;
    function FindOverduePayments(CustomerId: integer): TList<TPayment>;
  end;
 
initialization
  RegisterServiceType(TypeInfo(IMyService));
end.

 

This will add the contract to the default entity model. You can use the Aurelius Model attribute to specify the model where the contract belongs to:

 

uses {...}, Aurelius.Mapping.Attributes;
{...}
[ServiceContract]
[Model('Sample')] // adds interface to "Sample" model
IMyService = interface(IInvokable)
{...}

 

2. Create a service implementation class that implements the interface

 

uses
  {...}, MyServiceInterface, 
  XData.Server.Module,
  XData.Service.Common;
 
type
  [ServiceImplementation]
  TMyService = class(TInterfacedObject, IMyService)
  private
    function Sum(A, B: double): double;
    function FindOverduePayments(CustomerId: integer): TList<TPayment>;
  end;
 
implementation
 
function TMyService.Sum(A, B: double): double;
begin
  Result := A + B;
end;
 
function TMyService.FindOverduePayments(CustomerId: integer): TList<TPayment>;
begin
  Result := TXDataOperationContext.Current.GetManager.Find<TPayment>
    .CreateAlias('Customer''c')
    .Where(TLinq.Eq('c.Id', CustomerId) and TLinq.LowerThan('DueDate', Now))
    .List;
end;
 
initialization
  RegisterServiceType(TMyService);
 
end.

 

3. Create the XData Server.

It will automatically detect the service interfaces and implementation. Your operations can now be invoked.

 

4. Invoke the operation using XData client, from Delphi applications.

This tutorial assumes you created the xdata server at base address "http://server:2001/tms/xdata".

 

uses
  {...}
  MyServiceInterface,
  XData.Client;
 
var
  Client: TXDataClient;
  MyService: IMyService;
  SumResult: double;

  Payments: TList<TPayment>;
begin
  Client := TXDataClient.Create;
  Client.Uri := 'http://server:2001/tms/xdata';
  MyService := Client.Service<IMyService>;
  SumResult := MyService.Sum(510);
  try
    Payments := MyService.FindOverduePayments(5142);
  finally
    // process payments
    Payments.Free;
  end;

  Client.Free;
end;

 

5. Invoke the operation using HTTP, from any platform and/or development tool

 

Request:

 

POST /tms/xdata/MyService/Sum HTTP/1.1
Host: server:2001
 
{
  "a": 5,
  "b": 8
}

 

Response:

 

HTTP/1.1 200 OK
 
{
    "result": 13
}