如何重用重新打开连接的代码?

我们的生产服务器会终止非活动连接,因此我们的API需要在需要时恢复它们。 以下代码有效,但它非常重复:

private const int MaxRetryCount = 3; public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command) { int retryCount = 0; while (retryCount++ < MaxRetryCount) { try { if (command.Connection.State == ConnectionState.Closed) command.Connection.Open(); return command.ExecuteReader(); } catch(Exception e) { if(!e.Message.ToLower().Contains("transport-level error has occurred")) { throw; } } } throw new Exception("Failed to restore connection for command:"+command.CommandText); } public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command) { var retryCount = 0; while(retryCount++ < MaxRetryCount) { try { if (command.Connection.State == ConnectionState.Closed) command.Connection.Open(); command.ExecuteNonQuery(); return; } catch(Exception e) { if (!e.Message.ToLower().Contains("transport-level error has occurred")) { throw; } } } throw new Exception("Failed to restore connection for command:" + command.CommandText); } 

我怎么能重构我的代码并消除重复? 我需要保留这些方法的签名,因为它们在整个系统中使用。

 private const int MaxRetryCount = 3; public static T RestoreConnectionAndExecute(SqlCommand command, Func func) { int retryCount = 0; while (retryCount++ < MaxRetryCount) { try { if (command.Connection.State == ConnectionState.Closed) command.Connection.Open(); return func(command); } catch(Exception e) { if(!e.Message.ToLower().Contains("transport-level error has occurred")) { throw; } } } throw new Exception("Failed to restore connection for command:"+command.CommandText); } public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command) { return RestoreConnectionAndExecute(command, c => c.ExecuteReader()); } public static int RestoreConnectionAndExecuteNonQuery(SqlCommand command) { return RestoreConnectionAndExecute(command, c => c.ExecuteNonQuery()); } 
 private const int MaxRetryCount = 3; public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command) { return RestoreConnectionAndExecuteQueryHelper(command, true); } public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command) { RestoreConnectionAndExecuteQueryHelper(command, false); } private static SqlDataReader RestoreConnectionAndExecuteQueryHelper(SqlCommand command, bool returnReader) { var retryCount = 0; while (retryCount++ < MaxRetryCount) { try { if (command.Connection.State == ConnectionState.Closed) command.Connection.Open(); if (returnReader) { return command.ExecuteReader(); } else { command.ExecuteNonQuery(); return null; } } catch (Exception e) { if (!e.Message.ToLower().Contains("transport-level error has occurred")) { throw; } } } throw new Exception("Failed to restore connection for command:" + command.CommandText); } 

这些方法的共同部分是连接检索过程。 您可以创建一个新的静态方法,该方法负责调用例如以命令作为参数的检索连接,如果在其他情况下建立连接则返回它会引发exception。

在代码示例中有一些我不喜欢的东西。 但是,要明确回答有关删除重复的问题 – 将公共代码提取到接受委托的方法中。

 private TReturn RestoreConnectionAndExecute(SqlCommand command, Func execute) { int retryCount = 0; while (retryCount++ < MaxRetryCount) { try { if (command.Connection.State == ConnectionState.Close) command.Connection.Open(); return execute(command); } catch(Exception e) { ... } } public SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command) { return this.RestoreConnectionAndExecute(command, c => c.ExecuteReader()); } public void RestoreConnectionAndExecuteNonQuery(SqlCommand command) { // Ignore return this.RestoreConnectionAndExecute(command, c => c.ExecuteNonQuery()); } 

不过,你应该重新思考一些事情。 包含:

  • 捕获特定的例外情况
  • 使用Exception.Number或ErrorCode而不是消息(将在本地化版本中更改,并且可能在更新的FX版本中更改)
  • 使用IDisposable资源的语句
  • 抛出特定的例外情况