ステートメントを使用してにリファクタリングコード
-
05-09-2019 - |
質問
私はそれらのすべては、いくつかのリターンリスト(SqlDataReader
を使用したので)、他の人が特定の値のみのストアドプロシージャを呼び出し、メソッドの多くとのdal層を持っています。
私は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
を使用することができ、私のヘルパー関数を失わないように、これをリファクタリングする方法はありますか?
解決
一つの方法は、の返すのコマンドにのコマンドを使用していますのデリゲートを取ってから、それを変更することです
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
});
その方法の処分は、作成と同じ場所にあり、呼び出し側はそれを心配する必要はありません。私はこのパターンのかなりのファンになってきている - 。それが今、私たちは、ラムダ式を持っていることを多くのクリーナーです。
他のヒント
あなたはこれを行うことができます:
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
}
あなたが巣の文を使用してすることができます:
using (SqlCommand cmd = CreateSprocCommand("..."))
{
using (var connection = cmd.Connection)
{
connection.Open();
using (var reader = cmd.ExecuteReader())
{
...
}
...
}
}
いかがます:
using (SqlCommand cmd = CreateSprocCommand("whatever"))
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
//blabla
}
}
これはあなたが何を意味するかですか?
using (SqlCommand cmd = CreateSprocCommand("SomeSprocName"))
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
//some code looping over the recors
}
}
所属していません StackOverflow