Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Mapping Arbitrary Data with DynamoDB Using the AWS SDK for .NET Object Persistence Model

In addition to the supported .NET types (see Supported Data Types), you can use types in your application for which there is no direct mapping to the DynamoDB types. The object persistence model supports storing data of arbitrary types as long as you provide the converter to convert data from the arbitrary type to the DynamoDB type and vice-versa. The converter code transforms data during both the saving and loading of the objects.

You can create any types on the client-side, however the data stored in the tables is one of the DynamoDB types and during query and scan any data comparisons made are against the data stored in DynamoDB.

The following C# code example defines a Book class with Id, Title, ISBN, and Dimension properties. The Dimension property is of the DimensionType that describes Height, Width, and Thickness properties. The example code provides the converter methods, ToEntry and FromEntry to convert data between the DimensionType and the DynamoDB string types. For example, when saving a Book instance, the converter creates a book Dimension string such as "8.5x11x.05", and when you retrieve a book, it converts the string to a DimensionType instance.

The example maps the Book type to the ProductCatalog table. For illustration, it saves a sample Book instance, retrieves it, updates its dimensions and saves the updated Book again.

For step-by-step instructions on how to test the following sample, see .NET Code Samples.

using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;
using Amazon.SecurityToken;

namespace com.amazonaws.codesamples
    class HighLevelMappingArbitraryData
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();

        static void Main(string[] args)
                DynamoDBContext context = new DynamoDBContext(client);

                // 1. Create a book.
                DimensionType myBookDimensions = new DimensionType()
                    Length = 8M,
                    Height = 11M,
                    Thickness = 0.5M

                Book myBook = new Book
                    Id = 501,
                    Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data",
                    ISBN = "999-9999999999",
                    BookAuthors = new List<string> { "Author 1", "Author 2" },
                    Dimensions = myBookDimensions


                // 2. Retrieve the book.
                Book bookRetrieved = context.Load<Book>(501);

                // 3. Update property (book dimensions).
                bookRetrieved.Dimensions.Height += 1;
                bookRetrieved.Dimensions.Length += 1;
                bookRetrieved.Dimensions.Thickness += 0.2M;
                // Update the book.

                Console.WriteLine("To continue, press Enter");
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }
    public class Book
        [DynamoDBHashKey]   //Partition key
        public int Id { get; set; }
        public string Title { get; set; }
        public string ISBN { get; set; }
        // Multi-valued (set type) attribute. 
        public List<string> BookAuthors { get; set; }
        // Arbitrary type, with a converter to map it to DynamoDB type.
        public DimensionType Dimensions { get; set; }

    public class DimensionType
        public decimal Length { get; set; }
        public decimal Height { get; set; }
        public decimal Thickness { get; set; }

    // Converts the complex type DimensionType to string and vice-versa.
    public class DimensionTypeConverter : IPropertyConverter
        public DynamoDBEntry ToEntry(object value)
            DimensionType bookDimensions = value as DimensionType;
            if (bookDimensions == null) throw new ArgumentOutOfRangeException();

            string data = string.Format("{1}{0}{2}{0}{3}", " x ",
              bookDimensions.Length, bookDimensions.Height, bookDimensions.Thickness);

            DynamoDBEntry entry = new Primitive { Value = data };
            return entry;

        public object FromEntry(DynamoDBEntry entry)
            Primitive primitive = entry as Primitive;
            if (primitive == null || !(primitive.Value is String) || string.IsNullOrEmpty((string)primitive.Value))
                throw new ArgumentOutOfRangeException();

            string[] data = ((string)(primitive.Value)).Split(new string[] { " x " }, StringSplitOptions.None);
            if (data.Length != 3) throw new ArgumentOutOfRangeException();

            DimensionType complexData = new DimensionType
                Length = Convert.ToDecimal(data[0]),
                Height = Convert.ToDecimal(data[1]),
                Thickness = Convert.ToDecimal(data[2])
            return complexData;