MyTableItem
で定義された項目を持つDynamoDBテーブルにをDynamoDBProperty
として追加する必要があります。C#DynamoDB複合型IList <T>コンバータ
このAWS Documentation pageの情報を使用して、MyCustomType
(IList<MyCustomType>
ではなく)のコンバータを実装しました。 しかし、新しいMyTableItem
を作成すると、ToEntry()
メソッドが、IList<MyCustomType>
のオブジェクトであり、MyCustomType
ではないオブジェクトをパラメータとして受け取っていることに気付きました。私はリスト(List
またはIList
、または一般にコレクションが)自動的DynamoDBので扱われたことを理解していたドキュメントを読ん
...
どのように私は望ましい結果を達成することができますか?
これはコードです:
// 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;
}
}