1

I have a namespace that contains some classes, one of the classes I'm working on contains properties, where each proprty has an attribute associated with it, as the follwing

namespace Local.Business
{
    [DynamoDBTable("myTableName")]     
    public class Business
    {
        [DynamoDBHashKey("PK")] 
        public string MunId {get; set;}

        [DynamoDBRangeKey("SK")]
        public string Id {get; set;}

        [DynamoDBProperty("Dba")] 
        public string Dba {get; set;} 
    }
}

the string "myTableName" need to be determined at runtime(by calling a function or reading it from other class's property) How can I achieve that, please?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Tarik Ziyad
  • 115
  • 1
  • 10
  • 1
    It's a bad idea to try and edit the data stored in attributes, but if you really need to then you'll need to use reflection. – Sean Mar 04 '20 at 12:22
  • have a look on this so question [C# How to set attributes values using reflection](https://stackoverflow.com/questions/2160476/how-to-set-attributes-values-using-reflection) – rekcul Mar 04 '20 at 12:23
  • how can change it using reflection? thanks @sean – Tarik Ziyad Mar 04 '20 at 12:23
  • 1
    @Sean that just doesn't work: https://gist.github.com/mgravell/08b20ed8fd7c4da714028a15eb1893d0 – Marc Gravell Mar 04 '20 at 12:32

3 Answers3

0

What you are trying to do is inherently flawed. You kinda can ish change attributes sort of, sometimes, but there's a good chance that whatever is consuming the attributes won't see the change, which makes it entirely pointless.

Basically: you need to find another way of reconfiguring DynamoDB at runtime. This isn't it.


For the "kinda can ish":

  • you can materialize attributes, and if they're mutable, change the copies you have; but when other code materializes the attributes, they'll get different unrelated versions, which will not have any changes you have made
  • there is an API that supports this concept (System.ComponentModel), but: most attribute consumers do not use that API - it is mostly just UI binding tools (think PropertyGrid, DataGridView, etc) that would take any notice of it - because they are expecting to work with things like DataTable that require a different approach to metadata and reflection
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

Set the table name value to empty string in the class file:

[DynamoDBTable("")]

During runtime, use the overloaded functions on DynamoDBMapper to pass DynamoDBMapperConfig configured with TableNameOverride

Parag Kutarekar
  • 975
  • 11
  • 10
0

Actually I deleted the table property and I gave the table name in the query

 dynamoDBOperationConfig = new DynamoDBOperationConfig();
 dynamoDBOperationConfig.OverrideTableName = "tableName";
 string munId = "1";
 var search = dynamoDBcontext.QueryAsync<Business>(munId, dynamoDBOperationConfig);

and It works fine, thank you all guys for helping

Tarik Ziyad
  • 115
  • 1
  • 10