找回密码
 立即注册
首页 业界区 业界 NH剖析:Configuration篇——SessionFactory的Propertie ...

NH剖析:Configuration篇——SessionFactory的Properties之fluent-configuration配置

凳舒 2025-5-29 19:51:45
本节内容
       
  • 系列引入       
  • 概览       
  • 流配置(fluent-configuration)
                     
    • SessionFactory基本设置(IFluentSessionFactoryConfiguration)               
    • 数据库集成配置(IDbIntegrationConfiguration)               
    • 缓存配置(ICacheConfiguration)               
    • 代理配置(IProxyConfiguration)               
    • 集合工厂配置(ICollectionFactoryConfiguration)               
    • 映射配置(IMappingsConfiguration)       
                   
  • 结语       
  • 参考资料
系列引入

NHibernate3.0剖析系列分别从Configuration篇、Mapping篇、Query篇、Session策略篇、应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种应用程序的集成,基于NHibernte3.0版本。如果你还不熟悉NHibernate,可以快速阅读NHibernate之旅系列文章导航系列入门,如果你已经在用NHibernate了,那么请跟上NHibernate3.0剖析系列吧。
           
  • NHibernate专题:http://kb.cnblogs.com/zt/nhibernate/       
  • NHibernate官方站点:http://nhforge.org/       
  • NHibernate参考文档:http://nhforge.org/doc/nh/en/       
  • 获取NHibernate地址:http://sourceforge.net/projects/nhibernate/
概览

全新的NHibernate3.0为我们带来了很多便利。我们一起来看看在Configuration篇中有哪些新玩意吧。
我们一直都提倡“约定胜于配置(Convention over Configuration)”。然而在NHibernate2时代我们大多数情况下使用hibernate.cfg.xml配置文件中使用字符串配置SessionFactory的一些信息。在NHibernate3.0中,NHibernate3.0新增了NHibernate.Cfg.Loquacious这个命名空间。为我们增加了强类型配置支持。我们可以通过流配置(fluent-configuration)和/或者lambda表达式配置(lambda-configuration)来配置SessionFactory的Properties属性,真正做到了“约定胜于配置”。我们先来看下流配置(fluent-configuration)的具体实现吧。
流配置(fluent-configuration)

用过NHibernate,可能你知道Fluent NHibernate,它是通过Fluent配置映射文件。fluent-configuration顾名思义,使用Fluent API配置SessionFactory的Properties属性。约定胜于配置,当然有了很多特性,例如强类型支持、编译期错误检查等等。
fluent-configuration就是使用IFluentSessionFactoryConfiguration接口实现。原先在hibernate.cfg.xml有很多配置属性,在NHibernate3.0中,NHibernate贡献者(Fabio Maulo)按功能把SessionFactory的Properties配置分为以下几类,如图所示:
1.png

我们从中看出,这个接口包括以下六种配置:
           
  • SessionFactory基本设置(IFluentSessionFactoryConfiguration)       
  • 数据库集成配置(IDbIntegrationConfiguration)       
  • 缓存配置(ICacheConfiguration)       
  • 代理配置(IProxyConfiguration)       
  • 集合工厂配置(ICollectionFactoryConfiguration)       
  • 映射配置(IMappingsConfiguration)
其中数据库集成配置(IDbIntegrationConfiguration)和代理配置(IProxyConfiguration)是必不可少的配置选项。
SessionFactory基本设置(IFluentSessionFactoryConfiguration)

在SessionFactory基本的Properties设置里,我们可以定义其SessionFactoryName、启用查询统计、定义实体模式、HQL查询解析工厂。
例如我们这样配置,定义这个SessionFactory名称为“NHibernate”并启用查询统计,使用Poco实体模式和基于Antlr解析hql语句:
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. var cfg = new Configuration();
  3. cfg.SessionFactory()
  4.     .Named("NHibernate")
  5.     .GenerateStatistics()
  6.     .Using(EntityMode.Poco)
  7.     .ParsingHqlThrough<NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory>();
