我在ASP.NET站点中有三个类,BookingItem,BookingHistoryItem和BookingManager。
bookingManager用于在数据库中查找“预订更改”事务列表。发生这种情况是因为我想查找“今天”发生的任何预订(由BookingItem表示)的所有更改
BookingItem有一个列表属性,代表所有以前的预订状态。例如:预订原定于1月4日,但已移至1月1日,名称由Alistair Smith改为Smythe。预订项目代表当前实际有效预订的内容(因为它也是历史记录表中最新创建的项目),而历史记录应包含针对Alistair Smith的第4个预订项目以及任何其他先前版本。
我正在迭代搜索的结果(非异步),并希望异步查找预订历史记录项,创建列表,并将其添加到该预订,但我想我误解了BeginExecuteReader如何回调作品。我想我需要对我的逻辑提供一些帮助,并了解如何以及为什么它会更好。
示例:
public class BookingItem
{
public int BookingID{ get; set; }
public int ClientID { get; set; }
public DateTime bookingTime{ get; set; }
public List<BookingHistoryItem> BookingHistory {get; set;}
public BookingItem()
{
}
}
public class BookingHistoryItem
{
public int TransactionID{ get; set; }
public DateTime BookingDate{ get; set; }
public int BookingID{ get; set; }
public int ClientID { get; set; }
public DateTime bookingTime{ get; set; }
public List<BookingHistoryItem> BookingHistory {get; set;}
public BookingHistoryItem()
{
}
}
public class BookingHistoryItem
{
public int TransactionID{ get; set; }
public DateTime BookingDate{ get; set; }
public int BookingID{ get; set; }
public int ClientID { get; set; }
public DateTime bookingTime{ get; set; }
public List<BookingHistoryItem> BookingHistory {get; set;}
public BookingItem()
{
}
}
public class BookingManager
{
public DateTime StartRange { get; set; }
public DateTime EndRange { get; set; }
string strSQL= @"Select * from [Bookings] Where BookingDate Between @Start and @End";
using (SqlCommand cmd = new SqlCommand(strCFRSQL, conSQL))
{
//start and end parameters are the selected range from the UI.
cmd.Parameters.Add("@Start", SqlDbType.Date).Value = StartRange.ToShortDateString();
cmd.Parameters.Add("@End", SqlDbType.DateTime).Value = EndRange.ToShortDateString();
//initializes conSQL
ConnectToDB();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
/*Here I sort of Asynchronously look it up using BeginReader
and a callback but I don't know how to add the History info to the
Booking Item as when it comes back to the actual thread, I have no
reference to this specific booking Item. I want to go through all
Changes, build the booking, add the history, and THEN I will go on to
apply my rules. */
}
}
}
}
这是我所处位置的粗略近似值。查找历史记录的功能如下,使用回调方法: public void GetHistory(BookingItem curBook,bool useAsync) {
ConnectToDB();
System.Data.SqlClient.SqlCommand sqcBookingHistory = new System.Data.SqlClient.SqlCommand("Select * from [BookingHistory] Where BookingID=@BookingID, conSQL);
sqcBookingHistory .CommandTimeout = 320;
sqcBookingHistory .CommandType = CommandType.Text;
sqcBookingHistory .Parameters.Add("@BookingID", SqlDbType.Int).Value = curBook.BookingID;
try
{
IAsyncResult result = sqcBookingHistory .BeginExecuteReader(MyEndHistoryCallback, sqcBookingHistory);
}
catch(Exception ex)
{
// ...
// handle any execution initiation errors here
}
}
public void MyEndHistoryCallback(IAsyncResult result)
{
try
{
// obtain the results from the database instance in the AsyncState
System.Data.SqlClient.SqlCommand sqcBookingHistory = (System.Data.SqlClient.SqlCommand)result.AsyncState;
using (IDataReader dr = sqcBookingHistory .EndExecuteReader(result))
{
while (dr.Read())
{
//build BookingHistory Item here
//now how I put but it back into the bookingItem that originally
//INitiated it? curBook is a new item.
}
}
}
catch(Exception ex)
{
// ...
// handle any execution completion errors here
}
}
我通常很聪明,但我觉得这很愚蠢。我承认这一点,并想学习如何以“最佳实践”的方式做到这一点。
底线:检索一组预订交易项目,构建预订项目(因为更改项目代表最新预订)想要遍历它们并为每个预订构建bookinghistoryitem,并将其作为属性添加到原始bookingItem 。异步似乎是最快的方式,所以多个预订同时检索他们的历史,而不是预订&gt;我的历史记录,预订项目&gt;什么是我的犹豫?所以从预订我可以说,去获取历史并将其添加到我,所以该程序可以进入下一个。