How can integrate MVCMiniProfiler with PetaPoco without changing the sources
Asked Answered
D

2

4

I'm trying to get MVCMiniProfiler to work with PetaPoco

I'm trying to set the connection in the creation of the PetaPoco DB, but run into problems (connectionClosed)

public class DbHelper
{
    static Database _CurrentDb = null;
    public static Database CurrentDb()
    {
        if (_CurrentDb == null)
        {
            string connstr = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
            var conn = ProfiledDbConnection.Get(new SqlConnection(connstr));
            _CurrentDb = new PetaPoco.Database(conn);
        }
        return _CurrentDb;
    }

}

I've read this item https://github.com/toptensoftware/PetaPoco/issues/44 but can get it to work

What is the right way to do it?

Edit

The solution was provided by Gareth Elms:

public class DbHelper
{
    static Database _CurrentDb = null;
    public static Database CurrentDb()
    {
        if (_CurrentDb == null)
        {
            _CurrentDb = new DatabaseWithMVCMiniProfiler("MainConnectionString");
        }
        return _CurrentDb;
    }

}
public class DatabaseWithMVCMiniProfiler : PetaPoco.Database
{
    public DatabaseWithMVCMiniProfiler(IDbConnection connection) : base(connection) { }
    public DatabaseWithMVCMiniProfiler(string connectionStringName) : base(connectionStringName) { }
    public DatabaseWithMVCMiniProfiler(string connectionString, string providerName) : base(connectionString, providerName) { }
    public DatabaseWithMVCMiniProfiler(string connectionString, DbProviderFactory dbProviderFactory) : base(connectionString, dbProviderFactory) { }

    public override IDbConnection OnConnectionOpened( IDbConnection connection)
    {
        // wrap the connection with a profiling connection that tracks timings 
        return MvcMiniProfiler.Data.ProfiledDbConnection.Get( connection as DbConnection, MiniProfiler.Current);
    }
}
Dyeline answered 19/8, 2011 at 19:5 Comment(2)
This should work. Is it not working?Currin
No, I guess PetaPoco close the conn and does not open it again. The must be the singletonDyeline
Y
3

I wonder if this is because it is a static class. It might be some weirdness around the connection being automatically closed after a request, and petapoco's _sharedConnectionDepth counter not knowing about it. I reproduced this easily using your code. Have a look at my sample petapoco app at github https://github.com/GarethElms/PetaPoco----A-simple-web-app all I do is instantiate Database in the base controller

Yongyoni answered 20/8, 2011 at 17:34 Comment(3)
I think the same, maybe I should return a new PetaPoco database in dev and the static in production (for perf reasons)Dyeline
I would be instantiating a PetaPoco class once per request.Currin
I mean...the Database class in PetaPoco. eg. Create a action filter that on action executing, it instantiates the Database class, begins a transaction, then on action result executed, you can commit the transaction or roll it back if an exception occurred.Currin
A
1

as Schotime mentioned, per request is the best for mvc applications. This is my impl using Ninject

private static void RegisterServices(IKernel kernel)
{


#if DEBUG
            kernel.Bind<IDatabase>().To<DebugDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "DebugCnnString");
#else
            kernel.Bind<IDatabase>().To<ReleaseDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "ReleaseCnnString");
#endif
}

public class DebugDatabase : PetaPoco.Database
{
    public DebugDatabase(string connectionStringName) : base(connectionStringName) { }

    public override IDbConnection OnConnectionOpened(IDbConnection connection)
    {
        // wrap the connection with a profiling connection
        return new ProfiledDbConnection(connection as DbConnection, MiniProfiler.Current);
    }
}

public class ReleaseDatabase : PetaPoco.Database
{
    public ReleaseDatabase(string connectionStringName) : base(connectionStringName)    {   }

    // ... some stuff
}
Affiliation answered 15/2, 2012 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.