复制代码
其实现原理就是相应的调用Configuration类的SetProperty方法赋值,例如.Named("NHibernate")就是把“session_factory_name”赋予“NHibernate”,其他的配置完全类似,下面一幅图片清晰的展示了具体实现:
2.png

数据库集成配置(IDbIntegrationConfiguration)

在数据库集成配置中,包含了以下几个配置选项:
           
  • 基础数据库配置       
  • 连接字符串配置(IConnectionConfiguration)       
  • 数据库批量操作配置(IBatcherConfiguration)       
  • 事务配置(ITransactionConfiguration)       
  • 命令配置(ICommandsConfiguration)       
  • 数据库架构配置(IDbSchemaIntegrationConfiguration)
我们只要调用相应的Fluent API(图右边)进行方便的配置: 
3.png

来进一步看看究竟:
1.基础数据库配置

我们看看基础数据库配置中五个方法的具体实现吧,下面五个提示框分别是上面Using、DisableKeywordsAutoImport、AutoQuoteKeywords、LogSqlInConsole、DisableLogFormatedSql方法的实现原理:
4.png

2.连接字符串配置(IConnectionConfiguration)

在NHibernate配置中,连接字符串配置属于必不可少的部分,下图右侧就是这个类提供的Fluent API。
5.png

内部具体实现:
6.png

NHibernate提供了三种方法来配置连接字符串:
1.字符串方式:
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Integrate
  4.         .Using<MsSql2005Dialect>()
  5.         .Connected
  6.             .Using(@"Server=.\sqlexpress;initial catalog=NHibernate;Integrated Security=SSPI");
复制代码
2.使用DataProvider(System.Data.SqlClient.SqlConnectionStringBuilder)指定的DbConnectionStringBuilder,不过你也可以使用自己的DataProvider实现。
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Integrate
  4.         .Using<MsSql2005Dialect>()
  5.         .Connected
  6.             .Using(new SqlConnectionStringBuilder
  7.                        {
  8.                            DataSource = @".\sqlexpress",
  9.                            InitialCatalog = "NHibernate",
  10.                            IntegratedSecurity = true
  11.                        });
复制代码
3.可以把连接字符串写到配置文件中,从配置文件中读取配置信息,这种方式在项目中应该推荐,这样连接字符串就不在程序中写死了。
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Integrate
  4.         .Using<MsSql2005Dialect>()
  5.         .Connected
  6.             .ByAppConfing("MyConnectionStringName");
复制代码
3.数据库批量操作配置(IBatcherConfiguration)

用于定义Ado.Net的批量操作设置,例如Through方法设置“adonet.factory_class”属性,Each方法设置“adonet.batch_size”属性。
7.png

4.事务配置(ITransactionConfiguration)

用于定义“transaction.factory_class”属性。
8.png

5.命令配置(ICommandsConfiguration)

我们通过下面右侧的方法分别配置了“prepare_sql”、“command_timeout”、“sql_exception_converter”、“use_sql_comments”、“max_fetch_depth”、“query.substitutions”属性。注意一下:.WithHqlToSqlSubstitutions("true 1, false 0, yes 'Y', no 'N'")和.WithDefaultHqlToSqlSubstitutions()两个方法只能定义一个,因为这两个方法都是配置“query.substitutions”属性的。
9.png

上面方法的具体实现:
10.png

6.数据库架构配置(IDbSchemaIntegrationConfiguration)

这个配置用于定义“hbm2ddl.auto”选项,SchemaAutoAction有Recreate(create-drop)、Create(create)、Update(update)、Validate(validate)四种动作,默认什么都不做。
           
  • none:不产生任何操作。       
  • create-drop:在程序启动时,自动生成数据库架构并导入到数据库中,在程序关闭时,删除数据库架构,常用在测试中。       
  • create:在程序启动时,自动生成数据库架构并导入到数据库中。       
  • update:在程序启动时,检查持久化类与映射文件的改变,更新数据库架构。       
  • validate:在程序启动时,检查持久化类与映射文件的改变。
