我目前有两种方法:结合类似的方法
CalculateDaily()
{
List<string> tempList;
// Effective query. not what is really passed
tempList = "SELECT timestamp FROM table1 WHERE date = today";
var total = tempList.Sum();
}
和:
CalculateTotal()
{
List<string> tempList;
// Effective query. not what is really passed
tempList = "SELECT timestamp FROM table1"
var total = tempList.Sum();
}
我的问题是我应该让他们分开,或者将它们组合成一个单一的方法可行,运行if
检查?喜欢的东西:
Calculate(bool daily)
{
List<string> tempList;
if(daily)
tempList = "SELECT timestamp FROM table1 WHERE date = today";
else
tempList = "SELECT timestamp FROM table1";
var total = tempList.Sum();
}
首选的做法是分解和减少耦合,而不是增加复杂性。一种功能,一种责任。但更好的是下面的建议,以结合常见的然后是子类。 – dkretz