- Posted by Jakob Andersen on August 26, 2009
I have used nHibernate on linux for some time now without any major problems. The issues I have encountered have infact not been with Mono but with the MySql dialect in nHibernate. One of these where a nasty issue where nHibernate closed the connection to the database before retrieving the newly generated identifier which (in this case) unfortunately is bound to the current database connection. However I made a fix for this in the MySqlDialect and now it should be committet to nHibernate repository be included in next release.
Today I was starting a new project writing some tests and wanted to use SQLlite in memory for some pseudo integration testing, Mono has built-in support for SQLite in the Mono.Data.Sqlite assembly however nHibernate isn't aware of this and hence espects that you have the SQLite.NET assembly and the SQLite binaries present either in your bin directory or in the GAC. To avoid having to deploy these on my linux box which already has a .NET implementation of SQLite's IDbConnection and IDbCommand i turned to one of the extensions points in nHibernate and made my own Driver as follows:
public class MonoSqliteDriver : NHibernate.Driver.ReflectionBasedDriver
{
public MonoSqliteDriver() :
base("Mono.Data.Sqlite",
"Mono.Data.Sqlite.SqliteConnection",
"Mono.Data.Sqlite.SqliteCommand")
{
}
public override bool UseNamedPrefixInParameter {
get {
return true;
}
}
public override bool UseNamedPrefixInSql {
get {
return true;
}
}
public override string NamedPrefix {
get {
return "@";
}
}
public override bool SupportsMultipleOpenReaders {
get {
return false;
}
}
}
Initially I wanted to derive from the existing SQLiteDriver in nHibernate, but unfortunately the important part is the call to the base constructor on ReflectionBasedDriver, and as SqlLiteDriver hides the constructor of this i can't hook in and provide the three parameters that I need to change. The things changed is the three strings in the constructor call, "Mono.Data.Sqlite" is the assembly where the IDbConnection and IDbCommand implementations are located, and the other two are the names of the implementation. The overridden properties are taken from the existing SqliteDriver in the nHibernate source.
Finally to use our driver we just specify it in our nHibernate configuration
<add key="connection.driver_class" value="Name.Space.MonoSqliteDriver, AssemblyName" />
Conclusion: Extension points are indeed nice, and the stack I use including nHibernate has a lot of them which makes my life a lot easier