4.
Deleting entities
In listing 4 we included some event definitions in the
markup to handle deletes. It’s now time to implement those event
handlers so that when you click the Delete button in the grid, it will
delete the corresponding shirt from the Products table in the Table
service.
In the listing that
follows you’ll see the code-behind that relates to the Delete button.
Listing 6. Code-behind to delete shirts
The code in listing 6 shows how you can delete a shirt from the Products table
when the Delete button is clicked in the grid. The DeleteShirt method is very similar to the code in listing 3 that you used to add a shirt. You instantiate the
shirt context , add
the shirt to be deleted to the context object’s tracking list , and
submit the changes to the Table service. Finally, you rebind the grid
to display the updated list.
Finding the Entity to
Delete
There are a couple of
differences between adding and deleting a shirt. When adding a shirt to
the product list, you can simply create a new Product object and add that to the tracking list. If you’re
deleting an object, you first need to get a local copy of the object
that you wish to delete (as you did at in listing 6) and then add that object to the tracking list (as shown at ).
For the sake of simplicity,
you can pass the object to be deleted by refetching the object from the
Table service. In production code, you should never refetch an object
for deletion because this performs an unnecessary call to the Table
service. In this example, however, you can take the ProductId (row key) that was passed as the Delete button’s
command argument, and then perform a LINQ query to retrieve that entity
from the Table service.
The following code shows the
LINQ query we used to retrieve the individual shirt:
from item in shirtContext.Product
where item.PartitionKey=="Shirts" && item.RowKey==rowKey
select item
Looking at the LINQ query,
you can see that we’re using the IQueryable Product from the shirtContext object as the data source. Because this query
is IQueryable, you can modify the
query before it’s sent to the server to restrict the result set to only
return those entities that reside in the Shirts partition whose row key
matches the passed-in ProductId.
Because this query is manipulated before it’s sent to the server, all
the filtering is performed by the Table service rather than by the
client application.
You’ll now be able to add,
list, and delete shirts in the Products table.
5. Updating
entities
Finally, you need to update the code-behind to
allow editing the grid and saving any changes back to the Products
table. The following listing contains the code you need to edit the
grid.
Listing 7. Updating entities
After editing the grid with
your new values, you need to store the modified data back to the
Products table. Listing 7 will be called whenever there’s a
change to any data in the grid. The GridView’s RowUpdating event is where you’ll perform that update. The
process of updating data is very similar to deleting an entity, as shown
earlier.
You retrieve the row that has
been edited and extract the product ID displayed in the second cell of
the row. As before, you instantiate the product context , and at you
retrieve a copy of the edited entity from the Table service using a LINQ
query, passing the partition key and the row key. At you
replace the current name and description of the shirt with the modified
data extracted from the text boxes of the row being edited. You then add
the modified entity to the shirtContext’s tracking list via
the UpdateObject method, commit the
changes back to the Table service using the SaveChanges method, take the grid out of edit mode, and rebind
the grid to a fresh copy of the data returned from the Table service.