Castle ActiveRecord学习实践(2):构建配置信息
发布时间:2020-12-13 18:02:24 所属栏目:百科 来源:网络整理
导读:摘要:ActiveRecord在底层封装了NHibernate,在框架启动时需要指定相关的配置信息,那么我们需要配置些什么?又该如何去配置呢?本文将会介绍在ActiveRecord中构建配置信息。 主要内容 1 .需要配置什么 2 .如何去配置 3 .常见的配置示例 一.需要配置什么
摘要:ActiveRecord在底层封装了NHibernate,在框架启动时需要指定相关的配置信息,那么我们需要配置些什么?又该如何去配置呢?本文将会介绍在ActiveRecord中构建配置信息。
主要内容
1
.需要配置什么
2
.如何去配置
3
.常见的配置示例
一.需要配置什么
在第一篇大家都已经看到了,其实我们的配置信息跟用NHibernate时的配置是一样的,这是因为ActiveRecord在底层封装了NHibernate。为了没有用过NHibernate的朋友,这里再把配置信息简单介绍一下。
1
.配置NHibernate ADO.NET属性
2
.可选的配置属性
除了上面的ADO.NET属性之外,我们还有如下的可选属性
可以数据库设置一个hibernate.dialect方言,它是NHibernate.Dialect.Dialect 的一个子类。如果不需要使用基于native或者sequence的主键自动生成算法,或者悲观锁定(使用ISession.Lock() 或者 IQuery.SetLockMode())的话,方言就可以不必指定。然而,假若你指定了一个方言,Hibernate会为上面列出的一些属性使用特殊默认值,省得我们手工指定。
NHibernate SQL
方言对照表:
二.如何去配置
ActiveRecord
为我们提供了三种方式的配置
1
.XmlConfigurationSource配置
可以使用自己的XML文件来保存配置信息,例如有一个MyConfig.xml的文件
<?
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 >
这时候我们在框架初始化的时候就应该这样写:
XmlConfigurationSourcesource
=
new
XmlConfigurationSource(
"
MyConfig.xml
"
);
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)); 3
这种方式是最为常见的一种,即使用应用程序的配置文件(Web.config 或者App.config),在配置文件中
<?
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 > 这时候我们的框架初始化代码应该这样写
[.NET1.1]
.在Web应用程序中的配置
IConfigurationSourcesource
=
System.Configuration.ConfigurationSettings.GetConfig(
"
activerecord
"
)
as
IConfigurationSource;
ActiveRecordStarter.Initialize(source, typeof (ActiveRecordBase)); [.NET2.0]
IConfigurationSourcesource
=
System.Configuration.ConfigurationManager.GetSection(
"
activerecord
"
)
as
IConfigurationSource;
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 > 一般的初始化工作我们会放在Application_ Start中,示例代码
protected
void
Application_Start(Objectsender,EventArgse)
{ IConfigurationSourcesource= System.Configuration.ConfigurationSettings.GetConfig("activerecord")asIConfigurationSource; ActiveRecordStarter.Initialize(source,typeof(ActiveRecordBase)); }
三.常见的配置示例
Castle网站为我们提供的几个常见的配置示例
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 >
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 > 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 > 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 >
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 > 关于ActiveRecord构建配置信息的介绍就这么多了,内容比较简单。下篇文章中我会详细介绍ActiveRecord中的映射,希望研究过Castle的朋友不吝赐教。
参考资料
Castle
的官方网站http://www.castleproject.org
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |