NPoco and MiniProfiler

Now that you have NPoco installed, being able to see exactly what queries were run and how they performed can make a huge difference in the overall performance of your site.

MiniProfiler is an awesome project by Sam Saffron that can easily profile your site and SQL queries. It is very easy to install and there is heaps of documentation at its home page.

To install MiniProfiler type Install-Package MiniProfiler in the Package Manager Console.

So how do you wire it up to NPoco? That’s also very easy.

public class TheDatabase : Database
    public TheDatabase(string connectionStringName) 
        : base(connectionStringName) {}

    public override IDbConnection OnConnectionOpened(IDbConnection conn)
        return new ProfiledDbConnection((DbConnection) conn, MiniProfiler.Current);

Create a class that inherits from Database and override the OnConnectionOpened returning a new ProfiledDbConnection. Now instantiate your new class instead of Database when running queries.

As long as you have the @MiniProfiler.RenderIncludes() in your layout and MiniProfiler is started each request as per the installation instructions then your queries should be displayed in the top corner.

Let me know how you get on, and thank Sam for his awesome project. 

This entry was posted in .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s