The first part of this book describes the technologies required by distributed systems, such as Remoting, XML Web Service, MSMQ, COM +, and multithreading.
These are the most basic and best-mastered content.
The second part is the essence of this book. From this chapter, the author begins to talk about the factors to be considered in architecture and design, and some of his experiences.
Before that, we should correct two incorrect programming concepts:
- Do not apply OO design methods to distributed systems.
- Do not make the object stateful
Why? There are two reasons:
- Remote objects are different from local objects because they need to be accessed across processes and networks. performance is the biggest problem. although you cannot feel it, each method and attribute call always sends network data. the underlying layer is complex.
- Remote objects are hard to maintain. Even if they can, they occupy too much memory on the server. In the future, Load-Balancing expansion may cause problems.
Let's look at an example:
Public Class Account
'For simplicity's sake we use public variables instead
'Full property procedures.
Public AccountID As Integer
Public Balance As Decimal
Public Sub Update ()
'(Update this record in the database .)
End Sub
Public Sub Insert ()
'(Insert a new account record using the information in this
'Class .)
End Sub
Public Sub Delete ()
'(Delete the database record that matches this class .)
End Sub
Public Sub Fill ()
'(Get the database record that matches this class .)
End Sub
End Class
If you want to transfer funds between two accounts, you will write:
Dim AccA As New Account (), AccB As New Account ()
'Retrieve the balance for account 1001.
Accas. AccountID = 1001
AccA. Fill ()
'Retrieve the balance for account 1002.
AccB. AccountID = 1002
AccB. Fill ()
'Transfer the money.
Dim Transfer As Decimal = 150
AccA. Balance + = Transfer
AccB. Balance-= Transfer
'Save both records.
AccA. Update ()
AccB. Update ()
This code is very good and intuitive, but it is very inefficient if the Account is a remote object.
- There will be a total of 8 Socket calls
- The Account is stateful. If the number of your clients is small, you do not feel anything. But if the data grows exponentially, your program will have a performance bottleneck.
Update is atomic, but the above Code does not guarantee atomicity
Let's see what a stateless Account looks like.
Public Class AccountUtility
Public Sub UpdateAccount (accountID As Integer, balance As Decimal)
'(Update the account record in the database .)
End Sub
Public Sub InsertAccount (accountID As Integer, balance As Decimal)
'(Insert a new account record .)
End Sub
Public Sub Delete (accountID As Integer)
'(Delete the corresponding database record .)
End Sub
Public Sub TransferFunds (accountIDFrom As Integer ,_
AccountIDTo As Integer, transferAmount As Decimal)
'(Start a database transaction, and modify the two accounts .)
End Sub
End Class
If you want to transfer funds now, write the following statement:
Dim AccUtility As New AccountUtility ()
AccUtility. TransferFunds (1001,100 2, 150)
The new Account version is very similar to the old process-oriented programming method, but it is very distributed, because:
- Only one call reduces the number of network calls
- The server does not maintain the task status.
- The transfer logic is on the server, not the client.
Of course, it also has its shortcomings. If a method requires N parameters, all the calls should be passed in, which is very troublesome.
Public Sub InsertCustomer (firstName As String, lastName As String, _ userID As String, password As String, email As String, _ streetAddress As String, city As String, postalCode As String, _ country As String)
'(Code omitted)
End Sub
In this case, we can write an object class (that is, the Information Package mentioned above) to store these parameters.
Public Class AccountDetails
Public AccountID As Integer
Public Balance As Integer
End Class