BCB Development Midas (DATASNAP) multi-tier applications

Source: Internet
Author: User
Tags server hosting
BCB Development Midas (DATASNAP) multi-tier applications

Author: vrknights Date: February 11, 2012

Many times we need to compile a program that allows multiple clients to access and manipulate the data in the database on the server. If the way you want to use it is not a Web site, there are usually two ways we can do it, 1. The client connects directly to the remote database, logs in and operates the data. 2. The client cannot directly connect to the remote database, but instead connects to the server-side program on the remote servers and sends the required operations to the service side in order to implement the operational data.
First, the business logic is all in the client program, one is not safe enough, and the other is not conducive to the change of business logic. To give a very simple example, the V1.0 program has the registration function, may save the user name, the password, the personal data and so on information. But if you need to add a "registration time" field to the program, you need to upgrade all the client programs to modify the key SQL statements that operate the database.
The second is that the business logic can be located in an intermediary server program, where the client is solely responsible for submitting applications and data, making the business more secure and also conducive to logical changes. Still, in the example, the client packs the data that the user fills out and invokes the server-side remote method AddUser (Mypackget *). And then you just need to modify the SQL within the "AddUser (mypackget*)" function in the server program.
The second implementation approach is a typical three-tier application architecture. This system not only realizes the "thin" client, but also can be very convenient to implement the distributed, you can divide the server into multiple login servers and application servers, according to the current server hosting state load balance.
Midas is the key technology of multilayer application architecture. In the Borland C + + Builder, the Datasnap component group is provided, which can realize the multi-layer application system very simply. Here is a simple implementation of the multi-tier system client and server function calls and message delivery.
Service side aspect:
1. In your working folder, create a good server directory and client directory.
2. Open BCB, select New Application (application), click Save, Save the form file as MyServerFrm.cpp, and save the working file as MyServerPrj.cpp. Of course, you can also take a different name.
3. Drag a text component Memo1 on the form Form1, which will be used to display the messages that the client passes over.
4. On the menu, select New (new)-> other-> multi-tier (muilter), select the Remote Data module, and fill in "MYRDM" in class name "CoClass name" ( You can also name it, but in the back if useful to MYRDM, please use your own name instead. ), thread mode (Threading model) remains apartment unchanged. Click OK (OK). To save a data module as a MyRDMImpl.cpp
5. Open View-> type library (Classes libary, the English interface does not show this. My interface is Chinese version), in which the IMYRDM right-click, select New->method a new method.
6. Name the method "SayHello". In parameters, add two parameters. One is the message content, name is Strsay, and the type is BSTR, in the following way: in
The other is the return value, name is NOK, the type is: int*, by the way: Out,retval
As shown in figure:

7. Click the Refresh button at the top of the type library and enter the following code in the newly displayed code box: View Source code printing Help

1 STDMETHODIMP Tmyrdmimpl::sayhello (BSTR Strsay, int* nOk)
2 {
3 Ansistring str=ansistring (Strsay);
4 Ansistring stroutput=ansistring ("Client input is:") +str;
5 Form1->memo1->lines->add (Stroutput);
6 if (str. Ansicompare ("OK") ==0) {
7
8 *nok=1;
9 }else{
10 *nok=0;
11 }
12 }

and add a header file to the file where the SayHello function resides, #include "MyServerFrm.h" (otherwise Form1 undefined)
8. Execute the program to register it in the system. So that clients can connect remotely.
9. In your BCB installation folder under the Bin directory, find the Scksrvr.exe program, run it.

Client:
1. Create a new application, save the main form as: MyClientFrm.cpp, save the project file as: MyClientPrj.cpp
2. Click Project (Project)-> add to the project (add to Project), add the myrdm_tlb.cpp in the server directory to the project.
3. File-> new (new)-> data module, create a data module, save the file as MyDataModule1.cpp.
4. In the Data module form, drag into the SocketConnection component in the Datasnap component group. and will SocketConnection1 address as 127.0.0.1 (that is, this machine), Socketconnection1->servername find Xxx.myrdm,guid will automatically be obtained according to ServerName. The port remains unchanged at 211 (unless you have changed the port in Scksrvr.exe). Also, note that socketconnection1->active is changed to true. At this point, if it is normal, the server program will run automatically if it is not turned on.
5. Add a Edit1 and a button Button1 to the main form Form1, double-click Button1, and enter the following code in the Button1Click event: View source code printing Help

1 void __fastcall Tform1::button1click (tobject *sender)
2 {
3 int nok=-1;
4 Widestring wssay=form1->edit1->text;
5 Idispatch* disp = (IDispatch *) datamodule1->socketconnection1->appserver;
6 Imyrdmdisp THERDM ((imyrdm*) disp);
7 Nok=therdm.sayhello (Wssay);
8 if (nok==-1) {
9 ShowMessage ("Error in execution");
10 }else if (nok==0) {
11 ShowMessage ("Input in Edit1 is not OK");
12 }else if (nok==1) {
13 ShowMessage ("Input is OK in Edit1");
14 }else{
15 ShowMessage ("Unknown error");
16 }
17 }

and add header files to the file where Button1Click is located: #include "myrdm_tlb.cpp" and #include "MyDataModule.cpp"
6. Operation of the. and try to enter different values in the Edit1 to see the results.
As shown in the figure:

Thus, a simple Midas structure is completed. To improve it, you only need to add adoconnection, Adoquery, Datasetprovider, adotable, and other database components to the server-side->MYRDM (Remote Data module) to connect and manipulate the database. You can implement a three-tier architecture.
By the end, I forgot to say a little. Remote Data module in apartment mode The working state is this: when the server program starts running, the first RDM is created to accept the client-side connection request. When a formal connection is made between the server end and the client side, the server side creates a new remote Data Module to accept the new client connection request. Therefore, each client will have its own independent, private, a remote Data Module.
So, here's the problem. When you need to invoke other members of the RDM (such as adoquery), you need to use M_datamodule to specify a remote data module to manipulate your own private. That should be used: M_datamodule->adoquery1->sql->add ("SELECT Count" (1) from the users where Username=:username and password=: Password "); Rather than Tmyrdmimpl::adoquery1->sql->add (...)
Earnestly, saying that I also pay a lot of time cost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.