In this section, you’ll build a new
product-management web page to manage the Hawaiian shirt product list
stored in the Table service. Through this web page, you’ll be able to create, read, update, and delete (also known as
CRUD) data in the table. Figure 1
shows what the web page will look like.
The product-management web page shown in figure 1 will allow us to do several things:
Add new shirts
List
all shirts
Edit existing shirts
Delete shirts
You’ve already set up the
entity and registered the entity table in the development storage. It’s
time to develop the product-management web page shown in figure 1. The first step is to set up a context class for your
entity.
1. Creating a
context class
In order to work with
entities in any way (query, add, insert, delete) using ADO.NET Data
Services, you first need to set up a context object.
The context class is really a
bridge between an entity and ADO.NET Data Services. It will define the
endpoint of the storage account, the name of the table that we’ll query,
and the entity that will be returned. The following listing shows the
context class for the Products table.
Listing
1. Product context class
Listing 1
shows the context object for the Hawaiian shirt Product entity. As you can see, most of the complexity of the
context class is abstracted away in the classes that you inherit from.
The TableServiceContext class
inherits from the standard ADO.NET Data Services context class, DataServicesContext.
The TableServiceContext
class provides some additional functionality beyond what is provided
out of the box with ADO.NET Data Services, including retry policies.
In listing 1, the storage account details and credentials are
automatically populated from the service configuration. This allows you
to simplify your calling classes—you don’t need to get the endpoint and
credentials every time you wish to use the context class.
Finally, the Product
property is what you’ll use to perform LINQ queries on the Products
table.
Tip
Code generation is outside the
scope of this book, but if you’re generating a large number of tables
and entities, you may wish to consider using a code generation tool such
as T4 to autogenerate code where possible. Typical areas to consider
generating code automatically would include table context classes and
table-creation scripts.
Let’s look at how you can
start using this.