私は多くのメソッドを備えた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
か?