1

我在 DynamoDB 中保存用户配置文件,并使用属性将 .net 类型转换为 dynamo 条目

所以这是我正在使用的转换器:

public class DictRoomClassHouseInfoConverter : IPropertyConverter
{
    ...

    public DynamoDBEntry ToEntry(object value)
    {
        var dictionary = value as Dictionary<RoomClass, HouseInfo>;

        if (dictionary == null)
        {
            throw new ArgumentException("Invalid type");
        }

        var entry = new PrimitiveList();

        foreach (var kvp in dictionary)
        {
            entry.Add(new Primitive { Value = string.Format("{0}|{1}|{2}", (int)kvp.Key, kvp.Value.House, kvp.Value.TimesSold) });
        }

        return entry;
    }
}

现在的问题是,当 Dictionary 为空并且PrimitiveList返回一个空时ToEntry,此 Dictionary 的更改将不会被保存

所以如果我做类似的事情:

var profile = GetProfile(id);
//profile.DictProp has 3 items; 


profile.DictProp.Clear();
//profile.DictProp has 0 items;

SaveProfile(profile);

var p = GetProfile(id);
//profile.DictProp has 3 items; 
4

2 回答 2

6

@jtlebi 正确回答该服务不接受空字符串或空集。.NET SDK 以此为基础,如果您尝试保存一个空的 PrimitiveList,SDK 将忽略它并且属性不会改变。

但是,仍有一种方法可以删除属性:将 .NET 对象上的字段设置为 null 或从 IPropertyConverter.ToEntry 方法返回 null。然后,当您调用 DynamoDBContext.Save 时,SDK 将为该特定属性发出 DELETE 命令。

于 2012-10-31T21:19:09.470 回答
3

ValidationException如果您尝试保存具有空属性的元素,DynamoDB 将始终返回 a 。

避免这种情况的唯一方法是在没有此字段的情况下保存项目。

这就是说,我不知道.NET SDK,但我很惊讶它不能自动处理这个?

有关 DynamoDB 棘手限制的列表,请参阅http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/Limits.html

于 2012-10-31T17:57:51.220 回答