0

I'm trying to create a "smart" library that can store different objects in a DynamoDb table. For this, I built on the answer to this question in Stackoverflow to generate a new type in runtime according to the fields that we need to save in the table.

https://stackoverflow.com/a/3862241/2390800

The scenario is the following:

1- I create a runtime type. It works perfectly.

var myNewType = MyTypeBuilder.CreateNewType(
    //
    // the name for my runtime type
    "my_type",


    // A list with information about each field to save.
    // The structure for each item is one class like this:
    // class Field {
    //    public string Name { get; set; }
    //    public string Value  { get; set; }
    // }
    // where:
    // Name => name of the field in DynamoDb
    // Value => value for the field in DynamoDb
    fieldAndValueList
);

2- I instance the runtime type by using Activator. It works perfectly.

var myObjectToStore = Activator.CreateInstance(myNewType);

3- I call my "dynamoDbContext" by using reflection due to the "SaveAsync" method is generic and I don't know the "type" of the object to store. I get a Task because "SaveAsync" is async. It works perfectly.

var task = (Task) dynamoDbContext
    .GetType()
    .GetMethods()
    .First(
        m => m.Name == "SaveAsync" &&
             m.IsGenericMethod &&
             m.GetParameters().Any(p => p.ParameterType == typeof(DynamoDBOperationConfig))
    )
    .MakeGenericMethod(myNewType)
    .Invoke(dynamoDbContext, new object[] { myObjectToStore, MyDynamoTableConfig, null });

4- Now, I wait for the task result.

task.Wait();

5- I get the following error:

System.InvalidOperationException: Type System.Object is unsupported, it has no supported members
   at Amazon.DynamoDBv2.DataModel.StorageConfig..ctor(ITypeInfo targetTypeInfo)
   at Amazon.DynamoDBv2.DataModel.ItemStorageConfig..ctor(ITypeInfo targetTypeInfo)
   at Amazon.DynamoDBv2.DataModel.ItemStorageConfigCache.CreateStorageConfig(Type baseType, String actualTableName)
   at Amazon.DynamoDBv2.DataModel.ItemStorageConfigCache.GetConfig(Type type, DynamoDBFlatConfig flatConfig, Boolean conversionOnly)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SerializeToDocument(Object value, Type type, DynamoDBFlatConfig flatConfig)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ToDynamoDBEntry(SimplePropertyStorage propertyStorage, Object value, DynamoDBFlatConfig flatConfig, Boolean canReturnScalarInsteadOfList)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.TryToList(Object value, Type type, DynamoDBFlatConfig flatConfig, DynamoDBList& output)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ToDynamoDBEntry(SimplePropertyStorage propertyStorage, Object value, DynamoDBFlatConfig flatConfig, Boolean canReturnScalarInsteadOfList)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.PopulateItemStorage(Object toStore, ItemStorage storage, DynamoDBFlatConfig flatConfig, Boolean keysOnly, Boolean ignoreNullValues)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorageHelper(Object toStore, ItemStorageConfig config, DynamoDBFlatConfig flatConfig, Boolean keysOnly, Boolean ignoreNullValues)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorage(Object toStore, Type objectType, Boolean keysOnly, DynamoDBFlatConfig flatConfig)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorage[T](T toStore, Boolean keysOnly, DynamoDBFlatConfig flatConfig)
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)

Why does AWS say that I'm using "System.Object" if I'm specifying the "Type" by using reflection?

JaimeCamargo
  • 353
  • 1
  • 3
  • 14
  • 1
    Try https://stackoverflow.com/a/52101333/1048799 – rfmodulator Oct 28 '21 at 22:56
  • Have you tried casting the return value of Activator.CreateInstance() to the "myNewType" type? FWIW it looks like you are fighting against the strongly type nature of C#. Perhaps the dynamodb object persistence model may not be the best fit for what you are trying to do. I recommend you check out the dynmodb document model. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DotNetSDKMidLevel.html – Mike Hixson Nov 03 '21 at 18:31
  • @MikeHixson thanks for your answer. I ended up finding the problem, there was one property that had the "object" type and Dynamo doesn't like this type :-). With the comments from "rfmodulator" I got the issue. – JaimeCamargo Nov 04 '21 at 19:37

0 Answers0