We have done this by creating an SqlMapClient that handles all access to the 
Sql Mapper. We created a ProxyFactory that proxies the original DataSource in 
the SqlMapper. This way, we can resolve the datasource that the mapper uses at 
runtime. We let IBatis configure the datasource, and only proxy it when we need 
to.

 

We use Spring.NET to configure the possible proxies, for instance, we want to 
resolve a different database based on the URL, but our mapper class does not 
have to know that it is running in an ASP.NET environment. We configure the 
proxies somewhere else.

 

A code snippet from our SqlMapClient: 

 

 

DomSqlMapBuilder builder = new DomSqlMapBuilder();

mapper = builder.Configure(SqlMapConfig);

mapper.DataSource = 
dataSourceProxyFactory.GetProxiedDataSource(mapper.DataSource);

 

And our datasource proxyfactory (a generic, so we can use it to instantiate any 
type of proxy) looks like this:

 

    /// <summary>

    /// Provides a generic way of wrapping IBatis DataSource objects

    /// </summary>

    /// <typeparam name="T">The type of proxy to use</typeparam>

    public class GenericIBatisDataSourceProxyFactory<T> : 
IDataSourceProxyFactory where T : IDataSourceProxy

    {

        /// <summary>

        /// Creates a new instance of T, assigns the datasource to it, and 
returns it.

        /// </summary>

        /// <param name="dataSource">The datasource to proxy</param>

        /// <returns>The proxied datasource of type T</returns>

        public IDataSource GetProxiedDataSource(IDataSource dataSource)

        {

            T dataSourceProxy = Activator.CreateInstance<T>();

            dataSourceProxy.DataSource = dataSource;

 

            return dataSourceProxy;

        }

    }

 

And IDataSourceProxy is just a IDataSource that has a setter for the proxied 
datasource itself, so it basically wraps the calls to the original datasource, 
changing when necessary.

 

This way every data access objects can use the same Sql Mapper (we use 
singleton dao’s, so we can’t get a separate instance for each datasource) but 
we can pick the database at runtime by modifying the connection string in the 
datasource proxy.

 

Hope this helps.

 

 

Kamiel Wanrooij
EveryWeb Solutions

 

Vinkenstraat 3bg
1013 JL Amsterdam, The Netherlands
T: +31 20 6236445 / +31 614194798
E: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 
W: http://www.everywebsolutions.nl <http://www.everywebsolutions.nl/> 

 

From: smartkid [mailto:[EMAIL PROTECTED] 
Sent: Saturday, January 26, 2008 4:16 PM
To: [email protected]
Subject: 答复: Multiple Datasources?

 

〉〉It sounds like it can't be done in an application that has a base data access 
class that all other data access implementations inherit from.

 

It depends on what your base data access class looks like~~~~~I suggest your 
data access class can takes an ISqlMapper instance as a constructor parameter, 
or better, applies the factory pattern for the creation of the data access 
classes.

 

>> It would be really nice if the datasource could be set on a map file by map 
>> file basis.

I don’t understand what u really mean L

The datasource is set in SqlMap.xml, for a multi database application, you 
should have multiple SqlMap.xml files, and the datasource element in each 
SqlMap.xml points to a different database.

 

发件人: Vincent Apesa [mailto:[EMAIL PROTECTED] 
发送时间: 2008年1月26日 22:41
收件人: [email protected]; [EMAIL PROTECTED]
主题: Re: Multiple Datasources?

 

It sounds like it can't be done in an application that has a base data access 
class that all other data access implementations inherit from.

 

It would be really nice if the datasource could be set on a map file by map 
file basis.

 

Vince

        ----- Original Message ----- 

        From: smartkid <mailto:[EMAIL PROTECTED]>  

        To: [email protected] 

        Sent: Saturday, January 26, 2008 2:19 AM

        Subject: 答复: Multiple Datasources?

         

        Hi, 

         

        1.       Creates a SqlMap.xml and data maps for each database.

        2.       Creates a DomSqlMapBuilder instance for each database.

        3.       Call DomSqlMapBuilder.Configure(…) with the corresponding 
SqlMap.xml for each instance.

        4.       Use the ISqlMapper instances returned from the previous step 
for accessing a certain database.

         

         

        Pseudo code:

         

        DomSqlMapBuilder builderA = new DomSqlMapBuilder();

        ISqlMapper mapperA = builderA.Configure(“sqlMap_A.xml”);

         

        DomSqlMapBuilder builderB = new DomSqlMapBuilder();

        ISqlMapper mapperB = builder.Configure(“sqlMap_B.xml”);

         

         

        Smartkid

         

        发件人: Nathan O. Miller [mailto:[EMAIL PROTECTED] 
        发送时间: 2008年1月26日 2:38
        收件人: [email protected]
        主题: Multiple Datasources?

         

        Hello everyone,

         

        I have a web application that contains individual tools that each use 
their own database.  Is there some way for IBatis to support the use of 
multiple datasources for something like this?  

         

        Thanks,

        Nathan Miller

Reply via email to