Castle Connecting multiple database configurations

Source: Internet
Author: User

ActiveRecord's multi-database configuration basically inherits the NHibernate idea, but makes some adjustments on the configuration file structure.
1. Use the inheritance method to summarize the type of the same database. such as a, B, C, D, E in a, B connected to the database test1,c, D to Test2, and E to the default test, then the specific code will be the following way.

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:test2bas E{}[activerecord ("D")]public class D:test2base{}[activerecord ("E")]public class e:activerecordbase{}

Config.

<?xml version= "1.0" encoding= "Utf-8"?><activerecord> <config> <add key= "Hibernate.connecti On.driver_class "value=" NHibernate.Driver.SqlClientDriver "/> <add key=" Hibernate.dialect "value=" NHibernate. Dialect.mssql2000dialect "/> <add key=" Hibernate.connection.provider "value=" NHibernate.Connection.DriverCon Nectionprovider "/> <add key=" hibernate.connection.connection_string "value=" Data source=localhost;initial Ca Talog=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= "Hibern Ate.dialect "value=" NHibernate.Dialect.MsSql2000Dialect "/> <add key=" Hibernate.connection.provider "value=" N Hibernate.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= "Hibern Ate.dialect "value=" NHibernate.Dialect.MsSql2000Dialect "/> <add key=" Hibernate.connection.provider "value=" N Hibernate.Connection.DriverConnectionProvider "/> <add key=" hibernate.connection.connection_string "value=" D ATA source=localhost;initial Catalog=test2; Uid=sa; Password=sa "/> </config></activerecord>

We will find that ActiveRecord implements multiple database connection configurations by using a common base class. As long as you inherit from the specified base class, we can use a different target database. In the configuration file we can let ActiveRecord use the multi-database configuration by adding multiple config sections and specifying the Config Type property. The config type is the base class we write, and all types that inherit from that base class will use that connection configuration, and those that inherit directly from Activerecordbase or use the default connection configuration.
The above example uses different databases for SQL Server 2000, and we can also connect to different database systems, such as A, B connected to SQL Server,c, D to DB2, and so on.
2. The base class needs to follow certain rules.
(1) must inherit from Activerecordbase.
(2) must be an abstract class.
(3) The [ActiveRecord ()] attribute may not be added. The
(4) must initialize the abstract base class. such as "activerecordstarter.initialize (source, typeof (Test1base));", if you use the Activerecordstarter.initialize ( Assembly.getexecutingassembly (), source); "Then add" [ActiveRecord ()] "to the abstract base class.
The following is a relatively 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");            Loads all ActiveRecord classes in the assembly.            Activerecordstarter.initialize (assembly.getexecutingassembly (), source); Autonomous loading of the specified type//activerecordstarter.initialize (source, typeof (Activerecordbase), TyPeof (User), typeof (Base));                        Delete Database schema//activerecordstarter.dropschema ();        Create a database schema (this method deletes the same name and then creates it) 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.connecti On.driver_class "value=" NHibernate.Driver.SqlClientDriver "/> <add key=" Hibernate.dialect "value=" NHibernate. Dialect.mssql2000dialect "/> <add key=" Hibernate.connection.provider "value=" NHibernate.Connection.DriverCon Nectionprovider "/> <add key=" hibernate.connection.connection_string "value=" Data source=localhost;initial Ca Talog=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" V Alue= "NHibernate.Connection.DriverConnectionProvider"/> <add key= "hibernate.connection.connection_string" V Alue= "DATA source=localhost;initial catalog=test; Uid=sa; Password=sa "/> </config></activerecord>

-----------------
Attached: All demo code for this article uses the Castle ActiveRecord Beta3 version released in 2006-01-01.
Castle ActiveRecord There may be a lot of changes before the release of version 1.0, such as the demo code cannot be compiled, it is recommended that you refer to the latest version of the relevant documentation

Castle Connecting multiple database configurations

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.