我的ActiveRecord学习之路(二):配置文件
上一篇文章主要写了AR的一些简单的操作,目的是让大家对AR有一定的了解,今天我主要来说一下AR里的配置文件的使用以及几种常见数据库的配置。
本文参考与:http://www.castleproject.org/index.php/ActiveRecord:Configuration_Reference
首先引用Castle官方网站上的一句话:It's necessary to specify some information in order to use ActiveRecord.在配置文件里我们需要指定数据库的连接字符串,数据源驱动等信息。
一、使用XML进行配置,如下:
<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;Integrated Security=SSPI" />
</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;Integrated Security=SSPI" />
</config>
</activerecord>
然后在我们的程序中应该这样初始化:
ActiveRecordStarter.Initialize( new XmlConfigurationSource(@"myfile.xml"), typeof(Company) );
二、使用应用程序的配置文件进行配置(web程序的Web.Config或应用程序的App.Config)
<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="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
</config>
</activerecord>
</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="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
</config>
</activerecord>
</configuration>
在程序中的初始化方法如下:
IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource; //this for .net 1.1
IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource; //this for .net 2.0
ActiveRecordStarter.Initialize( source, typeof(Company) );
//如果为多个实体初始化,Initialize()方法的第二个参数可以是一个Type[]类型,这个数组里存放需要初始化的实体类
Type[] paramTypes = new Type[2];
paramTypes[0] = typeof(Company);
paramTypes[1] = typeof(People);
ActiveRecordStarter.Initialize( source,paramTypes);
IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource; //this for .net 2.0
ActiveRecordStarter.Initialize( source, typeof(Company) );
//如果为多个实体初始化,Initialize()方法的第二个参数可以是一个Type[]类型,这个数组里存放需要初始化的实体类
Type[] paramTypes = new Type[2];
paramTypes[0] = typeof(Company);
paramTypes[1] = typeof(People);
ActiveRecordStarter.Initialize( source,paramTypes);
三、动态配置(InPlace)
这种方法主要用于动态创建应用程序的配置信息,这个时候就不能用XML了。
InPlaceConfigurationSource config = new InPlaceConfigurationSource();
Hashtable myproperties = new Hashtable();
//在myproperties中就可以动态加入一些配置信息了
myproperties.add("","");
config.Add( typeof(ActiveRecordBase), myproperties );
ActiveRecordStarter.Initialize( source, typeof(Company),typeof(People) );
Hashtable myproperties = new Hashtable();
//在myproperties中就可以动态加入一些配置信息了
myproperties.add("","");
config.Add( typeof(ActiveRecordBase), myproperties );
ActiveRecordStarter.Initialize( source, typeof(Company),typeof(People) );
四、几种常见数据库的配置(摘自Castle官方网站)
1、SQL Server
<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;Integrated Security=SSPI" />
</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;Integrated Security=SSPI" />
</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>
关于AR的配置就讲这么多了,希望对初学者有一定的帮助。<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>
还有需要注意的一点,我们在写程序的时候对AR框架的初始化一般都放在global.asax.cs中的Application_start()方法里,如下:
public class GlobalApplication : HttpApplication
{
public void Application_Start()
{
IConfigurationSource source =
System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof(Company), typeof(People) );
}
}
{
public void Application_Start()
{
IConfigurationSource source =
System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize( source, typeof(Company), typeof(People) );
}
}
posted on 2006-05-21 11:53 Daniel Pang 阅读(2312) 评论(0) 编辑 收藏 举报