When communicating over the web (web services or .NET Remoting), you must remember that the most expensive part of the operation comes when you transfer objects between distant machines. The more granular the API is, the higher percentage of time your application spends waiting for data to return from the server. Therefore, be sure to create web-based interfaces based on serializing documents or sets of objects between client and server. This way, the server receives all of the information it needs to complete the requested task.
Here's an example of how
NOT to code a web-based interface:
1//Create CartItem object on the server
2CartItem objCart = new Server.CartItem();
3//Round trip to set the ProductID
4objCart.ProductID = 135;
5//Round trip to set the Quantity
6objCart.Quantity = 1;
7//Round trip to add the item
8objCart.AddItem();
Here's an example of a well designed web-based interface:
1//Create CartItem object on the client
2CartItem objCart = new CartItem();
3//Set local copy
4objCart.ProductID = 135;
5//Set local copy
6objCart.Quantity = 1;
7//One round trip to add the item
8Server.AddItem(objCart);
The above example is a simple example. To really make this more efficient, you need to apply this to real world scenarios and further examine what's being transmitted back and forth. For example, let's pretend you're writing a software system for an order intake company who has a few millions customers who each place 15-20 orders per year. You staff consist of 20 order operators plus a dozen or so people running reports or simply querying the database.
When a customer calls, you might want to retrieve their orders so you create the following method:
1public OrderData FindOrders(string strCustomerName)
2{
3 //Return all orders for a customer searched by name
4}That's OK, but why return all orders...how about just open orders. So we change it t
1public OrderData FindOpenOrders(string strCustomerName)
2{
3 //Return all open orders for a customer searched by name
4}Better, but we're still requiring 2 data transmissions per phone call (one get, one save). Let's assume we could partition the call center into regions/states. At the beginning of the operator's shift he/she could retrieve all customers (with open orders) and open orders for the given region. As calls come in, the operator would never need to retrieve any information from the server. Once the call is completed, the operator could push the updates to the server and at the same time, retrieve any updates made since the last update (only 2-way transmission).
But it can get even better! What if you only retrieved customers who have made purchases in the past 6 months. Anyone else probably won't be coming back and if so, we could always make a quick trip to get their data (well worth the saving on the initial data retrieval!).
These are just a few reasons why...to read more, pick up "Effective C#: 50 Specific Ways to Improve Your C#" by Bill Wagner.
You can buy it at the Addison-Wesley website:
http://www.aw-bc.com/
Comments
Leave a Comment