NHibernate3 Anatomy: Configuration of the cache lambda-configuration

Source: Internet
Author: User

Overview

In the previous article we introduced the lambda expression configuration (lambda-configuration), where the Entitycache extension method is used to configure the level two cache for domain.

Entity Cache Configuration

Once, Fabio Maulo did a survey and found that few people configured level two caches in Hibernate.cfg.xml or app. config files, and found that it is common practice to use <cache/in each domain's mapping (Mapping) file > Configuration. I think one of the reasons is that we are not aware of this feature. Before we introduce the Entitycache extension method, however, let's review the cache configuration of the NH2 weakly typed (Weak type).

Entity Cache configuration (Weak Type)

Configure the entity cache and the collection cache in the Hibernate.cfg.xml file in the Session-factory-configuration node by setting the Class-cache and Collection-cache nodes:

//code Snippets Copyright http://lyj.cnblogs.com/<Class-cacheclass="namespace.entity"usage="read-only|read-write|nonstrict-read-write|transactional" Region="aregion"/><Collection-cacheCollection="NameSpace.Entity.CollectionProperty"usage="read-only|read-write|nonstrict-read-write|transactional" Region="aregion"/>

This allows nhibernate to implement the cache configuration by scanning the Hibernate.cfg.xml file and then invoking the methods in the configuration class, so we can only manually write "string information" to use strongly typed.

//code Snippets Copyright http://lyj.cnblogs.com/ public  configuration  Setcacheconcurrencystrategy (string  Clazz, string  concurrencystrategy) public void  setcacheconcurrencystrategy (string  clazz, string  concurrencystrategy, string  region) Span style= "Color:blue" >public  configuration  Setcollectioncacheconcurrencystrategy (string  collectionrole, string  concurrencystrategy) 

In each domain's mapping file, we configure level two caching on the class or collection (set, Bag, list, map) node:

//Code Snippets Copyright http://lyj.cnblogs.com/<usage="read-only|read-write|nonstrict-read-writeregion="ARegion"/>
Entity cache Configuration (Entitycache) Entitycache principle

The so-called Entitycache configuration is implemented through lambda expressions, that is, in the Configurationextensions class Entitycache extension methods, we look at the implementation of this extension method:

//code Snippets Copyright http://lyj.cnblogs.com/Public StaticConfigurationEntitycache<tentity> ( ThisConfigurationConfigurationAction<ientitycacheconfigurationproperties<TEntity>> entitycacheconfiguration)whereTEntity:class{varECC =Newentitycacheconfigurationproperties<TEntity> (); Entitycacheconfiguration (ECC);if(ECC. Strategy.hasvalue) {configuration. Setcacheconcurrencystrategy (typeof(TEntity). FullName,Entitycacheusageparser. ToString (ECC. Strategy.value), ECC.    Regionname); }foreach(varCollectioninchEcc. Collections) {configuration. Setcollectioncacheconcurrencystrategy (collection. Key,Entitycacheusageparser. ToString (collection. Value.strategy), collection.    Value.regionname); }returnConfiguration;}

As we see it, we call the Setcacheconcurrencystrategy and Setcollectioncacheconcurrencystrategy methods in the configuration class to implement.

Entitycache use

We configure a level two cache for domain, first defining a domain entity, which also contains a collection:

//Code Snippets Copyright http://lyj.cnblogs.com/ public class   Entitytocache  {public string  Name {get ; Span style= "Color:blue" >set ; } public  ilist  < string  > Elements {get ; set ; }}

Let's practice using the Entitycache extension method:

1. Configure only the domain entity level two cache:
//Code Snippets Copyright http://lyj.cnblogs.com/configure.EntityCache<EntityToCache>(ce =>      {          EntityCacheUsage.NonStrictReadWrite;          "MyRegion";      });
2. Configure the domain entity and its collection level two cache:
//Code Snippets Copyright http://lyj.cnblogs.com/configure.EntityCache<EntityToCache>(ce =>         {             ce.Strategy = EntityCacheUsage.NonStrictReadWrite;             ce.RegionName = "MyRegion";             ce.Collection(e => e.Elements, cc =>                       {                           cc.RegionName = "MyCollectionRegion";                           cc.Strategy = EntityCacheUsage< /c16> .NonStrictReadWrite;                       });         }); 
3. Configure only the collection (not cache domain) level two cache:
//Code Snippets Copyright http://lyj.cnblogs.com/configure.EntityCache<EntityToCache>(ce => ce.Collection(e => e.Elements, cc =>        {            "MyCollectionRegion";            EntityCacheUsage.NonStrictReadWrite;        }));
Conclusion

In NHibernate3.0, we can implement a strongly typed configuration of the entity level two cache through the Entitycache extension method. With this extension method, you can configure all domain level two caches in one configuration class for easy management, modification, and uninstallation at any time. But think about it, if the project architecture is poorly designed, this will cause the configuration class to reference all domain projects in the assembly, and some domain projects sometimes need to refer to the assembly where the configuration class is located, which can easily lead to bidirectional references. To avoid this problem, it is time to think about how to design our project architecture, and the perfect solution for using nhibernate in large projects will be introduced slowly.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

NHibernate3 Anatomy: Configuration of the cache lambda-configuration

Related Article

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.