Search code examples
asp.netmvc-mini-profiler

Is it possible to use the mvc-mini-profiler with typed datasets?


I've been experimenting with using mvc-mini-profiler on a ASP.NET website. All of the request profiling works perfectly, but now I'm trying to see if there is a way to hook it into our database calls.

All of our database calls on our website are done by using typed datasets (.xsd files) with table and query table adapters. Is it possible to somehow hook in the mvc-mini-profiler to these datasets?

I understand that you would typically get some sort of dbconnection (SqlConnection, etc.) and then wrap that with the profiler's ProfiledDbConnection. I just don't know how to do this with a dataset, is it even possible?

Note: changing from using datasets to linq2sql or some other way would not really be feasible as this is a rather large project that has been around for some time.

Example database call

DAL_ClientTableAdapters.ClientTableAdapter tba = new DAL_ClientTableAdapters.ClientTableAdapter();
DAL_Client.ClientDataTable dt = tba.GetData();

Where there is a DAL_Client.xsd file containing a table adapter called Client which uses a connection string defined in the web.config


Solution

  • As Darin Dimitrov pointed out, you can't really do it. You can modify the code generator to set the TableAdapter's Connection property public, so you could pass in your own Connection. But SqlConnection is a sealed class, so you can't extend it by adding profiling hooks to it...

    Possible workarounds:

    1. You can get basic timing information by add profiling "steps" around your db calls. But you get no special highlighting in the UI, total SQL time, or details about the command(s) and parameter(s) of the query like you get for regular db calls. If these are important, I think you could extend the miniprofiler code to help with timings. I think Something like this:

      DAL_ClientTableAdapters.ClientTableAdapter tba = new DAL_ClientTableAdapters.ClientTableAdapter();
      DAL_Client.ClientDataTable dt = null;
      //if you are not familiar with miniprofiler Steps, this adds the elapsed time between the curly braces to your timing information
      using(MiniProfiler.StepStatic("Executing GetData() from 'Client' table")) {
          dt = tba.GetData();
      } 
      

      Here is what you would get: Basic timing information about your db call

    2. Try to change the code generator to add DbConnection into the generated files instead of SqlConnection. Don't know if the typed datasets, or your code uses anything SqlConnection specific, but sound pretty risky for an old and large project...

    3. Try to use Moles to insert profiling hooks to SqlConnection. OK, not exactly serious about this...