ActiveReocrd是Castle中一ORM框架,它对NHibernate做了进一步的封装。
1.环境搭建
a.程序集引用 (注意各程序集间版本兼容性)
Castle.ActiveRecord.dll
Castle.DynamicProxy2.dll
Iesi.Connections.dll
NHibernate.dll
Nhibernate.ByteCode.Castle.dll
/*
这一项根据自己需要来选择不同的延迟加载代理工厂
NHibernate.ByteCode.Castle.dll
NHibernate.ByteCode.LinFu.dll
NHibernate.ByteCode.Spring.dll
*/
b.配置
可以在应用程序配置文件和单独的配置文件中来进行配置,也可以通过编程方式配置
配置项参考nhibernate hibernate.cfg.xml
在应用程序配置文件中配置
<?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="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/> <add key="connection.connection_string" value="server=.;database=study;uid=sa;pwd=sa"/> <add key="show_sql" value="true"/> <add key="proxyfactory.factory_class"
value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /> </config> </activerecord> </configuration>
加载配置
IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);
在xml文件中配置
<?xml version="1.0" encoding="utf-8" ?> <activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/> <add key="connection.connection_string" value="server=.;database=study;uid=sa;pwd=sa"/> <add key="show_sql" value="true"/> <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /> </config> </activerecord>
加载配置
IConfigurationSource source = new XmlConfigurationSource("XMLFile2.xml");
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);
编程方式配置
InPlaceConfigurationSource source = new InPlaceConfigurationSource(); Hashtable properties = new Hashtable(); properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver"); properties.Add("dialect", "NHibernate.Dialect.MsSql2000Dialect"); properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider"); properties.Add("connection.connection_string", "UID=sa;Password=19811218;Initial Catalog=ARDemo;Data Source=."); properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"); source.Add(typeof(ActiveRecordBase), properties); ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);