如何将 mvc-mini-profiler 连接从 MVC3 传递到基类

发布于 2024-11-28 19:11:23 字数 793 浏览 2 评论 0原文

给定这段代码,

public abstract class Foo
{
    private static SqlConnection _sqlConnection;

    protected SqlConnection GetOpenConnection()
    {
        if (_sqlConnection == null)
        {
            _sqlConnection = new SqlConnection("connection string");
        }
        return _sqlConnection;
    }

    protected abstract void Execute();
}

public class FooImpl : Foo
{

    protected override void Execute()
    {
        var myConn = GetOpenConnection();
        var dog = myConn.Query<dynamic>("select 'dog' Animal");
        var first = dog.First();

        string animalType = first.Animal;
        // more stuff here
    }
}

如果您无权访问连接创建过程,如何将连接包装在配置文件连接中?重写超类中的代码并将其包装在那里?这将涉及更改从基类继承的数百个类。我更喜欢一种改变基类的方法,而对超级类所需的改变尽可能少。

谢谢你, 斯蒂芬

Given this snippet of code

public abstract class Foo
{
    private static SqlConnection _sqlConnection;

    protected SqlConnection GetOpenConnection()
    {
        if (_sqlConnection == null)
        {
            _sqlConnection = new SqlConnection("connection string");
        }
        return _sqlConnection;
    }

    protected abstract void Execute();
}

public class FooImpl : Foo
{

    protected override void Execute()
    {
        var myConn = GetOpenConnection();
        var dog = myConn.Query<dynamic>("select 'dog' Animal");
        var first = dog.First();

        string animalType = first.Animal;
        // more stuff here
    }
}

How would you wrap the connection in a profiled connection if you don't have access to the connection creation process? Rewrite the code in the super class and wrap it there? This would involve changing hundreds of classes that inherit from the base. I'd prefer a way to change the base class, with as little changes necessary to the supers.

Thank you,
Stephen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-12-05 19:11:24

经过一番尝试和错误后,我妥协了,在基础库中添加了对 MvcMiniProfiler 的引用,并稍微更改了连接代码。

    protected DbConnection GetOpenConnection()
    {
        if (_connection == null)
        {
            _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection string "].ConnectionString);
            _connection.Open();
        }
        return MvcMiniProfiler.Data.ProfiledDbConnection.Get(_connection, MiniProfiler.Current); 
    }

    private static SqlConnection _connection;

这适用于 MVC 项目中的托管(出于分析目的,我们没有该功能(QA/产品数据库))和 WPF/Windows 服务

Well after a bit of trial and error I compromised and added a ref to MvcMiniProfiler in the base library and changed the connection code a bit.

    protected DbConnection GetOpenConnection()
    {
        if (_connection == null)
        {
            _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection string "].ConnectionString);
            _connection.Open();
        }
        return MvcMiniProfiler.Data.ProfiledDbConnection.Get(_connection, MiniProfiler.Current); 
    }

    private static SqlConnection _connection;

This works for both hosting in the MVC project (for profiling purposes, where we don't have that capability (QA/Prod Databases)) and WPF/Windows Service

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文