The request for rapid to support multiple database providers was made by many users of this framework. To think of it, an efficient ORM framework should be able to connect to multiple data sources at the same time. Most database designs, legacy applications etc are created using one database. But for some reasons, some situations allow us to use multiple databases to achieve certain application requirements.

Rapid is a framework built on top of databases, i i do not see a reason why rapid should not support multiple databases (These databases could be different (Oracle versus Mysql) and migth be on different servers).

You can setup rapid to use as many as possible database providers. In-fact rapid allows you to use mixtures of databases within a single application. Following this scenario:

Scenario 1
We have two databases, one is Microsoft access and one is Mysql, we would like to connect to both at runtime and perform operation on it using one rapid configuration manager. Note, the two databases are different in the area of providers (Access and Mysql), so how do we achieve this using Rapid.

Setting Configuration to allow multiple database

ConfigurationFactory factory = ConfigurationFactory.GetInstance(ConfigurationManager.ConnectionStrings["MSAccess"].ConnectionString);
factory.ProviderDialect = ProviderDialect.OleDbProvider;
//Multiple Database test.
factory.RegisterSiblingConnection("Mysql2", ProviderDialect.MySQLProvider, ConfigurationManager.ConnectionStrings["MySql"].ConnectionString);

A simple look at the code above shows that we are registering two database providers into rapid. One is Microsoft access and the other (Registered as a sibling) is MySQL database. Rapid entity requires you to set one of the database providers as the parent provider (In our case, Microsoft access), and the other providers will be registered as siblings of the parent. from the example above, MySql is a sibling to Microsoft access database. Note the first parameter of the method RegisterSiblingConnection is an alias of the database provider which you are registering as a sibling, you will need to use this alias when instantiating an EntityManager class .

Now, how do we use parent and sibling

Using Parent : A parent provider is the default behavior of rapid framework. The following code is what you should be familiar with already when using a parent provider:

using(EntityManager manager = new EntityManager())
{
       manager.OpenDatabaseSession();
       Customer customer = manager.LoadEntity(typeof(Customer), 2);
}

Using A sibling Provider : There are no real difference but you will need the alias to tell rapid which provider you want to use for the particular operation. An example follows :

//Using a sibling provider
using (EntityManager manager = EntityManager.OpenOn("Mysql2"))
{
    manager.OpenDatabaseSession();

    IList customers = manager.GetAllLazily(typeof(Customer));
    Customer customer = customers[0] as Customer;
}

The above code makes use of EntityManager.OpenOn("Mysql2") to create an instance of the EntityManager class by supplying the alias Mysql2 which we registered as a sibling in the configuration.

Using Sibling within parent (At the same time) : You are allowed to use the sibling provider within the context of the parent provider if you wish. The following example describes the scenario :

using(EntityManager manager = new EntityManager())
{
       manager.OpenDatabaseSession();  //Parent context begins

       using (EntityManager manager = EntityManager.OpenOn("Mysql2"))  //Sibling context begins
       {
            manager.OpenDatabaseSession();
       } //Sibling context ends

}//parent context ends

Coming Soon (Based on feasibility). Cross Provider Query
We are making serious efforts that you get good functionality while using rapid entity framework. One challenge that we faced while implementing multiple database support is cross database query. We are researching this area and once we are happy with the outcome of the research, we will enable this functionality. Note* cross database query for Rapid will means a query that can be used across multiple providers (From access to mysql, from mysql to microsoft sql server) etc You can make contribution to this.

Last edited Feb 23, 2010 at 2:03 PM by ahmedsalako, version 9

Comments

No comments yet.