3

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();
5
  • @gunr2171 Yes I am already using configurations in my application. In Startup as: services.AddSingleton<IConfiguration>(Configuration). And then in my class which requires the key as: IConfiguration _configuration; _configuration["UserId"]. But how can I get it over this class attribute? Commented Jul 10, 2018 at 19:23
  • @gunr2171 should be fine, but what edits are you talking about. I have updated my code, please check. Commented Jul 10, 2018 at 19:28
  • Oh wait, you're talking about trying to change the value of the [DynamoDBTable] attribute using the configuration file? Commented Jul 10, 2018 at 19:42
  • @gunr2171 yeah, I just realized that is what he is trying to do, I'm looking to see if there is anything like the ConfigurationPropertyAttribute decoration that was available in the .NetFramework Commented Jul 10, 2018 at 19:43
  • Thanks for the edit, I should have made it more clear while posting. In MVC controller we could create custom filter/attribute so I was looking something around those lines. The above class is being called in my MVC controller of an API. I just dint wanted to hardcode the attribute values and wanted to see if there is an option to get these from the appSettings.json. Commented Jul 11, 2018 at 2:05

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi aman i tried Your Code. But the table is not dynamically loading. I have assumed the code from start GetDynamoDbOperationConfig("MyData"); It did not shows the error. But the table is not inserted. When i give [DynamoDBTable("MyData")] the datas are storing in DynamoDB. I have try to use this code by this way [DynamoDBTable( H ighLevelTableExample.GetDynamoDbOperationConfig(S_tablefieldset))] // s_tablefield set is a string variable. So How do you call the variable dynamically. if you share your code it would helpful for many peoples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.