Castle ActiveRecord学习实践(10)——使用多数据库

我们在开发中会遇到这样的情况,创建的ActiveRecord类在不同的数据库中,本文来看看Castle ActiveRecord如何处理这种情况。

比如我们有 DB1数据库,里面包含一个Articles表,DB2数据库,里面包含一个AppInfo表

首先,我们创建抽象基类

 

   1:  public abstract class DB1<T> : ActiveRecordBase<T>
   2:  {
   3:  }

 

 

   1:  public abstract class DB2<T> : ActiveRecordBase<T>
   2:  {
   3:  }

 

抽象基类里面什么都没有,只是个空架子

其次,创建具体类

   1:  [ActiveRecord("Articles")]
   2:  public class Article : DB1<Article>
   3:  {
   4:      [PrimaryKey("id")]
   5:      public int Id
   6:      {
   7:          get;
   8:          set;
   9:      }
  10:      [Property("title")]
  11:      public string Title
  12:      {
  13:          get;
  14:          set;
  15:      }
  16:      [Property("text")]
  17:      public string Text
  18:      {
  19:          set;
  20:          get;
  21:      }
  22:  }
   1:  [ActiveRecord]
   2:  public class AppInfo : DB2<AppInfo>
   3:  {
   4:      [PrimaryKey("id")]
   5:      public int Id
   6:      {
   7:          get;
   8:          set;
   9:      }
  10:      [Property("appname")]
  11:      public string AppName
  12:      {
  13:          get;
  14:          set;
  15:      }
  16:      [Property("text")]
  17:      public string Text
  18:      {
  19:          get;
  20:          set;
  21:      }
  22:  }

最后,配置文件

   1:  <activeRecord isWeb="true">
   2:    <config type="Model.DB1`1,Model">
   3:      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
   7:      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
   4:      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
   5:      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
   6:      <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB1.mdf;Integrated Security=True;User Instance=True"/>
   8:    </config>
   9:    <config type="Model.DB2`1,Model">
  10:      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
  11:      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
  12:      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
  13:      <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB2.mdf;Integrated Security=True;User Instance=True"/>
  14:      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
  15:    </config>
  16:  </activeRecord>

global类

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

这样就完成了

需要注意:DB1和DB2是泛型类,它指定的类型名称为  type="Model.DB1`1,Model"

 

posted @ 2013-01-04 22:47  Ian.w  阅读(325)  评论(0编辑  收藏  举报