Creating Web Methods - In code
From Morfikwiki.com
It is now very easy to create extensive Web Service based APIs with Morfik AppsBuilder through coding. Web Methods can now be declared in any module and you can declare as many as you want in any one module. This makes it easier for experienced developers to create extensive APIs much quicker than it would be possible using the special Web Method modules.
Declaring Web Methods in code involves creating a class that descends from the WebMethod class and using some special metadata tags to inform the compiler what are the parameters for this Web Method.
[edit] Metadata Tags for WebMethods
The following are the metadata tags that need to be applied to a WebMethod declaration for it to be properly published in the application's WSDML file.
['WSPublished=true']- The WebMethod descendant class and each of its member variables that will represent a parameter need to have this tag applied.
'WSFieldKind=In|Out|In/Out'- This should be added to the tag for each member variable that represents a parameter to specify the behavior for that parameter.
'WSHeader=true'- This should be added to the tag for each member variable that represents a parameter to specify if this parameter should be passed in the request header to the server.
[edit] Example Code
In the following listing you see an example of a code declaration for a Web Method. In this case a Method that returns a list of objects with the names and phones of some people. This module could be added to any AppsBuilder Web projects and provided the necessary table (Contacts) where present, the project would publish this the ListContacts Web Method.
Unit PhoneBookAPI;
Interface
Uses
SystemSerialiser,
SystemWebMethod;
type
MyContact = class(TSerializable)
Id: integer;
Name: string;
Phone: string;
Constructor Create(aId: integer; aName, aPhone: string);
end;
ListContacts = class(WebMethod)
MyContacts: List of MyContact; ['WSPublished=true','WSFieldKind=out','WSHeader=false'];
procedure Execute; override;
end;['WSPublished=true'];
Implementation
uses SystemDatabase;
Constructor MyContact.Create(aId: integer; aName, aPhone: string);
begin
inherited Create;
Id := aId;
Name := aName;
Phone := aPhone;
end;
procedure ListContacts.Execute;
var
ContactRS: TRecordSet;
aContact: MyContact;
Begin
ContactRS := DefaultDBConnection.CreateRecordSet('SELECT "id", "contactname", "phone" FROM "Contacts"');
try
ContactRS.Prepare;
ContactRS.Active := True;
ContactRS.First;
If Not ContactRS.Eof then
while not ContactRS.Eof do
Begin
aContact := MyContact.Create(ContactRS.FieldbyName('id').AsInteger, ContactRS.FieldbyName('contactname').AsString,
ContactRS.FieldbyName('phone').AsString);
MyContacts.Add(aContact);
ContactRS.Next;
End;
ContactRS.Close;
finally
ContactRS.Free;
end;
End;
End.
