Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a class properties in dynamodb without using attribute in .net

I am very new in dynamodb. I am following http://www.rkconsulting.com/blog/persistence-model-framework-with-aws-dynamodb step by step tutorial for connecting and CRUD operation in dynamodb and it`s works fine.

In that tutorial they using attribute mapping for map class properties

[DynamoDBTable("Dinosaur")]
public class Dinosaur
{
    [DynamoDBHashKey]
    public string Id { get; set; }

    [DynamoDBProperty(AttributeName = "Name")]
    public string Name { get; set; }

    [DynamoDBProperty(AttributeName = "HeightMetres")]
    public double HeightMetres { get; set; }

    [DynamoDBProperty(AttributeName = "WeightKG")]
    public double WeightKg { get; set; }

    [DynamoDBProperty(AttributeName = "Age")]
    public int Age { get; set; }

    [DynamoDBProperty(AttributeName = "Characteristics")]
    public List<string> Characteristics { get; set; }

    [DynamoDBProperty(AttributeName = "Photo", Converter = typeof(ImageConverter))]
    public Image Photo { get; set; }

    [DynamoDBIgnore]
    public int IgnoreMe { get; set; }
}

My question is there any way to map class properties without using attribute ?

like as mongoDb

public class Employee
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
}

we can write this in this way in a separate class

BsonClassMap.RegisterClassMap<Employee>(cm => {
  cm.AutoMap();
  cm.IdMemberMap.SetRepresentation(BsonType.ObjectId);
});

Is it possible in dynamodb ?

like image 374
Hasanuzzaman Avatar asked Apr 06 '15 05:04

Hasanuzzaman


People also ask

What are the three attributes of DynamoDB?

DynamoDB uses three basic data model units, Tables, Items, and Attributes. Tables are collections of Items, and Items are collections of Attributes. Attributes are basic units of information, like key-value pairs.

What is attribute in DynamoDB?

In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How it works. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality.

What is Mapper class in DynamoDB?

The DynamoDBMapper class is the entry point to Amazon DynamoDB. It provides access to a DynamoDB endpoint and enables you to access your data in various tables. It also enables you to perform various create, read, update, and delete (CRUD) operations on items, and run queries and scans against tables.

Does DynamoDB support map?

The following example shows a map that contains a string, a number, and a nested list that contains another map. DynamoDB lets you work with individual elements within maps, even if those elements are deeply nested. For more information, see Using expressions in DynamoDB.


2 Answers

In the latest version of the .NET SDK you don't have to put in the attribute tags, it will see all read/write properties and upload the attributes as the same name. You would only have to use the [DynamoDBProperty(...)] if you want the attribute name in DynamoDB to be something other than the .NET object name.

So in your case you could simply remove that attribute for all properties except photo (which needs the converter, you could remove the AttributeName part of it) and WeightKg (because the capitalization is different) and you would get the same result.

I see this is a little bit older question now, so it may not have been that way in older versions (not sure) but I'm using 3.3.0.0 of the SDK and it does work that way. You have probably moved on but answering for others that may come upon this thread as I did...

like image 100
sfaust Avatar answered Nov 14 '22 21:11

sfaust


There is no way, the default "strongly typed" client relies on attributes.

If you have time to do the plumbing yourself - there is nothing stopping your from doing your own implementation of the POC to Dynamo mapping though. Amazon client api (AWSSDK.DynamoDBv2) exposes the raw class AmazonDynamoDBClient which handles all the API calls and the DynamoDBConext is just implementation of IDynamoDBContext interface - which exposes all the "strongly typed" operations. So you can make your own implementation and take different mapping approach in it.

Also you can make a feature request for this: https://github.com/aws/aws-sdk-net/issues

like image 20
Ondrej Svejdar Avatar answered Nov 14 '22 23:11

Ondrej Svejdar