代码改变世界

NHibernate3剖析:Configuration篇之SessionFactory fluent配置

2010-01-18 09:46  李永京  阅读(12276)  评论(33编辑  收藏  举报

本节内容

系列引入

NHibernate3.0剖析系列分别从Configuration篇、Mapping篇、Query篇、Session策略篇、应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种应用程序的集成,基于NHibernte3.0版本。如果你还不熟悉NHibernate,可以快速阅读NHibernate之旅系列文章导航系列入门,如果你已经在用NHibernate了,那么请跟上NHibernate3.0剖析系列吧。

概览

全新的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配置分为以下几类,如图所示:

IFluentSessionFactoryConfiguration

我们从中看出,这个接口包括以下六种配置:

  • SessionFactory基本设置(IFluentSessionFactoryConfiguration)
  • 数据库集成配置(IDbIntegrationConfiguration)
  • 缓存配置(ICacheConfiguration)
  • 代理配置(IProxyConfiguration)
  • 集合工厂配置(ICollectionFactoryConfiguration)
  • 映射配置(IMappingsConfiguration)

其中数据库集成配置(IDbIntegrationConfiguration)和代理配置(IProxyConfiguration)是必不可少的配置选项。

SessionFactory基本设置(IFluentSessionFactoryConfiguration)

在SessionFactory基本的Properties设置里,我们可以定义其SessionFactoryName、启用查询统计、定义实体模式、HQL查询解析工厂。

例如我们这样配置,定义这个SessionFactory名称为“NHibernate”并启用查询统计,使用Poco实体模式和基于Antlr解析hql语句:

//Code Snippets Copyright http://lyj.cnblogs.com/
var cfg = new Configuration();
cfg.SessionFactory()
    .Named("NHibernate")
    .GenerateStatistics()
    .Using(EntityMode.Poco)
    .ParsingHqlThrough<NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory>();

其实现原理就是相应的调用Configuration类的SetProperty方法赋值,例如.Named("NHibernate")就是把“session_factory_name”赋予“NHibernate”,其他的配置完全类似,下面一幅图片清晰的展示了具体实现:

IFluentSessionFactoryConfiguration

数据库集成配置(IDbIntegrationConfiguration)

在数据库集成配置中,包含了以下几个配置选项:

  1. 基础数据库配置
  2. 连接字符串配置(IConnectionConfiguration)
  3. 数据库批量操作配置(IBatcherConfiguration)
  4. 事务配置(ITransactionConfiguration)
  5. 命令配置(ICommandsConfiguration)
  6. 数据库架构配置(IDbSchemaIntegrationConfiguration)

我们只要调用相应的Fluent API(图右边)进行方便的配置: 

IDbIntegrationConfiguration

来进一步看看究竟:

1.基础数据库配置

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

IDbIntegrationConfiguration

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

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

IConnectionConfiguration

内部具体实现:

IConnectionConfiguration

NHibernate提供了三种方法来配置连接字符串:

1.字符串方式:

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Integrate
        .Using<MsSql2005Dialect>()
        .Connected
            .Using(@"Server=.\sqlexpress;initial catalog=NHibernate;Integrated Security=SSPI");

2.使用DataProvider(System.Data.SqlClient.SqlConnectionStringBuilder)指定的DbConnectionStringBuilder,不过你也可以使用自己的DataProvider实现。

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Integrate
        .Using<MsSql2005Dialect>()
        .Connected
            .Using(new SqlConnectionStringBuilder
                       {
                           DataSource = @".\sqlexpress", 
                           InitialCatalog = "NHibernate", 
                           IntegratedSecurity = true
                       });

3.可以把连接字符串写到配置文件中,从配置文件中读取配置信息,这种方式在项目中应该推荐,这样连接字符串就不在程序中写死了。

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Integrate
        .Using<MsSql2005Dialect>()
        .Connected
            .ByAppConfing("MyConnectionStringName");

3.数据库批量操作配置(IBatcherConfiguration)

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

IBatcherConfiguration

4.事务配置(ITransactionConfiguration)

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

ITransactionConfiguration

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”属性的。

ICommandsConfiguration

上面方法的具体实现:

ICommandsConfiguration

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

这个配置用于定义“hbm2ddl.auto”选项,SchemaAutoAction有Recreate(create-drop)、Create(create)、Update(update)、Validate(validate)四种动作,默认什么都不做。

  • none:不产生任何操作。
  • create-drop:在程序启动时,自动生成数据库架构并导入到数据库中,在程序关闭时,删除数据库架构,常用在测试中。
  • create:在程序启动时,自动生成数据库架构并导入到数据库中。
  • update:在程序启动时,检查持久化类与映射文件的改变,更新数据库架构。
  • validate:在程序启动时,检查持久化类与映射文件的改变。

我们可以选择一种右侧的Fluent API调用。

IDbSchemaIntegrationConfiguration

如果你看了上面的介绍还不知所措,我写下完整的数据库集成配置(IDbIntegrationConfiguration)实现吧:

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
   .Integrate
        .Using<MsSql2005Dialect>()
        .DisableKeywordsAutoImport()
        .AutoQuoteKeywords()
        .LogSqlInConsole()
        .DisableLogFormatedSql()
        .Connected
            .Through<DriverConnectionProvider>()
            .By<SqlClientDriver>()
            .With(IsolationLevel.ReadCommitted)
            .Releasing(ConnectionReleaseMode.AfterTransaction)
            .Using("The connection string")
        .BatchingQueries
            .Through<SqlClientBatchingBatcherFactory>()
            .Each(15)
        .Transactions
             .Through<AdoNetTransactionFactory>()
         .CreateCommands
            .Preparing()
            .WithTimeout(10)
            .ConvertingExceptionsThrough<SQLStateConverter>()
            .AutoCommentingSql()
            .WithMaximumDepthOfOuterJoinFetching(11)
            .WithHqlToSqlSubstitutions("true 1, false 0, yes 'Y', no 'N'")
        .Schema
            .Validating();

缓存配置(ICacheConfiguration)

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

ICacheConfiguration

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

IQueryCacheConfiguration

完整写法:

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Caching
        .Through<HashtableCacheProvider>()
        .PrefixingRegionsWith("xyz")
        .UsingMinimalPuts()
        .Queries
            .Through<StandardQueryCache>()
        .WithDefaultExpiration(15)

代理配置(IProxyConfiguration)

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

IProxyConfiguration

下面的例子我选用了NHibernate.ByteCode.Castle.ProxyFactoryFactory作为代理:

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Proxy
        .DisableValidation()
        .Through<NHibernate.ByteCode.Castle.ProxyFactoryFactory>();

集合工厂配置(ICollectionFactoryConfiguration)

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

ICollectionFactoryConfiguration

完整实现

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .GeneratingCollections
        .Through<NHibernate.Type.DefaultCollectionTypeFactory>()

映射配置(IMappingsConfiguration)

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

IMappingsConfiguration

完整实现

//Code Snippets Copyright http://lyj.cnblogs.com/
cfg.SessionFactory()
    .Mapping
        .UsingDefaultCatalog("MyCatalog")
        .UsingDefaultSchema("MySche")

结语

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

参考资料

NHibernate Jira:Strongly typed configuration of SessionFactory properties

Fabio Maulo:NHibernate Fluent Configuration