Exercise3: Implement background Cache
This exercise demonstrates how to perform background loading.
Step 1
OpenEmployeebrowser. slnProject, the default installation path should beC: \ Program Files \ Microsoft Enterprise Library January 2006 \ labs \ CS \ caching \ exercises \ ex03 \ beginAnd compile.
Step 2 implement background Loading
1In solution manager, selectEmployeeservices. CSFile, selectView | codeMenu command. Add the following two methods to load the cache in the background.
// Todo: populatecache & beginbackgroundload
Private Static Void Populatecache ()
{
Byte [] Photodata = Null ;
Employeesdataset dsemployees = Getcontactdetails ();
If (Dsemployees = Null )
Return ;
Cachemanager Cache = Cachefactory. getcachemanager ();
Foreach (Employeesdataset. employeesrow employee In Dsemployees. Employees)
{
If ( ! Cache. Contains (employee. employeeid. tostring ()))
{
Employeedataprovider dataprovider= NewEmployeedataprovider ();
Photodata=Dataprovider. getemployeephotodata (employee. employeeid );
Cache. Add (employee. employeeid. tostring (), photodata );
}
}
}
Private Delegate Void Populatecachedelegate ();
Public Static Void Beginbackgroundload ()
{< br> If ( ! connectionmanager. isonline)
return ;
populatecachedelegate mi = New populatecachedelegate (populatecache );
mi. begininvoke ( null , null );
}
BeginbackgroundloadMethod to start with a delegate in the background threadPopulatecacheMethod, it will be. NetWorker thread processing.
2. SelectMainform. CSFile, selectView | codeMenu command, in the MethodMainform_loadAdd the followingCodeStart background work.
Private Void Mainform_load ( Object Sender, eventargs E)
{
This . Toolstriplabel1.text = Connectionmanager. statustext;
// Load data into the 'ployeesdataset '.
Employeesdataset tempdataset = Employeeservice. getcontactdetails ();
If (Tempdataset ! = Null )
This . Employeesdataset. Merge (tempdataset );
// Todo: Start loading cache in the background
Employeeservice. beginbackgroundload ();
}
Step 3 run the applicationProgram
1. SelectDebug | start without debuggingMenu command to run the application.
Do not browse any employee data.10And then exit the application. If the application is online, it will try to load Employee photos in the background, and the cache is stored in the physical storage location, that is, persistent cache, but it is different from the previous exercisePartitionname.
2In solution manager, selectConnectionmanager. CS, SelectView | codeMenu command, modify in the following codeIsonlinEAttribute Value.
Static Public Bool Isonline
{< br> Get { return false ;}
}
3. SelectDebug | start without debuggingMenu command to run the application. Now the application is no longer connected to the database offline, and all employee information has been cached.
MoreEnterprise LibraryOfArticleSee 《Enterprise LibrarySeries of articles"