Castle ActiveRecord with multiple databases

A few months ago, I came cross this problem that I had 2 databases in my project, so that my model classes might map to tables from different databases. It took me a while to work out how to deal with it in ActiveRecord. Today, I had a same problem again, even though I still got my last project’s source code, it still took me a couple of hours to get everything right. So, I think I’d better to write it done, just in case that I will have to do it again or anyone else may need it.  

The Scenario:

Database 1:      MyDatabase                             Table:  Product

Database 2:      MyProfileDatabase                   Table: UserProfile

Model Class:    Product, UserProfile

 The Code:

First, just define Product class as normal ActiveRecord model class:

[ActiveRecord]

public class Product : ActiveRecordBase<Product>

{

    [PrimaryKey("ProductID")]

    public string Id { get; set; }

   

    //...

}

 

Create an abstract base class for MyProfileDatabase.  All classes map to tables in MyProfileDatabase will inherit from this base class. Note: it has to be abstract. This was what I missed out and caused some fun time to find out.

 

public abstract class ProfileActiveRecord<T> : ActiveRecordBase<T>

{

}

 

Define UserProfile class which inherits from ProfileActiveRecord.

[ActiveRecord]

public class UserProfile : ProfileActiveRecord<UserProfile>

{

    [PrimaryKey("UserID")]

    public int Id { get; set; }

 

    //....

}

 

ActiveRecord Configration:

<activerecord>

  <config>

    <add key="hibernate.connection.driver_class"

         value="NHibernate.Driver.SqlClientDriver" />

    <add key="hibernate.dialect"

         value="NHibernate.Dialect.MsSql2005Dialect" />

    <add key="hibernate.connection.provider"

         value="NHibernate.Connection.DriverConnectionProvider" />

    <add key="hibernate.connection.connection_string"

         value="server=localhost;database=MyDatabase;Integrated Security=True" />

  </config>

 

  <config type="SomeNamespace.Core.Models.ProfileActiveRecord`1, SomeNamespace.Core">

    <add key="hibernate.connection.driver_class"

         value="NHibernate.Driver.SqlClientDriver" />

    <add key="hibernate.dialect"

         value="NHibernate.Dialect.MsSql2005Dialect" />

    <add key="hibernate.connection.provider"

         value="NHibernate.Connection.DriverConnectionProvider" />

    <add key="hibernate.connection.connection_string"

         value="server=localhost; database=MyProfileDatabase; Integrated Security=True" />

  </config>

</activerecord>

Initialize Active Records:

XmlConfigurationSource source = new XmlConfigurationSource("ActiveRecord.xml");

Type[] types =

    {

        typeof(Product),

        typeof(ProfileActiveRecord<>),

        typeof(UserProfile)

    };

ActiveRecordStarter.Initialize(source, types);

 

Print | posted on Sunday, September 21, 2008 7:07 AM

Feedback

# re: Castle ActiveRecord with multiple databases

Left by Krigs at 1/16/2009 3:34 AM
Gravatar Good stuff. But to clearify the "config type"

"SomeNamespace.Core" should be the assembly name. Right?

Why "`1" after the class name?

# re: Castle ActiveRecord with multiple databases

Left by Jason Sirota at 2/28/2009 10:24 AM
Gravatar Exactly what I needed...

# re: Castle ActiveRecord with multiple databases

Left by Lyle at 5/13/2009 1:12 PM
Gravatar .net framework use this syntex to distinguish all the generic versions.

"`1" means the generic version of class ProfileActiveRecord which has only one type parameter.

So Doo.Foo<T1,T2> should be referenced as
Doo.Foo`2, DooAssembly

# re: Castle ActiveRecord with multiple databases

Left by benchfolks at 9/19/2017 12:51 AM
Gravatar Your all post is full of information with new coding ideas.actually i had underwent ONLINE TRAINING on Java programming for 1 month, they taught me easily.

# re: Castle ActiveRecord with multiple databases

Left by tanajka at 3/6/2018 12:20 AM
Gravatar Good stuff. But to clearify the "config type" :D

Useful websites: Read manga online for free in high quality and most full at mangafox.

Your comment:





 

Copyright © Changhong Fu

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski