Guys, don’t forget that we’re talking about a prerelease beta. Anything can change till release. Also, let us not forget to thank Embarcadero for giving us the permission to talk about this.
First of all, what is very nice in the new DataSnap is that you have the feeling of a n-tier solution which is usable in the real sense of the word. The great thing is that because there is a DataSnap implementation on .NET and because DataSnap can use JSON, you can have a seamless data exchange between native Windows, .NET, and Java applications. Pretty neat I’d say.
But let’s see some from the DataSnap features. First, you have… a wizard which will do for you the plat work of creating and configuring a DataSnap server:
As you see above there are very ‘interesting’ features: Authentication, Http etc.
Also when we look at the result in the Form Designer and in the Object Inspector we’ll see at a glance that the new DataSnap supports REST. Also, an important thing is the ‘Filters’ property with which you can apply any transformations to the data stream. A ZLibCompression filter is provided out of the box, but if someone wants to add any other filter like eg. encryption is free to do so. See for yourself :
How it works?
The simplest way to find out is to run the demo shipped with the new Delphi. Yep, there are new demos in the package and you can learn enough from them in a very short amount of time. In my opinion this is a very good move to advertise the new features and to attract the new users.
But for the ones who want to know more from now, the principle is very simple: While in Indy (which DataSnap uses behind the scenes) you deal with streams of bytes, in Data Snap you deal with Delphi artifacts.
First, you create the code which will be executed on DataSnap server. In your Server project you’ll add as many methods you like, writing them in a classical Delphi way. Let’s say that we have the following:
procedure TParametersServerModule1.DirExists(aDir: string; out aExists: OleVariant); begin aExists:=TDirectory.Exists(aDir); end;
Compile and run the server outside of IDE (or Run without Debugging) in order to freely close the project while our DataSnap Server is running. Now, if we’ll open our DataSnap client project and add a
TSQLServerMethod instance correctly configured to point to our running DataSnap server through an
TSQLConnection component then in the Object Inspector we should see our new method:
By setting up this property, the ‘Params’ property is updated accordingly. But of course we can do this also in code:
procedure TForm13.Button4Click(Sender: TObject); begin SqlServerMethod1.CommandText := 'TParametersServerModule1.DirExists'; //the full qualified name of our server method SqlServerMethod1.Prepared := True; //in order to get the parameters from server SqlServerMethod1.Params.Value :='C:\'; SqlServerMethod1.ExecuteMethod; //execute the method on the server if SqlServerMethod1.Params.Value then //read and test the result ShowMessage('Directory exists') else ShowMessage('There isn''t such a directory!'); SqlServerMethod1.Close; //cleanup end;
…also you can use the fabulous JSON units (source code included) to serialize your class instances over the wire. The main class which is responsible for this (
TJSONMarshall) has an open plug-in architecture in order to allow you to write readers and writers (also called converters) for your own custom data types. A small example to see how the things work:
procedure TForm13.Button4Click(Sender: TObject); var LContact: TContact; oMarshaller: TJSONMarshall; crtVal: TJSONValue; begin LContact:=TContact.Create; //our custom class LContact.Name:='wings-of-wind.com'; LContact.Age:=20; //fill with some data oMarshaller:=TJSONMarshal.Create(TJSONConverter.Create); //our engine try crtVal:=oMarshaller.Marshal(LContact); //serialize to JSON Memo1.Text:=crtVal.ToString; //display finally //cleanup FreeAndNil(LContact); FreeAndNil(oMarshaller); end; end;
Of course that are much more to say about DataSnap, but I think that for a quick overview is enough :-). Also I think that perhaps we should mention that DataSnap implements callbacks for server to notify the client and asynchronous method execution. Also I do think that in the current iteration, DataSnap is becoming a solid framework for distributed computing. You?