Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

Modifying entities with the REST API is CRUD (part 2) - Deleting entities

3/16/2011 10:10:21 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2. Deleting entities

You could delete shirts from the product list using the following call:

var shirtContext = new ProductContext();
shirtContext.DeleteObject(shirtToDelete);
shirtContext.SaveChanges();

Deleting an entity is similar to adding an entity when using the StorageClient library. If you wished to delete a shirt from the Products table, you’d need to add the shirt to be deleted to the context object’s (shirtContext) tracking list using the DeleteObject method. All changes are again tracked locally by the context object and are only saved back to the Table service when the SaveChanges method is called (following the Unit of Work pattern).

Let’s now take a look at using the REST API for deleting entities from a table. To delete an entity you need to make a DELETE request to the appropriate URI, passing in the correct table name, partition key, and row key:

http://silverlightukstorage.table.core.windows.net/Products(PartitionKey="Shirts", RowKey="RedShirt")

The preceding URI would delete an entity called RedShirt from the Shirts partition of the Products table from a storage account named silverlightukstorage. See the following listing for the code required to the delete the entity from a console application.
Listing 1. Deleting entities using the REST API
Optimizing Delete Performance in Web Grids

As explained previously, to delete an entity using the StorageClient library, you first need to add a local copy of the entity to your context object’s tracking list. In Windows client applications, this isn’t such a big issue, as you’ll already have a local copy of the entities. In an ASP.NET application, if you listed the entities in a grid (as in the product-management web page), you’d no longer have a local copy of the entity because each web page call is stateless.

Although you could store a copy of all entities in the ASP.NET page state, this would massively increase the page size and reduce overall performance. Similarly, storing the entities in the session would put unnecessary overhead on the web server, again affecting the performance. Even if the grid were populated from a cache, unnecessary calls to the cache would still have a slight impact on performance. So let’s look at some other options.

We fetched the entity from the Table service (for the sake of simplicity) and then added that entity to the shirtContext’s tracking list. The following code shows how we used a LINQ query to fetch the data from the Table service:

var entity = new ProductContext();

shirtToDelete = (from item in shirtContext.Products
where item.PartitionKey == "Shirts"
&& item.RowKey == rowkey
select item).First();

shirtContext.DeleteObject(entity);

shirtContext.SaveChanges();

Refetching the entity to be deleted isn’t something you should consider in a production environment, as any unnecessary calls to the Table service will impact the performance of your application and add to the overall running cost of your service (calls to the Table service are billable).

As you saw in our discussion of the REST API, you really don’t need all the data of the object. In fact, you only need the partition key and row key, so rather than fetching the whole object, you could construct a lightweight version of the object to be deleted.

To do this, you can define a lightweight sister class of the Product class that only contains the PartitionKey and RowKey values. As long as the object held in the tracking list holds the correct PartitionKey and RowKey (which uniquely identify an entity), you’ll have enough information to perform the delete—there’s no need to fetch every property of the entity.

The following code shows how you can delete an entity from the Table service using a lightweight instance:

var shirtContext = new ProductContext();
shirtContext.DeleteObject
(
new ProductKey
{
PartitionKey = "Shirts",
RowKey = e.CommandArgument.ToString()
}
);
shirtContext.AttachTo("Product", entity, "*");
shirtContext.SaveChanges(saveOptions);

As you can see in the preceding code, there’s no need to fetch the entity from the Table service because you already know the PartitionKey (Shirts), and the RowKey can be extracted from the command argument of the Delete button. This optimization saves you that extra fetch.

Now that we’ve explored both inserting and deleting, it’s time to complete the set and look at updates.

Other -----------------
- Working with the Table service REST API - Authenticating requests against the Table service
- Content delivery networks
- Using BLOB storage as a media server (part 3) - A Silverlight-based chunking media player
- Using BLOB storage as a media server (part 2) - A WPF-based adaptive-streaming video player
- Using BLOB storage as a media server (part 1) - Building a Silverlight or WPF video player
- Hosting Silverlight applications in BLOB storage (part 2) - Communicating with third-party sites
- Hosting Silverlight applications in BLOB storage (part 1) - Hosting the Silverlight Spectrum emulator
- Hosting static HTML websites (part 2) - Publishing your website to BLOB services
- Hosting static HTML websites (part 1) - Creating a static HTML website
- Performing storage account operations using REST (part 3) - Creating a table using the REST API
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server