嗨我有两种方法,还有三种方法(在这个问题中没有提及)..
我怎样才能重构这些......
方法1 :
public DataTable GetVisits(System.DateTime startdate , System.DateTime enddate)
{
const string sql = @"SELECT CONCAT(UPPER(SUBSTRING(visit_Status, 1, 1)), SUBSTRING(visit_Status FROM 2)) as Status, COUNT('x') AS Visits
FROM visits
WHERE visit_Date BETWEEN @startdate AND @enddate
GROUP BY visit_Status";
var tblvisits = new DataTable();
using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
{
conn.Open();
var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
var ds = new DataSet();
var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter.Direction = ParameterDirection.Input;
parameter.Value = startdate.ToString(dateformat);
cmd.Parameters.Add(parameter);
var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = enddate.ToString(dateformat);
cmd.Parameters.Add(parameter2);
var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
da.Fill(ds);
try
{
tblvisits = ds.Tables[0];
}
catch
{
tblvisits = null;
}
}
return tblvisits;
}
方法2::
public DataTable sales(System.DateTime startdate, System.DateTime enddate)
{
const string sql = @"SELECT memberAccTran_Source as Category, sum(memberAccTran_Value) as Value
FROM memberacctrans
WHERE memberAccTran_DateTime BETWEEN @startdate AND @enddate
GROUP BY memberAccTran_Source";
var memacctvalues = new DataTable();
using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
{
conn.Open();
var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
var ds = new DataSet();
var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter.Direction = ParameterDirection.Input;
parameter.Value = startdate.ToString(dateformat);
cmd.Parameters.Add(parameter);
var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = enddate.ToString(dateformat);
cmd.Parameters.Add(parameter2);
var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
da.Fill(ds);
try
{
memacctvalues = ds.Tables[0];
}
catch
{
memacctvalues = null;
}
}
return memacctvalues;
}
任何人都知道如何重构这两种方法
(
我有三个更像这样的方法,但唯一不同的是sql查询 )
答案 0 :(得分:6)
如果方法的所有风格之间的唯一区别是SQL代码,那么为每个方法创建一个公共方法,并让所有方法调用具有公共代码的私有方法,并传入正确的sql字符串。
private DataTable PerformQuery(string sql, System.DateTime startdate, System.DateTime enddate)
{
var tblvisits = new DataTable();
using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
{
conn.Open();
var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
var ds = new DataSet();
var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter.Direction = ParameterDirection.Input;
parameter.Value = startdate.ToString(dateformat);
cmd.Parameters.Add(parameter);
var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = enddate.ToString(dateformat);
cmd.Parameters.Add(parameter2);
var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
da.Fill(ds);
try
{
tblvisits = ds.Tables[0];
}
catch
{
tblvisits = null;
}
}
return tblvisits;
}
然后是公众的:
public DataTable Sales(System.DateTime startdate, System.DateTime enddate)
{
const string sql = @"SELECT memberAccTran_Source as Category, sum(memberAccTran_Value) as Value
FROM memberacctrans
WHERE memberAccTran_DateTime BETWEEN @startdate AND @enddate
GROUP BY memberAccTran_Source";
return PerformQuery(sql, startdate, enddate);
}
public DataTable GetVisits(System.DateTime startdate , System.DateTime enddate)
{
const string sql = @"SELECT CONCAT(UPPER(SUBSTRING(visit_Status, 1, 1)), SUBSTRING(visit_Status FROM 2)) as Status, COUNT('x') AS Visits
FROM visits
WHERE visit_Date BETWEEN @startdate AND @enddate
GROUP BY visit_Status";
return PerformQuery(sql, startdate, enddate);
}
并重复其他每一个。正如您似乎指出的那样,这假设所有参数都是相同的,唯一的区别是正在运行的SQL。
答案 1 :(得分:1)
只需将SQL查询放在方法之外并将其作为参数传递。