2

私は多くのメソッドを備えたdal層を持っており、それらはすべてストアドプロシージャを呼び出し、一部のリターンリスト(を使用してSqlDataReader)を呼び出し、その他は特定の値のみを呼び出します。

を作成するヘルパーメソッドがありますSqlCommand

    protected SqlCommand CreateSprocCommand(string name, bool includeReturn, SqlDbType returnType)
    {
        SqlConnection con = new SqlConnection(this.ConnectionString);
        SqlCommand com = new SqlCommand(name, con);
        com.CommandType = System.Data.CommandType.StoredProcedure;

        if (includeReturn)
            com.Parameters.Add("ReturnValue", returnType).Direction = ParameterDirection.ReturnValue;

        return com;
    }

これで、私の平均的な(過度に単純化された)メソッド本体は次のようになります。

SqlCommand cmd = CreateSprocCommand("SomeSprocName"); //an override of the above mentioned method
try {
   cmd.Connection.Open();
   using (var reader = cmd.ExecuteReader()) {
       //some code looping over the recors
   }
   //some more code to return whatever needs to be returned
}
finally {
   cmd.Connection.Dispose();
}

これをリファクタリングして、ヘルパー関数を失わないようにする方法はありますか(それ以外の場合はかなりの反復作業を行います)、それでも使用できますusingか?

4

5 に答える 5

13

1つの方法は、コマンドを返すことから、コマンド使用するデリゲートを取得することへと変更することです

protected void ExecuteSproc(string name,
                            SqlDbType? returnType,
                            Action<SqlCommand> action)
{
    using (SqlConnection con = new SqlConnection(this.ConnectionString))
    using (SqlCommand com = new SqlCommand(name, con))
    {
        con.Open();
        com.CommandType = System.Data.CommandType.StoredProcedure;

        if (returnType != null)
        {
            com.Parameters.Add("ReturnValue", returnType.Value).Direction = 
                ParameterDirection.ReturnValue;
        }
        action(com);
    }
}

includeReturn(パラメーターも削除し、returnType代わりにnull許容にしたことに注意してください。 null「戻り値なし」を渡すだけです。)

これはラムダ式(または匿名メソッド)で使用します:

ExecuteSproc("SomeName", SqlDbType.DateTime, cmd =>
{
    // Do what you want with the command (cmd) here
});

そうすれば、処分は創造物と同じ場所にあり、発信者はそれについて心配する必要はありません。私はこのパターンのかなりのファンになりつつあります-ラムダ式を使用できるようになったので、かなりクリーンになりました。

于 2009-05-26T08:29:25.903 に答える
3

あなたはこれを行うことができます:

protected static SqlCommand CreateSprocCommand(SqlConnection con, string name, bool includeReturn, SqlDbType returnType)
{
    SqlCommand com = new SqlCommand(name, con);
    com.CommandType = System.Data.CommandType.StoredProcedure;

    if (includeReturn)
        com.Parameters.Add("ReturnValue", returnType).Direction = ParameterDirection.ReturnValue;

    return com;
}

そしてそれをこのように呼びます:

using (SqlConnection con = new SqlConnection(this.ConnectionString))
using (SqlCommand cmd = CreateSprocCommand(con, "SomeSprocName", true, SqlDbType.Int)
{
    cmd.Connection.Open();
    using (var reader = cmd.ExecuteReader())
    {
        //some code looping over the recors
    }
    //some more code to return whatever needs to be returned
}
于 2009-05-26T08:35:17.717 に答える
1

ステートメントを使用してネストできます。

using (SqlCommand cmd = CreateSprocCommand("..."))
{
  using (var connection = cmd.Connection)
  {
     connection.Open();
     using (var reader = cmd.ExecuteReader())
     {
         ...
     }
     ...
  }
}
于 2009-05-26T08:27:02.743 に答える
0

どうですか:

using (SqlCommand cmd = CreateSprocCommand("whatever"))
{
  cmd.Connection.Open();
  using (var reader = cmd.ExecuteReader())
  {
   //blabla
  }
}
于 2009-05-26T08:25:02.933 に答える
-1

これはあなたが意味することですか?

using (SqlCommand cmd = CreateSprocCommand("SomeSprocName"))
{
    cmd.Connection.Open();
    using (var reader = cmd.ExecuteReader()) 
    {
        //some code looping over the recors
    }
}
于 2009-05-26T08:27:04.817 に答える