Hilton Giesenow's Jumbled Mind

the madness that is...

News

This is my little spot in cyberspace where you will find a collection of random (but mostly software-related) thoughts and ideas that are frightening in their shining brilliance (or something like that ;->).
 
Please enjoy your stay and feel free to Contact Me.
 
Microsoft MVP

.Net Links

BlogRoll

Misc. Links

Syndication

April 2008 - Posts

Visual Studio Extensibility (VSX) Video - How Do I: Add Commands and Controls Into the Visual Studio IDE?

This week's VSX video is on adding commands and controls into the Visual Studio environment. It's a modified version of the VS 2005 version of the video that was up on the site before, more tailored around Visual Studio 2008, including using VSCT files for the command placement. Here's a screenshot:

Visual Studio Extensibility: Add Commands using VSCT file

Spotted in the Wild: Be Careful With Loading Your Data!

I've blogged before about how LINQ To SQL actually requires the same careful design and development practices as other OR/M frameworks (see for instance my post on LINQ To SQL performance). Part of this involves being aware of when and how calls to the database are executed, and with this is mind I found an interesting sample in the DinnerNow.net 3.5 reference sample application:

public bool UpdateOrderStatus(DinnerNow.Business.Data.RestaurantOrder
  restaurantOrder, stringstatus, Guid WorkflowId)
{
    var orderItems = fromod indb.OrderDetails
                     whereod.RestaurantId == restaurantOrder.RestaurantId
                     && od.OrderId == restaurantOrder.OrderId
                     select new
                   
{
                         OrderDetailId = od.OrderDetailId
                     };

    foreach (var orderItemId inorderItems)
    {
        var orderItem = db.OrderDetails.Single(oi => oi.OrderDetailId ==
  orderItemId.OrderDetailId);
        orderItem.WorkflowId = WorkflowId;
        orderItem.Status = status;
        orderItem.StatusUpdatedTime = DateTime.Now;
    }
    db.SubmitChanges();

    return true;
}

If I read this correctly, the code is building the list of id's into the orderItems list (1 database call), then it retrieves each order item one by one from the database (x number of calls, where x is the number of items in the list) using the Single() method (to find out more about this and similar methods, check out my post on Linq Tips: Retrieving an Element From a Collection). Now, the updates are batched, as far as I can tell, so that's ok. But, with 10 items on the order, this will mean 11 database calls for the reads! Perhaps a better approach might be:

public bool UpdateOrderStatus(DinnerNow.Business.Data.RestaurantOrder restaurantOrder, 
string status, Guid WorkflowId) { var orderItems = from od in db.OrderDetails where od.RestaurantId == restaurantOrder.RestaurantId && od.OrderId == restaurantOrder.OrderId select od; foreach (var orderItem in orderItems) { orderItem.WorkflowId = WorkflowId; orderItem.Status = status; orderItem.StatusUpdatedTime = DateTime.Now; } db.SubmitChanges(); return true; }

This time, there is one call for the reads (which executes for the first iteration of the foreach loop), then the items are updated and batched, as before. Another even higher level approach would be to have the orderdetails already in memory and loaded with the RestaurantOrder instance, perhaps using the LoadWith DataLoadOption...

As for the "return true", I'm not sure I see the value of it, as there's no "return false" if something goes wrong...Perhaps the same should at least have a look at the ChangeConflicts property on the DataContext.

Posted: Apr 03 2008, 09:41 PM by hiltong | with no comments
Filed under: ,