摘要:ActiveRecord在底层封装了NHibernate,在框架启动时需要指定相关的配置信息,那么我们需要配置些什么?又该如何去配置呢?本文将会介绍在ActiveRecord中构建配置信息。
主要内容
1
.需要配置什么
2
.如何去配置
3
.常见的配置示例
一.需要配置什么
在第一篇大家都已经看到了,其实我们的配置信息跟用NHibernate时的配置是一样的,这是因为ActiveRecord在底层封装了NHibernate。为了没有用过NHibernate的朋友,这里再把配置信息简单介绍一下。
1
.配置NHibernate ADO.NET属性
属性名
|
说明
|
hibernate.connection.provider_class
|
定制
IConnectionProvider
的类型.
例如:
full.classname.of.ConnectionProvider (如果提供者创建在NHibernate中), 或者 full.classname.of.ConnectionProvider, assembly (如果使用一个自定义的IConnectionProvider接口的实现,它不属于NHibernate)。
|
hibernate.connection.driver_class
|
定制
IDriver
的类型.
full.classname.of.Driver (如果驱动类创建在NHibernate中), 或者 full.classname.of.Driver, assembly (如果使用一个自定义IDriver接口的实现,它不属于NHibernate)。
|
hibernate.connection.connection_string
|
用来获得连接的连接字符串。
|
hibernate.connection.isolation
|
设置事务隔离级别. 请检查
System.Data.IsolationLevel 来得到取值的具体意义并且查看数据库文档以确保级别是被支持的。
例如:
Chaos, ReadCommitted, ReadUncommitted, RepeatableRead, Serializable, Unspecified
|
2
.可选的配置属性
除了上面的ADO.NET属性之外,我们还有如下的可选属性
属性名
|
说明
|
hibernate.dialect
|
NHibernate
方言(Dialect)的类名 - 可以让NHibernate使用某些特定的数据库平台的特性
例如:
full.classname.of.Dialect (如果方言创建在NHibernate中), 或者full.classname.of.Dialect, assembly (如果使用一个自定义的方言的实现,它不属于NHibernate)。
|
hibernate.default_schema
|
在生成的SQL中,scheml/tablespace的全限定名.
例如:
SCHEMA_NAME
|
hibernate.prepare_sql
|
是否准备sql语句
例如:
true | false
|
hibernate.session_factory_name
|
SessionFactory
被创建后将自动绑定这个名称.
例如:
some.name
|
hibernate.use_outer_join
|
允许使用外连接抓取。
例如:
true | false
|
hibernate.cache.provider_class
|
指定一个自定义的
CacheProvider
缓存提供者的类名
例如:
full.classname.of.CacheProvider (如果ICacheProvider创建在NHibernate中), 或full.classname.of.CacheProvider, assembly (如果使用一个自定义的ICacheProvider,它不属于NHibernate)。
|
hibernate.query.substitutions
|
把NHibernate查询中的一些短语替换为SQL短语(比如说短语可能是函数或者字符)。
例如:
hqlLiteral=SQL_LITERAL, hqlFunction=SQLFUNC
|
可以数据库设置一个hibernate.dialect方言,它是NHibernate.Dialect.Dialect 的一个子类。如果不需要使用基于native或者sequence的主键自动生成算法,或者悲观锁定(使用ISession.Lock() 或者 IQuery.SetLockMode())的话,方言就可以不必指定。然而,假若你指定了一个方言,Hibernate会为上面列出的一些属性使用特殊默认值,省得我们手工指定。
NHibernate SQL
方言对照表:
数据库系统
|
SQL
方言
|
DB2
|
NHibernate.Dialect.DB2Dialect
|
PostgreSQL
|
NHibernate.Dialect.PostgreSQLDialect
|
MySQL
|
NHibernate.Dialect.MySQLDialect
|
Oracle (any version)
|
NHibernate.Dialect.OracleDialect
|
Oracle 9/
|
NHibernate.Dialect.Oracle9Dialect
|
Sybase
|
NHibernate.Dialect.SybaseDialect
|
Microsoft SQL Server 2000
|
NHibernate.Dialect.MsSql2000Dialect
|
Microsoft SQL Server 7
|
NHibernate.Dialect.MsSql7Dialect
|
Firebird
|
NHibernate.Dialect.FirebirdDialect
|
二.如何去配置
ActiveRecord
为我们提供了三种方式的配置
1
.XmlConfigurationSource配置
可以使用自己的XML文件来保存配置信息,例如有一个MyConfig.xml的文件
<?
xml version="1.0" encoding="utf-8"
?>
< activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=.;Initial Catalog=ARDemo;UID=sa;Password=sa" />
</ config >
</ activerecord >
< activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=.;Initial Catalog=ARDemo;UID=sa;Password=sa" />
</ config >
</ activerecord >
这时候我们在框架初始化的时候就应该这样写:
XmlConfigurationSource source
=
new
XmlConfigurationSource(
"
MyConfig.xml
"
);
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
其中
XmlConfigurationSource通过重载提供了如下三个公用的构造函数
public XmlConfigurationSource(String xmlFileName)
public XmlConfigurationSource(Stream stream)
public XmlConfigurationSource(TextReader reader)
不管是以文件名还是Stream形式或者TextReader,在XmlConfigurationSource的内部都会转换为XmlDocument。最后要注意xml文件的路径,可以用生成后事件命令拷贝.xml文件到bin目录下
copy "$(ProjectDir)\*.xml" "$(TargetDir)"
2
.InPlaceConfigurationSource配置
这种实现是一种硬编码的方式,在实际的使用中并不推荐,但是有时候如果我们的配置信息是动态的获取,则这种方式就会变得非常有用。
InPlaceConfigurationSource source
=
new
InPlaceConfigurationSource();
Hashtable properties = new Hashtable();
properties.Add( " hibernate.connection.driver_class " , " NHibernate.Driver.SqlClientDriver " );
properties.Add( " hibernate.dialect " , " NHibernate.Dialect.MsSql2000Dialect " );
properties.Add( " hibernate.connection.provider " , " NHibernate.Connection.DriverConnectionProvider " );
properties.Add( " hibernate.connection.connection_string " , " UID=sa;Password=19811218;Initial Catalog=ARDemo;Data Source=. " );
source.Add( typeof (ActiveRecordBase), properties );
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
Hashtable properties = new Hashtable();
properties.Add( " hibernate.connection.driver_class " , " NHibernate.Driver.SqlClientDriver " );
properties.Add( " hibernate.dialect " , " NHibernate.Dialect.MsSql2000Dialect " );
properties.Add( " hibernate.connection.provider " , " NHibernate.Connection.DriverConnectionProvider " );
properties.Add( " hibernate.connection.connection_string " , " UID=sa;Password=19811218;Initial Catalog=ARDemo;Data Source=. " );
source.Add( typeof (ActiveRecordBase), properties );
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
3 .使用应用程序配置文件
这种方式是最为常见的一种,即使用应用程序的配置文件(Web.config 或者App.config),在配置文件中
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="activerecord" type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
</ configSections >
< activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="UID=sa;Password=sa;Initial Catalog=ARDemo;Data Source=." />
</ config >
</ activerecord >
</ configuration >
< configuration >
< configSections >
< section name ="activerecord" type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
</ configSections >
< activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="UID=sa;Password=sa;Initial Catalog=ARDemo;Data Source=." />
</ config >
</ activerecord >
</ configuration >
这时候我们的框架初始化代码应该这样写
[.NET2.0]
4 .在Web应用程序中的配置
[.NET1.1]
IConfigurationSource source
=
System.Configuration.ConfigurationSettings.GetConfig(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
[.NET2.0]
IConfigurationSource source
=
System.Configuration. ConfigurationManager.GetSection (
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
ActiveRecordStarter.Initialize( source, typeof (ActiveRecordBase) );
4 .在Web应用程序中的配置
如果我们是在Web应用程序中使用ActiveRecord,需要指定isWeb="true",如下
<
activerecord
isWeb
="true"
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="UID=sa;Password=sa;Initial Catalog=ARDemo;Data Source=." />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="UID=sa;Password=sa;Initial Catalog=ARDemo;Data Source=." />
</ config >
</ activerecord >
一般的初始化工作我们会放在Application_
Start
中,示例代码
protected
void
Application_Start(Object sender, EventArgs e)
{
IConfigurationSource source =
System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof(ActiveRecordBase));
}
{
IConfigurationSource source =
System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof(ActiveRecordBase));
}
三.常见的配置示例
Castle网站为我们提供的几个常见的配置示例
1 .MS SQLServer
1 .MS SQLServer
<
activerecord
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=.;Initial Catalog=test;UID=sa;Password=sa" />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.SqlClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MsSql2000Dialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=.;Initial Catalog=test;UID=sa;Password=sa" />
</ config >
</ activerecord >
2
.Oracle
<
activerecord
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.OracleClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.OracleDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=dm;User ID=dm;Password=dm;" />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.OracleClientDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.OracleDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Data Source=dm;User ID=dm;Password=dm;" />
</ config >
</ activerecord >
3
.MySQL
<
activerecord
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.MySqlDataDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MySQLDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Database=test;Data Source=someip;User Id=blah;Password=blah" />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.MySqlDataDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.MySQLDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Database=test;Data Source=someip;User Id=blah;Password=blah" />
</ config >
</ activerecord >
4
.Firebird
关于 ActiveRecord构建配置信息的介绍就这么多了,内容比较简单。下篇文章中我会详细介绍 ActiveRecord中的映射,希望研究过 Castle的朋友不吝赐教
<
activerecord
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.FirebirdDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.FirebirdDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Server=localhost;Database=d:\db.fdb;User=SYSDBA;password=masterkey;ServerType=1;Pooling=false" />
< add key ="hibernate.query.substitutions" value ="true 1, false 0" />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.FirebirdDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.FirebirdDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Server=localhost;Database=d:\db.fdb;User=SYSDBA;password=masterkey;ServerType=1;Pooling=false" />
< add key ="hibernate.query.substitutions" value ="true 1, false 0" />
</ config >
</ activerecord >
5
.PostgreSQL
<
activerecord
>
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.NpgsqlDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.PostgreSQLDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=nhibernate;" />
</ config >
</ activerecord >
< config >
< add key ="hibernate.connection.driver_class" value ="NHibernate.Driver.NpgsqlDriver" />
< add key ="hibernate.dialect" value ="NHibernate.Dialect.PostgreSQLDialect" />
< add key ="hibernate.connection.provider" value ="NHibernate.Connection.DriverConnectionProvider" />
< add key ="hibernate.connection.connection_string" value ="Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=nhibernate;" />
</ config >
</ activerecord >
关于 ActiveRecord构建配置信息的介绍就这么多了,内容比较简单。下篇文章中我会详细介绍 ActiveRecord中的映射,希望研究过 Castle的朋友不吝赐教
。本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67657,如需转载请自行联系原作者