Show/Hide Toolbars

TMS XData Documentation

Navigation: Service Operations

Service Implementation

Scroll Prev Top Next More

Once you have defined the service interface, you need to write the server-side implementation of the service. To do this you must:

 

1. Create a class implementing the service interface

2. Inherit the class from TInterfacedObject, or be sure the class implement automatic referencing counting for the interface

3. Add XData.Service.Common unit to your unit uses clause

4. Add [ServiceImplementation] attribute to your interface

5. Implement in your class all methods declared in the interface

6. Call RegisterServiceType method passing the implementation class (usually you can do that in initialization section of your unit).

 

When you create a TXDataServerModule to build your XData server, it will automatically find all classes that implement interfaces in the model and use them when operations are invoked by the client. When a client invokes the operation, the server will retrieve the class implementing the requested service, will create an instance of the class, marshal input parameters, and invoke the method. If the method returns a value, the server will marshal the value again and send it back to the client. The instance of the implementation class will then be destroyed.

 

The following source code illustrates how to implement the MyService interface defined in the topic "Defining Service Interface":

 

unit MyService;
 
interface
 
uses
  System.Classes, Generics.Collections, 
  MyServiceInterface,
  XData.Service.Common;
 
type
  [ServiceImplementation]
  TMyService = class(TInterfacedObject, IMyService)
  private
    function Sum(A, B: double): double;
    function HelloWorld: string;
  end;
 
implementation
 
function TMyService.Sum(A, B: double): double;
begin
  Result := A + B;
end;
 
function TMyService.GetWorld: string;
begin
  Result := 'Hello, World';
end;
 
initialization
  RegisterServiceType(TMyService);
 
end.