How can integrate MVCMiniProfiler with PetaPoco without changing the sources

自作多情 提交于 2019-12-21 18:13:06

问题


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);
    }
}

回答1:


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




回答2:


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
}


来源:https://stackoverflow.com/questions/7126205/how-can-integrate-mvcminiprofiler-with-petapoco-without-changing-the-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!