The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  83 Posts :: 0 Stories :: 57 Comments :: 2 Trackbacks

Ads

 

Donate via PayPal...

...if you feel the site helped.

Archives

Post Categories

Open Source Projects

Other Blogs

There are a few solutions out on the web for this – here’s my implementation for you to use (and for me to safe-keep). I use this primarily for testing my ActiveRecord objects, as I haven’t really found any use for this mechanism in any production environment

Firstly, the configuration. Notice that I specify my own connection provider (make sure you correctly swap out the relevant portions with your own namespaces). Also, there’s a slight twist in the connection string that you may want to take note of. The rest is straight forward.

  <activerecord isDebug="true" isWeb="false">
    <config>
      <add key="connection.provider" 
           value="Darkside.ARLibrary.SqLiteConnectionProvider, Darkside.ARLibrary"/>
      <add key="dialect" value="NHibernate.Dialect.SQLiteDialect"/>
      <add key="connection.driver_class" value="NHibernate.Driver.SQLite20Driver"/>
      <add key="connection.connection_string" value="Data Source=:memory:;Version=3;New=True;"/>
      <add key="proxyfactory.factory_class" 
           value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
      <add key="show_sql" value="true"/>
    </config>
  </activerecord>

Next, you’ll need to implement your own connection provider. The reason this needs to be done is because when you close a connection on a SQLite database which you’ve configured to be in-memory, the entire contents (tables and data) is lost.

You simply inherit from NHibernate.Connection.DriverConnectionProvider and override the GetConnection() and CloseConnection() methods. In your implementation of GetConnection(), you cache an instance of the connection and always return the same one. In CloseConnection(), you do nothing – you don’t need or want to close the connection.

    public class SqLiteConnectionProvider : NHibernate.Connection.DriverConnectionProvider
    {
        private static System.Data.IDbConnection m_Connection;
        public override System.Data.IDbConnection GetConnection()
        {
            if (m_Connection == null) 
                m_Connection = base.GetConnection();
            return m_Connection;
        }
 
        public override void CloseConnection(System.Data.IDbConnection conn)
        {
            //Do nothing
        }
    }
posted on Wednesday, July 07, 2010 7:36 PM
Comments have been closed on this topic.