gpt4 book ai didi

asp.net-mvc-3 - 如何将 mvc-mini-profiler 连接从 MVC3 传递给基类

转载 作者:行者123 更新时间:2023-12-04 06:17:57 26 4
gpt4 key购买 nike

鉴于这段代码

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

如果您无权访问连接创建过程,您将如何将连接包装在配置文件连接中?重写父类(super class)中的代码并包装在那里?这将涉及更改从基类继承的数百个类。我更喜欢一种更改基类的方法,对 super 类进行必要的更改。

谢谢,
斯蒂芬

最佳答案

经过一番反复试验,我妥协了,并在基础库中添加了对 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/Prod 数据库))和 WPF/Windows 服务

关于asp.net-mvc-3 - 如何将 mvc-mini-profiler 连接从 MVC3 传递给基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com