رمز Refactor في استخدام العبارة
-
05-09-2019 - |
سؤال
لدي طبقة 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
?
المحلول
طريقة واحدة هي تغييرها من عودة أمر ل مع الأخذ مندوب يستخدم الأمر:
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
nullable بدلا من ذلك. فقط مرر null
ل "لا قيمة العودة".)
كنت تستخدم هذا مع تعبير Lambda (أو طريقة مجهولة):
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
}
}