The AWS SDK for .NET provides document model classes that wrap some of the low-level
DynamoDB
operations, to further simplify your coding. In the document model, the primary classes
are
Table and Document. The Table class provides data
operation methods such as PutItem, GetItem, and
DeleteItem. It also provides the Query and the
Scan methods. The Document class represents a single item in a
table.

The preceding document model classes are available in the
Amazon.DynamoDBv2.DocumentModel namespace.

Operations Not Supported by the Document
Model

You cannot use the document model classes to create, update, and delete tables. The
document model does support most common data operations, however.

Getting an Item - Table.GetItem

The GetItem operation retrieves an item as a Document
instance. You must provide the primary key of the item that you want to retrieve as
shown in the following C# code snippet:

The GetItem operation returns all the attributes of the item and performs an
eventually consistent read (see Read Consistency) by default.

Specifying Optional Parameters

You can configure additional options for the GetItem operation by
adding the GetItemOperationConfig parameter. For a complete list of
optional parameters, see GetItem. The following C# code snippet retrieves an item from the ProductCatalog
table. It specifies the GetItemOperationConfig to provide the
following optional parameters:

The AttributesToGet parameter to retrieve only the
specified attributes.

The ConsistentRead parameter to request the latest values
for all the specified attributes. To learn more about data
consistency, see Read Consistency.

Deleting an Item - Table.DeleteItem

The DeleteItem operation deletes an item from a table. You can either
pass the item's primary key as a parameter or if you have already read an item and
have the corresponding Document object, you can pass it as a parameter
to the DeleteItem method as shown in the following C# code snippet.

Specifying Optional Parameters

You can configure additional options for the Delete operation by
adding the DeleteItemOperationConfig parameter. For a complete list
of optional parameters, see DeleteTable. The following C# code snippet specifies
the two following optional parameters:

The ConditionalExpression parameter to ensure that the
book item being deleted has a specific value for the ISBN attribute.

The ReturnValues parameter to request that the
Delete method return the item that it deleted.

Updating an Item - Table.UpdateItem

The UpdateItem operation updates an existing item if it is present.
If the item that has the specified primary key is not found, the
UpdateItem operation adds a new item.

You can use the UpdateItem operation to update existing attribute
values, add new attributes to the existing collection, or delete attributes from the
existing collection. You provide these updates by creating a Document
instance that describes the updates you wish to perform.

The UpdateItem action uses the following guidelines:

If the item does not exist, UpdateItem adds a new
item using the primary key that is specified in the input.

If the item exists, UpdateItem applies the
updates as follows:

Replaces the existing attribute values with the values in the
update.

If an attribute that you provide in the input does not exist,
it adds a new attribute to the item.

If the input attribute value is null, it deletes the
attributes, if it is present.

Note

This mid-level UpdateItem operation does not support the
Add action (see UpdateItem) supported by the underlying DynamoDB operation.

Note

The PutItem operation (Putting an Item - Table.PutItem Method) can also can perform an update. If
you call PutItem to upload an item and the primary key exists,
the PutItem operation replaces the entire item. Note that, if
there are attributes in the existing item and those attributes are not
specified on the Document that is being put, the PutItem
operation deletes those attributes. However, UpdateItem
only updates the specified input attributes. Any other existing attributes
of that item will remain unchanged.

The following are the steps to update an item using the AWS SDK for .NET document
model.

Execute the Table.LoadTable method by providing the name
of the table in which you want to perform the update operation.

Create a Document instance by providing all the updates
that you wish to perform.

To delete an existing attribute, specify the attribute value as
null.

Call the Table.UpdateItem method and provide the
Document instance as an input parameter.

You must provide the primary key either in the
Document instance or explicitly as a
parameter.

The following C# code snippet demonstrates the preceding tasks. The code sample
updates an item in the Book table. The UpdateItem operation updates the
existing Authors attribute, deletes the PageCount attribute, and adds a
new attribute XYZ. The Document instance includes the primary key of
the book to update.

Batch Write - Putting and Deleting Multiple
Items

Batch write refers to putting and deleting multiple items in a batch. The
operation enables you to put and delete multiple items from one or more tables in
a
single call. The following are the steps to put or delete multiple items from a
table using the AWS SDK for .NET document model API.

Create a Table object by executing the Table.LoadTable
method by providing the name of the table in which you want to perform
the batch operation.

Execute the CreateBatchWrite method on the table instance
you created in the preceding step and create
DocumentBatchWrite object.

Use DocumentBatchWrite object methods to specify
documents you wish to upload or delete.

Call the DocumentBatchWrite.Execute method to execute the
batch operation.

When using the document model API, you can specify any number of
operations in a batch. However, note that DynamoDB limits the number of
operations in a batch and the total size of the batch in a batch
operation. For more information about the specific limits, see
BatchWriteItem. If the document model API detects your batch write
request exceeded the number of allowed write requests or the HTTP
payload size of a batch exceeded the limit allowed by BatchWriteItem, it breaks the
batch in to several smaller batches. Additionally, if a response to a
batch write returns unprocessed items, the document model API will
automatically send another batch request with those unprocessed
items.

The following C# code snippet demonstrates the preceding steps. The code snippet
uses batch write operation to perform two writes; upload a book item and delete
another book item.

You can use the batch write operation to perform put and delete operations on
multiple tables. The following are the steps to put or delete multiple items from
multiple table using the AWS SDK for .NET document model.

You create DocumentBatchWrite instance for each table in
which you want to put or delete multiple items as described in the
preceding procedure.

Create an instance of the MultiTableDocumentBatchWrite
and add the individual DocumentBatchWrite objects in
it.

Execute the MultiTableDocumentBatchWrite.Execute
method.

The following C# code snippet demonstrates the preceding steps. The code snippet
uses batch write operation to perform the following write operations:

Put a new item in the Forum table item

Put an item in the Thread table and delete an item from the same table.