.NET Remoting and Delphi - BankPackage.dll: Contract Between Clients and Servers
(Page 9 of 11 )
If you're familiar with COM, you'll be accustomed to creating type libraries. The type library is a standardized binary resource linked in your server's executable or DLL that Windows requires to enable for interprocess communication. The type library represents the contract between your server and clients: It tells clients what they can do and what complex data types they will be using, but it contains no implementation.
When you develop .NET Remoting servers, type libraries are not necessary anymore: .NET's metadata included inside the compiled package will serve this purpose. The BankPackage.dll we just created is a .NET assembly that will contain the shared interfaces and classes used by the server and the client application.
We've added a new unit to BankPackage.dll and saved it as BankShared.pas. This unit is shown in Listing 29.1.
Listing 29.1 The BankShared.pas Unit
1: unit BankShared;
2:
3: interface
4:
5: type
6: { TAccount }
7: [Serializable]
8: TAccount = class
9: private
10: fNumber : integer;
11: fBalance: double;
12: fName: string;
13:
14: public
15: constructor Create(const aNumber : integer;
16: const aName : string;
17: const aBalance : double);
18:
19: property Number : integer read fNumber;
20: property Name : string read fName;
21: property Balance : double read fBalance write fBalance;
22: end;
23:
24: { IBankManager }
25: TAccountNumberArray = array of integer;
26:
27: IBankManager = interface
28: function GetAccountNumbers : TAccountNumberArray;
29: function GetAccount(const AccountNumber : integer) : TAccount;
30: procedure TransferMoney(const Origin, Destination : integer;
31: const Amount : double);
32: end;
33:
34: implementation
35:
36: { TAccount }
37:
38: constructor TAccount.Create(const aNumber : integer;
39: const aName : string;
40: const aBalance : double);
41: begin
42: inherited Create;
43:
44: fNumber := aNumber;
45: fName := aName;
46: fBalance := aBalance;
47: end;
48:
49: end.
Find the code on the CD: \Code\Chapter 29\Ex01.
In Listing 29.1, the IBankManager interface is a critical aspect of this unit (lines 24–32). As you can see, there isn't any class implementing it. This package is going to be shared between server and client, but the client doesn't need to know about (or contain) any implementation details. All it needs to know is what and how to call it.
The only class defined and implemented in this unit is TAccount (lines 6–22 and 38–47). In addition to this, the array TAccountNumberArray is also declared (line 25).
Because these two types are referenced by the methods of IBankManager, they need to be included in the shared assembly.
The interface IBankManager is implemented in the server project.
The other important thing to notice is the use of the attribute [Serializable] above the class TAccount (line 7). If TAccount was not marked as serializable, IBankManager.GetAccount would fail at runtime when called remotely.
TAccount is used as a Marshal-By-Value object. When the remote client calls GetAccount, a copy of the object is passed across application domain boundaries.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Implementing the Server >>
More .NET Articles
More By Xavier Pacheco