Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Professional ASP.NET 3.5 in C# and Visual Basic Part 112. Building on the revolutionary ASP.NET 2.0 release, ASP.NET 3.5 adds several key new developer features including AJAX, LINQ, and a new CSS designer in Visual Web Developer 2008. The dramatic reduction in code that developers realized from the more than 50 new server controls in ASP.NET 2.0 now allows developers the time to make their applications more interactive with AJAX, to work with data in their preferred language with LINQ, and to build visually attractive and consistent standards-based sites with CSS. . | Chapter 22 State Management expensive database retrieval those UserControls can retrieve the necessary data from HttpContext.Items. The database is hit only once. When individual units within a single HttpRequest need to act on the same or similar data If the lifetime of your data is just one request consider using HttpContext.Items as a short term cache. The Items collection holds objects just like many of the collections that have been used in this chapter. You need to cast those objects back to their specific type when they are retrieved. Within a Web-aware Database Access Layer per-request caching can be quickly implemented with the simple coding pattern shown here. Note that this sample code is a design pattern and there is no MyData class it s for illustration. VB Public Shared Function GetExpensiveData ID As Integer As MyData Dim key as string data ID.ToString Dim d as MyData _ CType HttpContext.Current.Items key MyData If d Is Nothing Then d New Data Go to the Database do whatever. HttpContext.Current.Items key d End If Return d End Function C public static MyData GetExpensiveData int ID string key data ID.ToString MyData d MyData HttpContext.Current.Items key if d null d new Data Go to the Database do whatever. HttpContext.Current.Items key d return d This code checks the Items collection of the current HttpContext to see if the data is already there. If not the data is retrieved from the appropriate backing store and then stored in the Items collection. Subsequent calls to this function within the same HttpRequest receive the already-cached object. As with all optimizations and caching premature optimization is the root of all evil. Measure your need for caching and measure your improvements. Don t cache just because it feels right cache because it makes sense. 1068 Chapter 22 State Management Summary This chapter explored the many ways to manage State within your ASP.NET application. The Session object and its providers offer many choices. Each has its .