Enterprise Library 2.0 hands on lab translation (11): cache application block (3)

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.