摘要: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
|
例如:
Chaos,ReadCommitted,ReadUncommitted,RepeatableRead,Serializable,Unspecified
|
2
.可选的配置属性
属性名
|
说明
|
hibernate.dialect
|
NHibernate
方言(Dialect)的类名 - 可以让NHibernate使用某些特定的数据库平台的特性
例如:
full.classname.of.Dialect (如果方言创建在NHibernate中),或者full.classname.of.Dialect,assembly (如果使用一个自定义的方言的实现,它不属于NHibernate)。
|
hibernate.default_schema
|
例如:
SCHEMA_NAME
|
hibernate.prepare_sql
|
是否准备sql语句
例如:
true
| false
|
hibernate.session_factory_name
|
例如:
some.name
|
hibernate.use_outer_join
|
允许使用外连接抓取。
例如:
true
| false
|
hibernate.cache.provider_class
|
例如:
full.classname.of.CacheProvider
(如果ICacheProvider创建在NHibernate中),或full.classname.of.CacheProvider,assembly (如果使用一个自定义的ICacheProvider,它不属于NHibernate)。
|
hibernate.query.substitutions
|
可以数据库设置一个hibernate.dialect方言,它是NHibernate.Dialect.Dialect 的一个子类。如果不需要使用基于native或者sequence的主键自动生成算法,或者悲观锁定(使用ISession.Lock() 或者 IQuery.SetLockMode())的话,方言就可以不必指定。然而,假若你指定了一个方言,Hibernate会为上面列出的一些属性使用特殊默认值,省得我们手工指定。
NHibernate sql
方言对照表:
数据库系统
|
sql
方言
|
DB2
|
NHibernate.Dialect.DB2Dialect
|
Postgresql
|
NHibernate.Dialect.PostgresqlDialect
|
NHibernate.Dialect.MysqLDialect
|
|
Oracle (any version)
|
NHibernate.Dialect.OracleDialect
|
Oracle 9/10g
|
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配置
<?
xmlversion="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 ="DataSource=.;InitialCatalog=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 ="DataSource=.;InitialCatalog=ARDemo;UID=sa;Password=sa" />
</ config >
</ activerecord >
这时候我们在框架初始化的时候就应该这样写:
XmlConfigurationSourcesource
=
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配置
InPlaceConfigurationSourcesource
=
new
InPlaceConfigurationSource();
Hashtableproperties = 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;InitialCatalog=ARDemo;DataSource=. " );
source.Add( typeof (ActiveRecordBase),properties);
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
Hashtableproperties = 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;InitialCatalog=ARDemo;DataSource=. " );
source.Add( typeof (ActiveRecordBase),properties);
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
3
<?
xmlversion="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;InitialCatalog=ARDemo;DataSource=." />
</ 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;InitialCatalog=ARDemo;DataSource=." />
</ config >
</ activerecord >
</ configuration >
这时候我们的框架初始化代码应该这样写
[.NET1.1]
.在Web应用程序中的配置
IConfigurationSourcesource
=
System.Configuration.ConfigurationSettings.GetConfig(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
[.NET2.0]
IConfigurationSourcesource
=
System.Configuration.ConfigurationManager.GetSection(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase));
4
如果我们是在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;InitialCatalog=ARDemo;DataSource=." />
</ 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;InitialCatalog=ARDemo;DataSource=." />
</ config >
</ activerecord >
一般的初始化工作我们会放在Application_ Start中,示例代码
protected
void
Application_Start(Objectsender,EventArgse)
{
IConfigurationSourcesource=
System.Configuration.ConfigurationSettings.GetConfig("activerecord")asIConfigurationSource;
ActiveRecordStarter.Initialize(source,typeof(ActiveRecordBase));
}
{
IConfigurationSourcesource=
System.Configuration.ConfigurationSettings.GetConfig("activerecord")asIConfigurationSource;
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 ="DataSource=.;InitialCatalog=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 ="DataSource=.;InitialCatalog=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 ="DataSource=dm;UserID=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 ="DataSource=dm;UserID=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;DataSource=someip;UserId=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;DataSource=someip;UserId=blah;Password=blah" />
</ config >
</ activerecord >
4.Firebird
<
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 ="true1,false0" />
</ 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 ="true1,false0" />
</ 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;initialcatalog=nhibernate;UserID=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;initialcatalog=nhibernate;UserID=nhibernate;Password=nhibernate;" />
</ config >
</ activerecord >
参考资料
Castle
的官方网站http://www.castleproject.org