Hi all,
I have a question around performance of collections/lists:
Take the following code example:
Code:
void add(OrderItem item) {
if (!_order.OrderItems.contains(item))
_order.OrderItems.add(item);
}
This seemingly innocuous piece of code would:
1) Fetch all of the OrderItems from the database
2) Perform an in memory comparision
3) Add the new item to the collection
If I were to do the same using a data-oriented approach, I'd use the following (pseudo code for brevity):
Code:
SqlCommand _cmd = _sqlConnection.CreateCommand();
_cmd.CommandText = "SELECT COUNT(*) FROM OrderItem WHERE OrderItemID = " + item.ID.ToString();
int _count = (int)_cmd.ExecuteScalar();
if (_count == 0)
// Insert the record into the table
The data oriented approach is much better for performance - one db hit to do a count, and then one to do the insert. I'm wondering how to get this kind of behaviour using the POCO approach.
I'm guessing that I need an 'intelligent' collection, which only loads all of it's elements in specific cases. For example, doing a contains() could hit the db directly, whereas an enumeration would need all elements.
Any thoughts or ideas on this matter?
TIA