<. NET distributed application development> Chapter 10: enterprise application architecture

Source: Internet
Author: User

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:

  1. Do not apply OO design methods to distributed systems.
  2. Do not make the object stateful

 

Why? There are two reasons:

  1. 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.
  2. 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.

  1. There will be a total of 8 Socket calls
  2. 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:

  1. Only one call reduces the number of network calls
  2. The server does not maintain the task status.
  3. 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

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.