Creating the Server application (TObjects and DataSnap Tutorial)

From Appmethod Topics
Jump to: navigation, search

Go Up to Tutorial: Passing Plain Old Object Pascal Objects with DataSnap

Creating the Server application

To build the server, follow the next steps:

1. Open Appmethod.

2. Select File > New > Other.

3. From the DataSnap Server node in the left column, select DataSnap Server and press OK.

4. Specify the type of application:

  1. Select VCL Forms Application and press Next.
  2. Keep the default values to use TCP/IP Protocol.
  3. Clear the generation of Sample Methods and press Next.
  4. Press Test Port to verify whether the Port 211 is open and press Next.
  5. Keep the TComponent selected by default and press Finish.
  6. Save your project pressing the Save All button in the menu bar and save all the items in the same folder.
    For ease of use, name the first unit FormServerUnit.pas and the project--PODOServer.

5. Select File > New > Other and from the Object Pascal Files node in the left column, select Unit, press OK, and save it as SharedStuffUnit.pas.

6. In the SharedStuffUnit, declare a class TPerson:

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

    constructor Create(ALast, AFirst: string);
    function ToString: string; override;
  end;
  class TPerson : public TObject {
  public:
    UnicodeString Lastname;
    UnicodeString Firstname;

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

7. Generate the getters and setters using the Ctrl + Shift + C key combination.

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

  constructor TPerson.Create(ALast, AFirst: string);
  begin
    FLastName := ALast;
    FFirstName := AFirst;
  end;
  __fastcall TPerson::TPerson(String ALast, String AFirst)
  {
    Lastname = ALast;
    Firstname = AFirst;
  }
Opo1.png


9. In the SharedStuffUnit, override the ToString function:

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


10. Switch to ServerMethodsUnit and add SharedStuffUnit.pas in the interface uses clause. Press OK.

Opo2.png


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

  function GetPerson(aFirst, aLast: string): TPerson;
  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.


Opo3.png


12. Press Ctrl + Shift + C and set the Result of the function:

  Result := TPerson.Create(ALast, AFirst);
  TPerson *p;

  p = new TPerson(ALast, AFirst);

  return p;

13. Run the server and minimize its window.

Next

See Also