鉴于这段代码
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
}
}
如果您无权访问连接创建过程,您将如何将连接包装在已分析的连接中?重写超类中的代码并将其包装在那里?这将涉及更改数百个从基类继承的类。我更喜欢一种更改基类的方法,对超级类进行尽可能少的更改。
谢谢你,斯蒂芬