Creating the Server application (TObjects and DataSnap Tutorial)

From RAD Studio
Jump to: navigation, search

Go Up to Tutorial: Passing Plain Old Delphi Objects with DataSnap

Creating the Server application

To build the server application, follow the next steps:

1. Open RAD Studio.

2. Select File > New > Other.

3. Expand C++Builder Projects or Delphi Projects from the left column.

4. From the DataSnap Server node, select DataSnap Server and press OK.

5. Specify the type of application:

  • Select Forms Application and press Next.
  • Select FireMonkey application and press Next.
  • Keep the default values to use TCP/IP Protocol.
  • Uncheck the Sample Methods checkbox and press Next.
  • Press Test Port to verify whether the Port 211 is open and press Next.
  • Keep the TComponent selected by default and press Finish.
  • Save your project by pressing the Save All button in the menu bar and save all the items in the same folder.

Note: For ease of use, name the first unit FormServerUnit and the project PODOServer.

6. Select:

  • For Delphi: File > New > Unit - Delphi
  • For C++: File > New > Unit - C++Builder

7. Save it as SharedStuffUnit.

8. In the SharedStuffUnit, declare a class TPerson:

Delphi:

  TPerson = class (TObject) 
  public
    property Lastname: string;
    property Firstname: string;

    constructor Create(ALast, AFirst: string);
    function ToString: string; override;
  end;

C++Builder:

  class TPerson: public TObject {
  public:
    UnicodeString Lastname;
    UnicodeString Firstname;

    TPerson(String ALast, String AFirst);
    __fastcall UnicodeString ToString(void);
  };

9. Delphi: Generate the getters and setters using the Ctrl + Shift + C key combination.

  TPerson = class (TObject)
  private
    FLastName: string;
    FFirstname: string;
    procedure SetFirstname(const Value: string);
    procedure SetLastName(const Value: string);
  public
    property LastName: string read FLastName write SetLastName;
    property Firstname: string read FFirstname write SetFirstname;

    constructor Create(ALast, AFirst: string);
    function ToString: string; override;
  end;

10. Implement the constructor for the TPerson class from the SharedStuffUnit:

Delphi:

  constructor TPerson.Create(ALast, AFirst: string);
  begin
    FLastName := ALast;
    FFirstName := AFirst;
  end;

C++Builder:

  TPerson::TPerson(String ALast, String AFirst)
  {
    Lastname = ALast;
    Firstname = AFirst;
  }

11. In the SharedStuffUnit, override the ToString function:

Delphi:

  function TPerson.ToString 
  begin
    Result := Firstname + ' ' + Lastname;
  end;

C++Builder:

  UnicodeString __fastcall TPerson::ToString(void)
  {
    return Firstname + " " + Lastname;
  }
  • ToString is a function inherited from the TObject, so it has to be overridden.

12. In the ServerMethodsUnit1 unit, select File > Use Unit..., add the SharedStuffUnit unit and press OK.

13. In the public zone of the ServerMethodsUnit, declare the GetPerson function:

Delphi:

  function GetPerson(aFirst, aLast: string): TPerson;

C++Builder:

  TPerson __fastcall TPerson::GetPerson(String ALast, String AFirst);
  • GetPerson is used to create and return a new instance of a TPerson class, with the given FirstName and LastName.

14. Set the Result of the function:

Delphi:

  function TServerMethods1.GetPerson(aFirst, aLast: string): TPerson;
  begin
    Result := TPerson.Create(ALast, AFirst);
  end;

C++Builder:

  TPerson *TServerMethods1::GetPerson(String ALast, String AFirst)
  {
    TPerson *p;
    p = new TPerson(ALast, AFirst);
    return p;
  }

15. Run > Run or press F9 to run the server.

16. Minimize the server window and go to the next step to create the client application.

Next

See Also