我们可以选择一种右侧的Fluent API调用。
11.png

如果你看了上面的介绍还不知所措,我写下完整的数据库集成配置(IDbIntegrationConfiguration)实现吧:
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.    .Integrate
  4.         .Using<MsSql2005Dialect>()
  5.         .DisableKeywordsAutoImport()
  6.         .AutoQuoteKeywords()
  7.         .LogSqlInConsole()
  8.         .DisableLogFormatedSql()
  9.         .Connected
  10.             .Through<DriverConnectionProvider>()
  11.             .By<SqlClientDriver>()
  12.             .With(IsolationLevel.ReadCommitted)
  13.             .Releasing(ConnectionReleaseMode.AfterTransaction)
  14.             .Using("The connection string")
  15.         .BatchingQueries
  16.             .Through<SqlClientBatchingBatcherFactory>()
  17.             .Each(15)
  18.         .Transactions
  19.              .Through()
  20.          .CreateCommands
  21.             .Preparing()
  22.             .WithTimeout(10)
  23.             .ConvertingExceptionsThrough<SQLStateConverter>()
  24.             .AutoCommentingSql()
  25.             .WithMaximumDepthOfOuterJoinFetching(11)
  26.             .WithHqlToSqlSubstitutions("true 1, false 0, yes 'Y', no 'N'")
  27.         .Schema
  28.             .Validating();
复制代码
缓存配置(ICacheConfiguration)

缓存配置包含了基本配置和查询缓存配置,用于我们定义二级缓存提供程序的,关于NHibernate二级缓存更多内容请参考入门级文章NHibernate之旅系列文章导航。
12.png

查询缓存配置,配置二级缓存提供程序的。
13.png

完整写法:
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Caching
  4.         .Through<HashtableCacheProvider>()
  5.         .PrefixingRegionsWith("xyz")
  6.         .UsingMinimalPuts()
  7.         .Queries
  8.             .Through<StandardQueryCache>()
  9.         .WithDefaultExpiration(15)
复制代码
代理配置(IProxyConfiguration)

这个配置属于必不可少的部分,在NHibernate2.1版本改变了ByteCode延迟加载机制,提供动态代理框架适配器,分别为:Castle框架、LinFu框架、Spring.Net框架。我们必须选择三种(NHibernate.ByteCode.Castle.ProxyFactoryFactory、NHibernate.ByteCode.LinFu.ProxyFactoryFactory、NHibernate.ByteCode.Spring.ProxyFactoryFactory)中的一种。
14.png

下面的例子我选用了NHibernate.ByteCode.Castle.ProxyFactoryFactory作为代理:
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Proxy
  4.         .DisableValidation()
  5.         .Through<NHibernate.ByteCode.Castle.ProxyFactoryFactory>();
复制代码
集合工厂配置(ICollectionFactoryConfiguration)

这个配置用于设置“collectiontype.factory_class”属性。
15.png

完整实现
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .GeneratingCollections
  4.         .Through<NHibernate.Type.DefaultCollectionTypeFactory>()
复制代码
映射配置(IMappingsConfiguration)

这个用于设置“default_catalog”和“default_schema”属性。
16.png

完整实现
  1. //Code Snippets Copyright http://lyj.cnblogs.com/
  2. cfg.SessionFactory()
  3.     .Mapping
  4.         .UsingDefaultCatalog("MyCatalog")
  5.         .UsingDefaultSchema("MySche")
复制代码
结语

NHibernate提供了全新的配置写法实现了强类型支持,为此我们SessionFactory中的Properties属性配置摆脱了过去hibernate.cfg.xml时代,我们现在可以说,快删除程序中实在不爽的hibernate.cfg.xml文件吧,不,因为还有一些配置还没有实现强类型配置。下篇将继续。
参考资料

NHibernate Jira:Strongly typed configuration of SessionFactory properties
Fabio Maulo:NHibernate Fluent Configuration

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册