C# 具有扩展数据的DynamoDb持久性模型

C# 具有扩展数据的DynamoDb持久性模型,c#,amazon-web-services,amazon-dynamodb,C#,Amazon Web Services,Amazon Dynamodb,我有一个模型 class Person { [DynamoDBHashKey("PK")] public string Id {get;set;} [DynamoDBProperty()] public string Name {get;set;} } 在我的dynamoDb表中,有许多具有不同属性的人员,但在我的系统中,Name是必填属性 我想从数据库中找到这种人: { "PK":"123", "Name": "John", "Pos

我有一个模型

class Person
{
    [DynamoDBHashKey("PK")]
    public string Id {get;set;}

    [DynamoDBProperty()]
    public string Name {get;set;}
}
在我的dynamoDb表中,有许多具有不同属性的人员,但在我的系统中,
Name
是必填属性

我想从数据库中找到这种人:

{
    "PK":"123",
    "Name": "John",
    "Position": "Developer",
    "Address": "NY"
}
并将其映射到我的持久化模型,但除了
Name
之外,我还希望获得所有尚未映射的属性

所以我想扩展我的模型如下:

class Person
{
    [DynamoDBHashKey("PK")]
    public string Id {get;set;}

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

    public Dictionary<string, string> AllOtherProperties {get;set;}
}
班级人员
{
[DynamoDBHashKey(“PK”)]
公共字符串Id{get;set;}
[DynamoDBProperty()]
公共字符串名称{get;set;}
公共字典AllOtherProperties{get;set;}
}
有机会这样做吗


我知道,当反序列化json时,我们可以使用
[JsonExtensionData]
属性,所以我正在寻找类似的行为。

不幸的是,我不相信通过DynamoDBContext可以实现。 除非您的所有附加属性都在“AllocherPropertiesAttribute”项下(我怀疑情况是否如此,尤其是根据您的示例)

但是您可以在底层尝试低级的dynamodb客户机,它返回属性映射。在这种情况下,您可以将其他字段存储到字典中

    var result = await client.GetItemAsync("persontable", new Dictionary<string, AttributeValue>
    {
        {  "PK", new AttributeValue { S = "MYID123" } }
    });

    var person = new Person
    {
        Id = result.Item["PK"].S,
        Name = result.Item["Name"].S,
        OtherAttributes = new Dictionary<string, string>()
    };

    foreach (var attribute in result.Item)
    {
        if (attribute.Key != "PK" && attribute.Key != "Name")
        {
            person.OtherAttributes.Add(attribute.Key, attribute.Value.S); // This code is handling only the case when attirubutes are on the top level and has string type. For more complex scenarios it should be extended additionally. 
        }
    }
var result=wait client.GetItemAsync(“persontable”,新字典)
{
{“PK”,新属性值{S=“MYID123”}
});
var person=新的人
{
Id=结果项[“主键”].S,
Name=result.Item[“Name”].S,
OtherAttributes=新字典()
};
foreach(result.Item中的var属性)
{
if(attribute.Key!=“PK”&&attribute.Key!=“Name”)
{
person.OtherAttributes.Add(attribute.Key,attribute.Value.S);//此代码仅处理AttirButes位于顶层且具有字符串类型的情况。对于更复杂的场景,应额外扩展此代码。
}
}

这种方法的缺点是,您需要自己处理映射,并以相同的方式处理保存操作。代码很难支持,put仍然可能适用于您处理的少量属性(在您的案例Id和名称中)。

不幸的是,我不认为可以通过DynamoDBContext实现。 除非您的所有附加属性都在“AllocherPropertiesAttribute”项下(我怀疑情况是否如此,尤其是根据您的示例)

但是您可以在底层尝试低级的dynamodb客户机,它返回属性映射。在这种情况下,您可以将其他字段存储到字典中

    var result = await client.GetItemAsync("persontable", new Dictionary<string, AttributeValue>
    {
        {  "PK", new AttributeValue { S = "MYID123" } }
    });

    var person = new Person
    {
        Id = result.Item["PK"].S,
        Name = result.Item["Name"].S,
        OtherAttributes = new Dictionary<string, string>()
    };

    foreach (var attribute in result.Item)
    {
        if (attribute.Key != "PK" && attribute.Key != "Name")
        {
            person.OtherAttributes.Add(attribute.Key, attribute.Value.S); // This code is handling only the case when attirubutes are on the top level and has string type. For more complex scenarios it should be extended additionally. 
        }
    }
var result=wait client.GetItemAsync(“persontable”,新字典)
{
{“PK”,新属性值{S=“MYID123”}
});
var person=新的人
{
Id=结果项[“主键”].S,
Name=result.Item[“Name”].S,
OtherAttributes=新字典()
};
foreach(result.Item中的var属性)
{
if(attribute.Key!=“PK”&&attribute.Key!=“Name”)
{
person.OtherAttributes.Add(attribute.Key,attribute.Value.S);//此代码仅处理AttirButes位于顶层且具有字符串类型的情况。对于更复杂的场景,应额外扩展此代码。
}
}
这种方法的缺点是,您需要自己处理映射,并以相同的方式处理保存操作。代码很难支持,但在处理少量属性(在您的案例Id和名称中)的情况下,put仍然可能适用