[Activerecord]: Multi-Database Configuration

Source: Internet
Author: User

The multi-database configuration of activerecord basically follows the idea of nhib.pdf, but only makes some adjustments to the configuration file structure.
1. Use the Inheritance Method to summarize the types of the same database. For example, in A, B, C, D, E, A and B connect to the database test1, c, d connect to Test2, and E connect to the default test, then the specificCodeThe following method is used.

 
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 =" nhib.pdf. driver. sqlclientdriver "/> <add key =" hibernate. dialect "value =" nhib.pdf. dialect. mssql2000dialect "/> <add key =" hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <add key =" hibernate. connection. connection_string "value =" Data Source = localhost; initial catalog = test; uid = sa; Password = sa "/> </config> <config type =" leleapplication1.castleactiverecord. test1base, learn. cui "> <add key =" hibernate. connection. driver_class "value =" nhib.pdf. driver. sqlclientdriver "/> <add key =" hibernate. dialect "value =" nhib.pdf. dialect. mssql2000dialect "/> <add key =" hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <add key =" hibernate. connection. connection_string "value =" Data Source = localhost; initial catalog = test1; uid = sa; Password = sa "/> </config> <config type =" leleapplication1.castleactiverecord. test2base, learn. cui "> <add key =" hibernate. connection. driver_class "value =" nhib.pdf. driver. sqlclientdriver "/> <add key =" hibernate. dialect "value =" nhib.pdf. dialect. mssql2000dialect "/> <add key =" hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <add key =" hibernate. connection. connection_string "value =" Data Source = localhost; initial catalog = Test2; uid = sa; Password = sa "/> </config> </activerecord>

We will find that activerecord implements multiple database connection configurations by using the public base class. As long as we inherit from the specified base class, we can use different target databases. In the configuration file, we add multiple config sections and specify the config type attribute to enable activerecord to use multi-database configuration. Config type is the base class we wrote. All types inherited from this base class use the connection configuration, and those directly inherited from activerecordbase still use the default connection configuration.

The preceding example uses different databases of SQL Server 2000. We can also connect to different database systems, such as connecting a and B to SQL Server, and connecting C and D to DB2.

2. The base class must follow certain rules.

(1) it must inherit from activerecordbase.
(2) It must be an abstract class.
(3) do not add the [activerecord ()] feature.
(4) The abstract base class must be initialized. For example, "activerecordstarter. initialize (source, typeof (test1base); ", if you use" activerecordstarter. initialize (assembly. getexecutingassembly (), source); "add" [activerecord ()] "to the abstract base class.

The following is a complete code demonstration.

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 () {// get database connection configuration xmlconfigurationsource source = new xmlconfigurationsource (@ "config/activerecord. XML "); // Load Program Set all activerecord classes. Activerecordstarter. initialize (assembly. getexecutingassembly (), source); // automatically load the specified type // activerecordstarter. initialize (source, typeof (activerecordbase), typeof (user), typeof (base); // Delete the database architecture // activerecordstarter. dropschema (); // create a database schema (this method will delete the same table and then create it) activerecordstarter. createschema ();} public static void test () {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 =" nhib.pdf. driver. sqlclientdriver "/> <add key =" hibernate. dialect "value =" nhib.pdf. dialect. mssql2000dialect "/> <add key =" hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <add key =" hibernate. connection. connection_string "value =" Data Source = localhost; initial catalog = Test2; uid = sa; Password = sa "/> </config> <config type =" leleapplication1.castleactiverecord. base, learn. cui "> <add key =" hibernate. connection. driver_class "value =" nhib.pdf. driver. sqlclientdriver "/> <add key =" hibernate. dialect "value =" nhib.pdf. dialect. mssql2000dialect "/> <add key =" hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <add key =" hibernate. connection. connection_string "value =" Data Source = localhost; initial catalog = test; uid = sa; Password = sa "/> </config> </activerecord>

-----------------

Appendix: All the Demo code in this article uses the castle activerecord beta3 version released on February 1.
Castle activerecord may have many major changes before the release of version 1.0. If the Demo code cannot be compiled, we recommend that you refer to the relevant documents of the latest version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.