日期:2012-08-05  浏览次数:20414 次

10 Tips for Great .NET Programming
Whether you re interested in Windows Forms, ASP.NET, Web Services, or the .NET Framework, these tips help you exploit the still young .NET technology.
by Dino Esposito
The .NET Framework is larger than ever and filled with a huge number of classes and methods, but the developer community has yet to explore and understand most of this incredible volume of software features. What might appear to be a bug or a design flaw at first might be considered a significant strength after a second, more thoughtful look.
In light of this inevitable refinement process, sharing tips with other developers, although far from providing the definitive solution you might be looking for, is a way to steer you in the right direction when you build your the first .NET application. I ve compiled a list of 10 tips to make your .NET development more effective and productive. To help as many developers as possible, the tips span the technology s whole spectrum—from ADO.NET to ASP.NET, from the CLR to the Framework, and from Windows Forms to Web Services. Here they are, counting up to the ones I anticipate will have the most impact.
Tip 1: Shape Up Your DataGrid s Footer
The DataGrid control might feature a footer—that is, a row that summarizes part of the content shown in the page. The footer is hidden unless you enable it by setting the ShowFooter property to True. Once you do, the footer appears, but it has exactly the same number of columns as other rows. If this is fine with you, it isn t a problem. Otherwise, you ll need to add more fields to the footer or remove existing ones.
OnItemCreated is the key event to hook up. It s the event handler s signature:
void ItemCreated(Object s,DataGridItemEventArgs e)

This code lets you control the creation process for the footer and any other DataGrid item:
ListItemType itemType = e.Item.ItemType;if (itemType == ListItemType.Footer){  e.Item.Cells.RemoveAt(1);  e.Item.Cells.RemoveAt(1);  cellSummary.ColumnSpan = 3;  e.Item.Cells[0].Text = "...";}

Make sure you intervene when the footer is being created, then grab the control instance representing the footer row. Then you can remove or add as many cells as you need. Don t forget to set the ColumnSpan property to the original number of columns to avoid rendering problems such as having the footer line look shorter or longer than the rest of the grid.


Tip 2: Use the Cache Object
In Active Server Pages (ASP), you were used to employing the Application object to detect global events and store application-wide data. Given many users from different pages can access the same data cell, you must use Lock and Unlock methods to serialize access and avoid conflicts and unpredictable results. In ASP.NET, together with the Application object, you have the Cache object, which has similar characteristics but ends up replacing the Application object in more than one instance.
Both the Cache and Application objects are collection data containers that make content visible across page and session boundaries. Both don t fully support Web farm and Web garden scenarios. A Web farm is a Web server architecture in which multiple servers host an application. In a Web garden scenario, multiple processes on the same server host an application.
Unlike the Application object, the Cache object isn t instantiated as soon as the first page of the application is loaded. The Cache object gets into the game only when you use it. In addition, the Cache object has features to minimize the amount of memory its data items occupy. You can set a relative or absolute expiration date for cached items as well as associate them with a priority and a decay factor. Then you have greater control over the status of your global data and can implement a special policy to degrade gracefully under low memory condition