Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert T object to Amazon DynamoDB Document dynamically

I am trying to use DynamoDB in Amazon AWS in my MVC .net project. And I am also trying to do a Business-DataAccess-Model layered project.

I have a GenericDataRepository class which implements an Add() functionality.

I am sending a T object to Add() and I would like to convert that to Amazon's Document object, dynamically. How can I do that and what is the best practice?

public void Add(T entity)
{
    if (entity == null)
        return;

    var doc = new Document();

    // Convert entity to Document automatically
    doc["Title"] = entity.Title;
    doc["Body"] = entity.Body;
    doc["Author"] = entity.Author;
    // Convert entity to Document automatically


    .....
    .....       
    .....
}
like image 548
Arif YILMAZ Avatar asked Feb 16 '17 13:02

Arif YILMAZ


People also ask

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

What is Marshall in DynamoDB?

marshall(data, options) ⇒ mapConvert a JavaScript object into a DynamoDB record. Examples: Convert a JavaScript object into a DynamoDB record.

How does Amazon DynamoDB perform automatic scaling?

To configure auto scaling in DynamoDB, you set the minimum and maximum levels of read and write capacity in addition to the target utilization percentage. Auto scaling uses Amazon CloudWatch to monitor a table's read and write capacity metrics. To do so, it creates CloudWatch alarms that track consumed capacity.

Can you store an object in DynamoDB?

The DynamoDBMapper utility class makes it simple to store instances of this class in DynamoDB.


2 Answers

In a Latest version of aws dynamo db SDK. this feature is supported, you can achieve your goal without any hassle. Simple and Fast

    public Document Add <T>(T entity, IAmazonDynamoDB dbClient)
            {
                Amazon.DynamoDBv2.DataModel.DynamoDBContext db = new Amazon.DynamoDBv2.DataModel.DynamoDBContext(dbClient);                                   
                Document document = db.ToDocument(entity);
                return document;
            }
like image 176
Dhanesh H S Avatar answered Sep 28 '22 01:09

Dhanesh H S


I think you can use Json.net to serialize your object to json and then create dynamoDb document form it according to their docs:

var jsonText = JsonConvert.SerializeObject(entity, Formatting.Indented);
var item = Document.FromJson(jsonText);
like image 35
Sergey Avatar answered Sep 28 '22 01:09

Sergey