Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DynamoDB complex type IList<T> converter

I need to add an IList<MyCustomType> as a DynamoDBProperty into a DynamoDB table whose items are defined by the class MyTableItem.

Using information from this AWS Documentation page, I implemented the converter for MyCustomType (not for IList<MyCustomType>). But when creating a new MyTableItem I noticed that the ToEntry() method receive as parameter an object of type IList<MyCustomType> and not MyCustomType.

Reading the documentation I had understood that the lists (List or IList, or collections in general) was automatically handled by DynamoDB...

How can I achieve the desired result?

This is the code:

// MyTableItem
[Serializable]
public class MyTableItem
{
    [DynamoDBHashKey]
    public string Id { get; set; }

    [DynamoDBProperty]
    public string Field1 { get; set; }

    [DynamoDBProperty]
    public string Field2 { get; set; }

    // List of MyCustomType objects
    [DynamoDBProperty(typeof(MyCustomTypeConverter))]
    public IList<MyCustomType> CustomField { get; set; }
}

// MyCustomType
[Serializable]
public class MyCustomType
{
    public string DocumentType { get; set; }
    public string Status { get; set; }
    public string Code { get; set; }
}

// Converter methods
public class MyCustomTypeConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        if (value == null)
            return new Primitive { Value = null };

        MyCustomType item = value as MyCustomType;
        if (item == null)
            throw new InvalidCastException("Cannot convert MyCustomType to DynamoDBEntry.");

        string data = string.Format("{0};{1};{2}", item.DocumentType, item.Status, item.Code);

        DynamoDBEntry entry = new Primitive { Value = data };

        return entry;
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        if (entry == null)
            return new MyCustomType();

        Primitive primitive = entry as Primitive;
        if (primitive == null || !(primitive.Value is string) || string.IsNullOrEmpty((string)primitive.Value))
            throw new InvalidCastException("Cannot convert DynamoDBEntry to MyCustomType.");

        string[] data = ((string)(primitive.Value)).Split(new string[] { ";" }, StringSplitOptions.None);
        if (data.Length != 3)
            throw new ArgumentOutOfRangeException("Invalid arguments number.");

        MyCustomType complexData = new MyCustomType
        {
            DocumentType = Convert.ToString(data[0]),
            Status = Convert.ToString(data[1]),
            Code = Convert.ToString(data[2])
        };

        return complexData;
    }
}
like image 778
Cheshire Cat Avatar asked Sep 02 '16 13:09

Cheshire Cat


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

It seems that the DynamoDb SDK has no problem serializing IList<T>, it does have trouble deserializing. Just speculation, but this is probably because it doesn't know which concrete type to use.

I had a similar setup to yours, and I tried changing my documents to use List<T> and the SDK was able to deserialize without adding any custom IPropertyConverter implementations.

It looks like the full two-way support is only there if you expose a concrete List instead of the interface. So that's one possible way out of the problem.

But if you want to try to solve the problem of the IList I would work with what the SDK is actually sending you, the IList, not the items from the list. To me it would make sense to iterate over that list and convert each item into a list of entries. For deserialization you'll get that collection of entries back, and you can build up a new list of your models.

TL;DR use a list if you can, otherwise implement your converter against the IList<T> not T.

like image 105
Jim Counts Avatar answered Oct 22 '22 15:10

Jim Counts