开发者

Using mvc-mini-profiler database profiling with Llblgen

开发者 https://www.devze.com 2023-03-13 00:11 出处:网络
I am trying to integrate mvc-mini-profiler to asp.net mvc application with Llblgen used for data access. I have tried to override llblgen\'s CommonDaoBase.CreateConnection:

I am trying to integrate mvc-mini-profiler to asp.net mvc application with Llblgen used for data access. I have tried to override llblgen's CommonDaoBase.CreateConnection:

public override DbConnection CreateConnection(string connectionString)
{
  return MvcMiniProfiler.Data.ProfiledDbConnection.Get(base.CreateConnection(connection开发者_如何学PythonString));
}

but this resulted in exception, saying 'Cannot cast to SqlConnection...'. Has someone got mvc-mini-profiler working with llblgen?


Miha Markic just posted the answer in his blog: http://blog.rthand.com/post/2011/07/24/Integrating-MvcMiniProfiler-and-LLBLGenPro.aspx


You need LLBLGen Pro v3. V2 does use dbproviderfactory for sqlserver but the generated code has some hardcoded casts still to SqlClient classes. v3 doesn't. I assume you use v2?

Additionally, a tiny change has to be made to the ProfiledDbProviderFactory.cs of the profiler:

/// <summary>
/// Extension mechanism for additional services;  
/// </summary>
/// <returns>requested service provider or null.</returns>
object IServiceProvider.GetService(Type serviceType)
{
    if(serviceType == typeof(ProfiledDbProviderFactory))
    {
        // For LLBLGen Pro v3 and up.
        return tail;
    }
    IServiceProvider tailProvider = tail as IServiceProvider;
    if (tailProvider == null) return null;
    var svc = tailProvider.GetService(serviceType);
    if (svc == null) return null;

#if ENTITY_FRAMEWORK
    if (serviceType == typeof(DbProviderServices))
    {
        svc = new ProfiledDbProviderServices((DbProviderServices)svc, profiler);
    }
#endif
    return svc;
}

EDIT. I am not sure whether this will work, as our framework uses the DbProviderFactory obtained from ADO.NET. That factory is the real Sqlclient's factory not the ProfiledDbProviderFactory. You can create a profiled connection here, but that's not going to work: the factory isn't obtained from the connection, the connection is created from the factory obtained from ADO.NET (DbProviderFactories.GetFactory()) and all other elements are created with that factory as well.

mvc profiler has to overwrite the datatable in DbProviderFactories through reflection with wrappers which wrap the actual factory types. This is kind of nasty but the only way to make things work with data-access code which does things by the book: get the factory, create elements through the factory.

it surprises me Linq to sql and Entity framework apparently work with the factory obtained from a connection object: how did one create that connection object in the first place?

0

精彩评论

暂无评论...
验证码 换一张
取 消