User guide
Document
The document programming model provides an easier way to work with data in DynamoDB.This model
is specifically intended for accessing tables and items in tables.You access this model through the
Amazon.DynamoDBv2.DocumentModel namespace.
Of the three models, the document model is easier to code against DynamoDB data compared to the
low-level programming model. For example, you don't have to convert as many .NET data types to their
equivalents in DynamoDB. However, this model doesn't provide access to as many features as the
low-level programming model. For example, you can use this model to create, retrieve, update, and delete
items in tables. However, to create tables, you must use the low-level model. Finally, this model requires
you to write more code to store, load, and query .NET objects compared to the object persistence model.
The following example shows how to use the document model to insert an item into a table in DynamoDB:
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.DocumentModel;
var client = new AmazonDynamoDBClient();
var table = Table.LoadTable(client, "AnimalsInventory");
var item = new Document();
item["Id"] = 3;
item["Type"] = "Horse";
item["Name"] = "Shadow";
table.PutItem(item);
In the preceding example, the item is inserted into the table through the Table class's PutItem method.
The PutItem method takes an instance of the Document class; the Document class is simply a collection
of initialized attributes.To determine the table to insert the item into, the Table class's LoadTable method
is called, specifying an instance of the AmazonDynamoDBClient class and the name of the target table
in DynamoDB.
The following example shows how to use the document model to get an item from a table in DynamoDB:
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.DocumentModel;
var client = new AmazonDynamoDBClient();
var table = Table.LoadTable(client, "AnimalsInventory");
var item = table.GetItem(3, "Horse");
Console.WriteLine("Id = " + item["Id"]);
Console.WriteLine("Type = " + item["Type"]);
Console.WriteLine("Name = " + item["Name"]);
In the preceding example, the item is retrieved through the Table class's GetItem method.To determine
the item to get, in this example, the GetItem method uses the hash-and-range primary key of the target
item.To determine the table to get the item from, the Table class's LoadTable method uses an instance
of the AmazonDynamoDBClient class and the name of the target table in DynamoDB.
The preceding example implicitly converts the attribute values for Id, Type, Name to strings for the
WriteLine method.You can do explicit conversions by using the various AsType methods of the
DynamoDBEntry class. For example, you could explicitly convert the attribute value for Id from a
Primitive data type to an integer through the AsInt method:
Version v2.0.0
50
AWS SDK for .NET Developer Guide
Programming Models