aman
aman

Reputation: 6252

AWS DynamoDB - Dynamically setting DynamoDBTable in object persistence model

I have a .NET Core 2.0 project using AWSDynamodDb (you can ignore dynamo db here as question is not regarding dynamodDB)

I have the following entity class as:

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

    public string Name{ get; set; }
}

What I want is the "TableName" - the value passed into the attribute - to be populated from my appSettings.json file.

 {
    "TableName": "NewTable" 
 }

Would it be possible to get this value durring runtime by looking at the key from appSettings file?

--Updated--

I am calling the "MyData" class from my controller as below:

  var response = await context.ScanAsync<MyData>(conditions).GetRemainingAsync();

Upvotes: 3

Views: 1840

Answers (1)

aman
aman

Reputation: 6252

Just for anyone else who come across I was able to resolve this as below:

Remove the table name from the model.

And then use the below code to set the table name dynamically

    public static DynamoDBOperationConfig GetDynamoDbOperationConfig(string dynamoDbTable)
    {
        DynamoDBOperationConfig config = new DynamoDBOperationConfig() { OverrideTableName = dynamoDbTable };
        return config;
    } 

We can call this function whenever we are calling any operation against dynamo db like scan, save, delete etc.

Upvotes: 8

Related Questions