gpt4 book ai didi

C# 抽象方法

转载 作者:行者123 更新时间:2023-12-02 12:57:32 24 4
gpt4 key购买 nike

我有一组方法,接受日期和 bool 值。然后,这些方法使用一些 SQL,然后将其传递给另一个方法并返回一个列表。除了 SQL 和返回的列表之外,每个方法都几乎相同。现在我知道有更好的方法来执行这些方法,但我不确定如何执行。

由于方法几乎相同,有人对我如何抽象它或使用更好的设计有任何想法吗?

这是代码:

private List<ENT_Message> GetMessageData(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_Message> ret = new List<ENT_Message>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_message";
}
else
{
sql = " Select * from tbl_message where created_Date between @start_Date and @end_date";
}

return Converter.SerializeToMessageList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

private List<ENT_SensorData> GetSensorData(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_SensorData> ret = new List<ENT_SensorData>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_sensor_data";
}
else
{
sql = "select * from tbl_sensor_data where Timestamp between @start_date and @end_Date";
}

return Converter.SerializeToSensorDataList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

private List<ENT_SensorDataEvent> GetSensorDataEvents(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_SensorDataEvent> ret = new List<ENT_SensorDataEvent>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_sensor_data_event";
}
else
{
sql = "select * from tbl_sensor_data_event where start_time between @start_date and @end_Date";
}

return Converter.SerializeToSensorEventDataList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

private List<ENT_SensorDataState> GetSensorDataState(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_SensorDataState> ret = new List<ENT_SensorDataState>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_sensor_data_state";
}
else
{
sql = "select * from tbl_sensor_data_state where start_time between @start_date and @end_Date";
}

return Converter.SerializeToSensorDateStateList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

private List<ENT_WorkOrder> GetWorkOrders(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_WorkOrder> ret = new List<ENT_WorkOrder>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_workorder";
}
else
{
sql = "select * from tbl_sensor_data_state where completed_date between @start_date and @end_Date";
}

return Converter.SerializeToWorkOrderList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

最佳答案

您可以大大缩短您的函数。例如,代替

private List<ENT_SensorDataState> GetSensorDataState(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
List<ENT_SensorDataState> ret = new List<ENT_SensorDataState>();
string sql = "";

if (IsSelectAll)
{
sql = "select * from tbl_sensor_data_state";
}
else
{
sql = "select * from tbl_sensor_data_state where start_time between @start_date and @end_Date";
}

return Converter.SerializeToSensorDateStateList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

你可以简单地写

private List<ENT_SensorDataState> GetSensorDataState(DateTime? startDate, DateTime? endDate, bool IsSelectAll)
{
string sql = "select * from tbl_sensor_data_state"
+ (IsSelectAll ? "" : "where start_time between @start_date and @end_Date");

return Converter.SerializeToSensorDateStateList(this.GetData(startDate, endDate, IsSelectAll, sql));
}

这基本上将您的函数简化为真正重要的内容:SQL 和序列化函数的名称。

关于C# 抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com