AWS Developer Tools Blog

DynamoDB Series – Document Model

This week we are running a series of five daily blog posts that will explain new DynamoDB changes and how they relate to the .NET SDK. This is blog post number 2, and today we will be looking at the Document Model API.

Document Model

Yesterday, we learned about Amazon DynamoDB’s new data types such as lists and maps. Today, we are going to talk about how you can use the new data types in the Document Model API.

As a quick refresher on the Document Model API, it is an abstraction over the low-level service client and is found in the Amazon.DynamoDBv2.DocumentModel namespace. Here is an example for creating an item using the Document Model API.

var table = Table.LoadTable(ddbClient, tableName);
Document company = new Document();
company["id"] = "1";
company["name"] = "Dunder Mifflin";
company["industry"] = "paper";

table.PutItem(company);

The Document Model keeps things simple and easy. It takes care of all the data conversions and creating the underlying low-level request objects. Now if you want to take advantage of the new map data type, you can simply create a separate document object and assign it to the parent document. So in our example above, let’s give our paper company an address.

Document address = new Document();
address["street"] = "1725 Slough Avenue";
address["city"] = "Scranton";
address["state"] = "Pennsylvania";

Document company = new Document();
company["id"] = "1";
company["name"] = "Dunder Mifflin";
company["industry"] = "paper";
company["address"] = address;

table.PutItem(company);

To get the data back out, use the GetItem method from the table object passing in the key information for the item.

Document dunder = table.GetItem("1");

Now we can take advantage of the recursive capabilities of the list and map data types and add a list of employees to our branch office.

var address = dunder["address"].AsDocument();
address["employee"] = new List<string> { "Michael Scott", "Dwight Schrute", "Jim Halpert", "Pam Beesly" };
table.UpdateItem(dunder);

As I said, the Document Model translates its calls into the low-level service client. Here is how the Document Model translates the save of the Dunder Mifflin company into a call to the low-level service client.

var request = new PutItemRequest
{
    TableName = tableName,
    Item = new Dictionary<string, AttributeValue>
    {
        {"id", new AttributeValue{S = "1"}},
        {"name", new AttributeValue{S = "Dunder Mifflin"}},
        {"industry", new AttributeValue{S = "paper"}},
        {"address", new AttributeValue
            {M = new Dictionary<string, AttributeValue>
            {
                {"street", new AttributeValue{S = "1725 Slough Avenue"}},
                {"city", new AttributeValue{S = "Scranton"}},
                {"state", new AttributeValue{S = "Pennsylvania"}},
                {"employee", new AttributeValue
                {L = new List<AttributeValue>
                {
                    new AttributeValue{S = "Michael Scott"},
                    new AttributeValue{S = "Dwight Schrute"},
                    new AttributeValue{S = "Jim Halpert"},
                    new AttributeValue{S = "Pam Beesly"}
                }}
            }}
        }
    }}
};


ddbClient.PutItem(request);

Tomorrow, we are going to go deeper into how the Document Model handles converting types passed into a document into the underlying data types that DynamoDB supports.