How to use with ServiceStack OrmLite?


I’d love to use MiniProfiler along with ServiceStack OrmLite, but can’t figure out how to get it working. It seems that ProfiledDbConnection can only take an instance of DbConnection, but OrmLite’s connections don’t inherit from DbConnection (they do implement IDbConnection though):

public class OrmLiteConnection : IDbConnection, IDisposable, IHasDbConnection

Is there a way to use OrmLite with OrmLite?



Figured this out myself :). You need to attach to the inner connection, which is the actual database connection. Here’s the code I’m using - I’m using Simple Injector but you can just pull out the “new OrmLiteConnectionFactory” code and use it separately.

// By default, don't add a filter to the DB connection factory
Func<IDbConnection, IDbConnection> connFilter = conn => conn;
// Check if profiling is enabled (shouldn't add extra overhead if it's not enabled)
if (enableProfiling)
	// Yes, so we need to add a custom filter to the connection factory
	connFilter = conn =>
		var innerConn = ((IHasDbConnection)conn).DbConnection;
		return new ProfiledDbConnection((DbConnection)innerConn, MiniProfiler.Current);

container.RegisterPerWebRequest<IDbConnectionFactory>(() =>
	new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["Database"].ConnectionString, 
	                             MySqlDialect.Provider) { ConnectionFilter = connFilter });