MattW
MattW

Reputation: 13212

Amazon DynamoDB - Is there a way to not specify each AttributeName?

Reading the DynamoDB docs, it seems that there is no way to not list out each attribute name/value when inserting data.

For example, to insert a user I'd need to do this:

var putRequest = new PutItemRequest
    {
        TableName = UsersTableName,
        Item = new Dictionary<string, AttributeValue>()
            {
                {"HashId", new AttributeValue() { S = user.Id.ToString() }},
                {"RangeId", new AttributeValue() { S = user.Email }},
                {"CreatedUtc", new AttributeValue() { S = user.CreatedUtc.ToString() }},
                {"DeactivatedUtc", new AttributeValue() { S = user.DeactivatedUtc.ToString() }},
                {"Timezone", new AttributeValue() { S = "US Eastern Standard Time" }},
            }
    };

var result = DbClient.PutItem(putRequest);

I like the RavenDb model of accepting any POCO and figuring out the "attribute" names on it's own, based on the class' public field names.

My question is: Do I need to do this? Is there away to write the data just by passing the object?

If not I am going to write something that will: I will use reflection to figure out the names and types and generate the PutItemRequest.Item dictionary based on that. I'd rather not if I don't have to.

As always, thanks in advance for any help!

Upvotes: 0

Views: 2098

Answers (2)

MattW
MattW

Reputation: 13212

Actually, Amazon already has exactly what I was looking for: The .NET Object Persistence Model

This takes care of serializing your objects for you - wonderful!

Upvotes: 1

yegor256
yegor256

Reputation: 105043

On every putItem request you should specify all attributes you want to store. If you miss some of them - their values will be deleted from DynamoDB. And there is no workaround at the moment. In order to change one attribute you should getItem first, change it in memory and putItem it back to DynamoDB.

Upvotes: 2

Related Questions