摘要:ActiveRecord在底层封装了NHibernate,在框架启动时需要指定相关的配置信息,那么我们需要配置些什么?又该如何去配置呢?本文将会介绍在ActiveRecord中构建配置信息。
@H_404_4@
主要内容
@H_404_4@
1
.需要配置什么
@H_404_4@
2
.如何去配置
@H_404_4@
3
.常见的配置示例
@H_404_4@
@H_404_4@
一.需要配置什么
@H_404_4@
在第一篇大家都已经看到了,其实我们的配置信息跟用NHibernate时的配置是一样的,这是因为ActiveRecord在底层封装了NHibernate。为了没有用过NHibernate的朋友,这里再把配置信息简单介绍一下。
@H_404_4@
1
.配置NHibernate ADO.NET属性
@H_404_4@
说明
@H_404_4@ |
|
hibernate.connection.provider_class
@H_404_4@ |
|
hibernate.connection.driver_class
@H_404_4@ |
|
hibernate.connection.connection_string
@H_404_4@ |
用来获得连接的连接字符串。
@H_404_4@ |
hibernate.connection.isolation
@H_404_4@ |
2
.可选的配置属性
@H_404_4@
除了上面的ADO.NET属性之外,我们还有如下的可选属性
@H_404_4@
说明
@H_404_4@ |
|
hibernate.dialect
@H_404_4@ |
|
hibernate.default_schema
@H_404_4@ |
|
hibernate.session_factory_name
@H_404_4@ |
|
hibernate.use_outer_join
@H_404_4@ |
|
hibernate.cache.provider_class
@H_404_4@ |
|
hibernate.query.substitutions
@H_404_4@ |
可以数据库设置一个hibernate.dialect方言,它是NHibernate.Dialect.Dialect 的一个子类。如果不需要使用基于native或者sequence的主键自动生成算法,或者悲观锁定(使用ISession.Lock() 或者 IQuery.SetLockMode())的话,方言就可以不必指定。然而,假若你指定了一个方言,Hibernate会为上面列出的一些属性使用特殊默认值,省得我们手工指定。
@H_404_4@
NHibernate sql
方言对照表:
@H_404_4@
DB2
@H_404_4@ |
NHibernate.Dialect.DB2Dialect
@H_404_4@ |
Oracle (any version)
@H_404_4@ |
NHibernate.Dialect.OracleDialect
@H_404_4@ |
Oracle 9/10g
@H_404_4@ |
NHibernate.Dialect.Oracle9Dialect
@H_404_4@ |
Sybase
@H_404_4@ |
NHibernate.Dialect.SybaseDialect
@H_404_4@ |
Firebird
@H_404_4@ |
NHibernate.Dialect.FirebirdDialect
@H_404_4@ |
@H_404_4@
二.如何去配置
@H_404_4@
ActiveRecord
为我们提供了三种方式的配置
@H_404_4@
1
.XmlConfigurationSource配置
@H_404_4@
可以使用自己的XML文件来保存配置信息,例如有一个MyConfig.xml的文件
@H_404_4@
<?
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 > @H_404_4@
< 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 > @H_404_4@
这时候我们在框架初始化的时候就应该这样写:
@H_404_4@
XmlConfigurationSourcesource
=
new
XmlConfigurationSource(
"
MyConfig.xml
"
);
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
其中XmlConfigurationSource通过重载提供了如下三个公用的构造函数
@H_404_4@
public XmlConfigurationSource(String xmlFileName)
@H_404_4@
public XmlConfigurationSource(Stream stream)
@H_404_4@
public XmlConfigurationSource(TextReader reader)
@H_404_4@
不管是以文件名还是Stream形式或者TextReader,在XmlConfigurationSource的内部都会转换为XmlDocument。最后要注意xml文件的路径,可以用生成后事件命令拷贝.xml文件到bin目录下
@H_404_4@
copy "$(ProjectDir)/*.xml" "$(TargetDir)"
@H_404_4@
2
.InPlaceConfigurationSource配置
@H_404_4@
这种实现是一种硬编码的方式,在实际的使用中并不推荐,但是有时候如果我们的配置信息是动态的获取,则这种方式就会变得非常有用。
.使用应用程序配置文件
@H_404_4@
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)); @H_404_4@
3 @H_404_4@
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)); @H_404_4@
3 @H_404_4@
这种方式是最为常见的一种,即使用应用程序的配置文件(Web.config 或者App.config),在配置文件中
@H_404_4@
@H_404_4@
<?
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 > @H_404_4@
@H_404_4@
< 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 > @H_404_4@
@H_404_4@
这时候我们的框架初始化代码应该这样写
@H_404_4@
[.NET1.1]
.在Web应用程序中的配置
@H_404_4@
IConfigurationSourcesource
=
System.Configuration.ConfigurationSettings.GetConfig(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
[.NET2.0] @H_404_4@
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
[.NET2.0] @H_404_4@
IConfigurationSourcesource
=
System.Configuration.ConfigurationManager.GetSection(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
4 @H_404_4@
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); @H_404_4@
4 @H_404_4@
如果我们是在Web应用程序中使用ActiveRecord,需要指定isWeb="true",如下
@H_404_4@
@H_404_4@
<
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 > @H_404_4@
@H_404_4@
< 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 > @H_404_4@
@H_404_4@
一般的初始化工作我们会放在Application_ Start中,示例代码
@H_404_4@
protected
void
Application_Start(Objectsender,EventArgse)
{
IConfigurationSourcesource=
System.Configuration.ConfigurationSettings.GetConfig("activerecord")asIConfigurationSource;
ActiveRecordStarter.Initialize(source,typeof(ActiveRecordBase));
} @H_404_4@
{
IConfigurationSourcesource=
System.Configuration.ConfigurationSettings.GetConfig("activerecord")asIConfigurationSource;
ActiveRecordStarter.Initialize(source,typeof(ActiveRecordBase));
} @H_404_4@
@H_404_4@
三.常见的配置示例
@H_404_4@
Castle网站为我们提供的几个常见的配置示例
1 .MS sqlServer
@H_404_4@
1 .MS sqlServer
@H_404_4@
<
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 > @H_404_4@
@H_404_4@
< 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 > @H_404_4@
@H_404_4@
2
.Oracle
@H_404_4@
@H_404_4@
<
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 > @H_404_4@
@H_404_4@
< 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 > @H_404_4@
@H_404_4@
3.MysqL
@H_404_4@
<
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 > @H_404_4@
< 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 > @H_404_4@
@H_404_4@
4.Firebird
@H_404_4@
<
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 > @H_404_4@
< 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 > @H_404_4@
@H_404_4@
5
.Postgresql
@H_404_4@
<
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 > @H_404_4@
关于ActiveRecord构建配置信息的介绍就这么多了,内容比较简单。下篇文章中我会详细介绍ActiveRecord中的映射,希望研究过Castle的朋友不吝赐教。 @H_404_4@
< 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 > @H_404_4@
关于ActiveRecord构建配置信息的介绍就这么多了,内容比较简单。下篇文章中我会详细介绍ActiveRecord中的映射,希望研究过Castle的朋友不吝赐教。 @H_404_4@