且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)

更新时间:2022-04-02 06:35:25

练习3:实现后台缓存

该练习将示范如何实现后台加载。

 

第一步

打开EmployeeBrowser.sln 项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Caching\exercises\ex03\begin,并编译。

 

第二步 实现后台加载

1.在解决方案管理器中选择EmployeeServices.cs文件,选择View | Code菜单命令,添加如下两个方法,它们将实现在后台加载缓存。
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)// TODO: PopulateCache & BeginBackgroundLoad
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
private static void PopulateCache()
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
byte[] photoData = null;
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    EmployeesDataSet dsEmployees 
= GetContactDetails();
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
if (dsEmployees == null)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        
return;
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    CacheManager cache 
= CacheFactory.GetCacheManager();
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
foreach (EmployeesDataSet.EmployeesRow employee in dsEmployees.Employees)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        
if (!cache.Contains(employee.EmployeeID.ToString()))
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)            EmployeeDataProvider dataProvider 
= new EmployeeDataProvider();
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)            photoData 
= dataProvider.GetEmployeePhotoData(employee.EmployeeID);
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)            cache.Add(employee.EmployeeID.ToString(), photoData);
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        }

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    }

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)}

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
private delegate void PopulateCacheDelegate();
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
public static void BeginBackgroundLoad()
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
if (!ConnectionManager.IsOnline)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        
return;
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    PopulateCacheDelegate mi 
= new PopulateCacheDelegate(PopulateCache);
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    mi.BeginInvoke(
nullnull);
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)}
BeginBackgroundLoad方法使用一个委托在后台线程开始PopulateCache方法,它将会被.NET工作线程处理。

2.选择MainForm.cs文件,选择View | Code菜单命令,在方法MainForm_Load中加入如下代码开始后台工作。

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)private void MainForm_Load(object sender, EventArgs e)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
this.ToolStripLabel1.Text = ConnectionManager.StatusText;
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
// Load data into the 'EmployeesDataSet'.
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    EmployeesDataSet tempDataset 
= EmployeeService.GetContactDetails(); 
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
if (tempDataset != null)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)        
this.EmployeesDataSet.Merge(tempDataset);
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
// TODO: Start loading cache in the background
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)

Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    EmployeeService.BeginBackgroundLoad();
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)}
 
第三步 运行应用程序

1.选择Debug | Start Without Debugging菜单命令运行应用程序。

不要浏览任何雇员数据,在等待大概10秒后退出应用程序。如果应用程序在线它将尝试后台加载雇员照片,缓存存储在物理存储位置上,即持久缓存,但是与前一个练习使用了不同的PartitionName

2.在解决方案管理器中选择ConnectionManager.cs,选择View | Code菜单命令,在下面的代码中修改IsOnline属性的值。
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)static public bool IsOnline
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)
{
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)    
get return false; }
Enterprise Library 2.0 Hands On Lab 翻译(11):缓存应用程序块(三)}
3.选择Debug | Start Without Debugging菜单命令运行应用程序。现在应用程序不再连接数据库处于离线状态,所有的雇员信息已经照片已经被缓存。

 

更多Enterprise Library的文章请参考《Enterprise Library系列文章












本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67642,如需转载请自行联系原作者