Castle ActiveRecord学习实践(3)配置与初始化

前面两篇文章大致对Castle ActiveRecord 有了了解。现在要运行程序,还有一个重要的步骤没做处理,就是初始化和配置。

先来看看需要配置什么信息

1、使用应用程序配置文件(Web.config 或者App.config)

   1:  <configuration>
   2:    <configSections>
   3:      <section name="activeRecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler,Castle.ActiveRecord"/>
   4:    </configSections>
   5:    <activeRecord isWeb="true">
   6:      <config>
   7:        <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
   8:        <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
   9:        <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
  10:        <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Blog.mdf;Integrated Security=True;User Instance=True"/>
  11:        <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
  12:      </config>
  13:    </activeRecord>
  14:      <system.web>
  15:          <compilation debug="true" targetFramework="4.0" />
  16:      </system.web>
  17:   
  18:  </configuration>

 

初始化代码 Global.asax.cs 引用

   1:  protected void Application_Start(object sender, EventArgs e)
   2:  {
   3:          IConfigurationSource source = ConfigurationManager.GetSection("activeRecord") as IConfigurationSource;
   4:         //Castle.ActiveRecord.ActiveRecordStarter.Initialize(source, typeof(Post), typeof(Comment),typeof(Tag),typeof(Category),typeof(TagPost));
   5:         Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Model"), source);
   6:  }

第4行代码为载入目标类型参数 Initialize(IConfigurationSource source, params Type[] types);

第5行为Initialize(Assembly assembly, IConfigurationSource source)

2、XmlConfigurationSource配置

可以使用自己的xml文件来保存配置信息。

ActiveRecordConfig.xml文件

   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <activeRecord isWeb="true">
   3:    <config>
   4:      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
   5:      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
   6:      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
   7:      <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Blog.mdf;Integrated Security=True;User Instance=True"/>
   8:      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
   9:    </config>
  10:  </activeRecord>

 

初始化代码

   1:  protected void Application_Start(object sender, EventArgs e)
   2:  {
   3:        XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("~/ActiveRecordConfig.xml"));
   4:        Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Model"), source);
   5:  }

注意引入 Castle.ActiveRecord.Framework.Config

 

3、InPlaceConfigurationSource配置

这种实现是一种硬编码的方式,在实际的使用中并不推荐,但是有时候如果我们的配置信息是动态的获取,则这种方式就会变得非常有用。

   1:   protected void Application_Start(object sender, EventArgs e)
   2:   {
   3:       InPlaceConfigurationSource source = new InPlaceConfigurationSource();
   4:   
   5:       Dictionary<string, string> properties = new Dictionary<string, string>();
   6:   
   7:       properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
   8:   
   9:       properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
  10:   
  11:       properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
  12:   
  13:       properties.Add("connection.connection_string", @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Blog.mdf;Integrated Security=True;User Instance=True");
  14:   
  15:       properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
  16:   
  17:       source.Add(typeof(ActiveRecordBase), properties);
  18:   
  19:       ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Model"), source);
  20:   }

注意 需要添加Castle.Core.dll

posted @ 2012-10-13 01:09  Ian.w  阅读(424)  评论(0编辑  收藏  举报