使用MiniProfiler';使用NHibernate进行数据库评测

使用MiniProfiler';使用NHibernate进行数据库评测,nhibernate,mvc-mini-profiler,Nhibernate,Mvc Mini Profiler,使用NHibernate的数据库评测最简单的方法是什么?为了让探查器工作,我需要包装NHibernate在ProfiledDbConnection中使用的DbConnection 我不太熟悉NHibernate的内部结构,所以我不知道所有的扩展点在哪里。(我注意到NHibernateISession有一个Connection属性,但它是只读的。)尝试实现NHibernate.Connection.IConnectionProvider(您可以继承DriverConnectionProvider)

使用NHibernate的数据库评测最简单的方法是什么?为了让探查器工作,我需要包装NHibernate在
ProfiledDbConnection
中使用的
DbConnection


我不太熟悉NHibernate的内部结构,所以我不知道所有的扩展点在哪里。(我注意到NHibernate
ISession
有一个
Connection
属性,但它是只读的。)

尝试实现
NHibernate.Connection.IConnectionProvider
(您可以继承
DriverConnectionProvider
),在
GetConnection()
中根据需要包装
IDbConnection


使用
环境插入连接提供程序。ConnectionProvider
在配置属性中键入。有关使用RealProxy代理SqlCommand的版本,请参阅以下链接-现在支持批处理

  • 博客
  • 要点
我没有改变原来的答案,因为它被接受了。[/更新]

我已经通过实现一个配置文件的客户端驱动程序(下面是Sql Server 2008的示例)部分实现了这一点——这适用于简单的示例,但是我还没有找到NH批处理的解决方案(尝试将命令转换回SqlCommand)


我将上面的Roberts答案扩展到NHibernate配料。这里有很多代码,因此可能会被缩短,其中一些代码基于客户端驱动程序的nHibernate源代码

YoureOnTime.Data.ProfiledSqlClientDriver,YoureOnTime.Common
公共类配置文件DSQLClientDriver:DriverBase,IEmbeddedBatcherFactoryProvider
{
公共覆盖IDbConnection CreateConnection()
{
返回新的ProfiledSqlDbConnection(
新建SqlConnection(),
微型轮廓仪(当前);
}
公共重写IDbCommand CreateCommand()
{
返回新的ProfiledSqlDbCommand(
新建SqlCommand(),
无效的
微型轮廓仪(当前);
}
公共重写bool UseNamedPrefixInSql
{
获取{return true;}
}
公共覆盖布尔UseNamedPrefixin参数
{
获取{return true;}
}
公共重写字符串NamedPrefix
{
获取{return“@”;}
}
公共覆盖布尔支持MultipleOpenReaders
{
获取{return false;}
}
公共静态void SetParameterSizes(IDataParameterCollection参数,SqlType[]参数类型)
{
对于(int i=0;imssql2000dial.maxsizeforlengthLimitedString&&
(DbType.String==dbParam.DbType | | DbType.StringFixedLength==dbParam.DbType));
}
私有静态void SetVariableLengthParameterSize(IDbDataParameter dbParam,SqlType SqlType)
{
SetDefaultParameterSize(dbParam,sqlType);
//使用SqlType中的数据覆盖默认值。
if(sqlType.LengthDefined&&!IsText(dbParam,sqlType))
{
dbParam.Size=sqlType.Length;
}
if(sqlType.PrecisionDefined)
{
dbParam.Precision=sqlType.Precision;
dbParam.Scale=sqlType.Scale;
}
}
公共重写IDbCommand GenerateCommand(CommandType、SqlString、SqlType[]参数类型)
{
IDbCommand=base.GenerateCommand(类型、sqlString、参数类型);
//如果(IsPrepareSqlEnabled)
{
SetParameterSizes(command.Parameters、parameterTypes);
}
返回命令;
}
公共覆盖布尔支持多功能查询
{
获取{return true;}
}
#区域IEMBedBatcherFactoryProvider成员
System.Type IEMBededBatcherFactoryProvider.BatcherFactoryClass
{
获取{return typeof(ProfiledSqlClientBatchingBatcherFactory);}
}
#端区
}
公共类配置文件DSQLClientBatchingBatcher:AbstractBatcher
{
私有整数批量大小;
受影响的私人总收入;
私有SqlClientSqlCo
public class ProfiledSql2008ClientDriver : Sql2008ClientDriver
{
    public override IDbCommand CreateCommand()
    {
        return new ProfiledDbCommand(
            base.CreateCommand() as DbCommand, 
            null,
            MiniProfiler.Current);
    }

    public override IDbConnection CreateConnection()
    {
        return ProfiledDbConnection.Get(
            base.CreateConnection() as DbConnection, 
            MiniProfiler.Current);
    }
}