ActiveRecord 的多数据库配置基本沿袭了 NHibernate 的思想,只不过在配置文件结构上作了些调整。NHibernate的配置也是基于配置得来的,配置多个SessionFactory传入多个Base类
1. 采用继承方式,归纳使用同一数据库的类型。比如 A、B、C、D、E 中 A、B连接到数据库Test1,C、D连接到Test2,而E连接到缺省的Test,那么具体的代码就会是下面这种方式。
public abstract class Test1Base : ActiveRecordBase{
}
public abstract class Test2Base : ActiveRecordBase{
}
[ActiveRecord("A")]
public class A : Test1Base{
}
[ActiveRecord("B")]
public class B : Test1Base{
}
[ActiveRecord("C")]
public class C : Test2Base{
}
[ActiveRecord("D")]
public class D : Test2Base{
}
[ActiveRecord("E")]
public class E : ActiveRecordBase{
}
config.xml
<?xml version="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="Data Source=localhost;Initial Catalog=Test;UID=sa;Password=sa" />
</config>
<config type="ConsoleApplication1.CastleActiveRecord.Test1Base , Learn.CUI">
<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=localhost;Initial Catalog=Test1;UID=sa;Password=sa" />
</config>
<config type="ConsoleApplication1.CastleActiveRecord.Test2Base , Learn.CUI">
<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=localhost;Initial Catalog=Test2;UID=sa;Password=sa" />
</config>
</activerecord>
我们会发现,ActiveRecord 通过使用公用基类的方式来实现多个数据库连接配置的。只要继承自指定的基类,我们就可以使用不同的目标数据库。在配置文件中我们通过增加多个 Config Section,同时指定 Config Type 属性,就可以让 ActiveRecord 使用多数据库配置。Config Type 就是我们编写的基类,所有继承自该基类的类型都会使用该连接配置,而那些直接继承自 ActiveRecordBase 的类型还是使用缺省的连接配置。
上面的例子使用了 SQL Server 2000 的不同数据库,同样我们也可以连接到不同的数据库系统,如 A、B 连接到 SQL Server,C、D 连接到 DB2 等等。
2. 基类需要遵循一定的规则。
(1) 必须继承自 ActiveRecordBase。
(2) 必须是抽象类。
(3) 可以不添加 [ActiveRecord()] 特性。
(4) 必须初始化抽象基类。如 "ActiveRecordStarter.Initialize(source, typeof(Test1Base ));" ,如果使用 "ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);" 那么就给抽象基类添加 "[ActiveRecord()]" 即可。
下面是一个相对完整的代码演示。
namespace ConsoleApplication1.CastleActiveRecord{
[ActiveRecord()]
public abstract class Base : ActiveRecordBase {
} [ActiveRecord("Users")]
public class User : Base {
private int id; [PrimaryKey(PrimaryKeyType.Identity)]
public int Id {
get { Console.WriteLine("Id..."); return id; }
set { id = value; }
} private string name; [Property(Unique=true)]
public string Name {
get { return name; }
set { name = value; }
}
} public class ActiveRecordTest {
static ActiveRecordTest() {
// 获取数据库连接配置
XmlConfigurationSource source = new XmlConfigurationSource(@"Config/ActiveRecord.xml"); // 载入程序集中所有 ActiveRecord 类。
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source); // 自主载入指定类型
//ActiveRecordStarter.Initialize(source, typeof(ActiveRecordBase), typeof(User), typeof(Base)); // 删除数据库架构
//ActiveRecordStarter.DropSchema(); // 创建数据库架构(该方法会删除同名表后再创建)
ActiveRecordStarter.CreateSchema();
} public static void Test() {
User user = new User();
user.Name = "tom" + new Random(DateTime.Now.Millisecond).Next();
ActiveRecordMediator.Create(user);
}
}
} ActiveRecord.xml <?xml version="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="Data Source=localhost;Initial Catalog=Test2;UID=sa;Password=sa" />
</config>
<config type="ConsoleApplication1.CastleActiveRecord.Base, Learn.CUI">
<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=localhost;Initial Catalog=Test;UID=sa;Password=sa" />
</config>
</activerecord>