【问题标题】:DynamoDb converting Attribute Values to TypesDynamoDb 将属性值转换为类型
【发布时间】:2023-02-11 03:26:17
【问题描述】:

有没有更有效的方法将发电机数据库数据转换为具体类型?例如,当我查询数据时,所有内容都在:

List<Dictionary<string, AttributeValue>>

是否可以轻松转换类型,而不必遍历每个项目并手动执行所有操作?

例如我在做:

    return items.Select(item => new Connection
        {
            ConnectionId = Guid.Parse(item["connectionId"].S),
            ClientId = item["clientId"].S,
            ProviderId = item["providerId"].S,
            Scopes = item["scopes"].SS.ToArray(),
            CredentialsId = item["credentialsId"].S,
            Evidences = ToEvidences(item["consentEvidences"].L)
        })
        .ToList();

然后这将返回我的类型列表 Connection 但是我明确映射了每个字段。有没有更简单的方法或可以进行映射的帮助程序库?

【问题讨论】:

    标签: c# amazon-dynamodb


    【解决方案1】:

    我认为您会幸运地使用更高级别的 .NET 文档模型。它呈现更自然的数据类型。

    https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DotNetSDKMidLevel.html

    【讨论】:

      【解决方案2】:

      我发现最简单的方法是使用 Document.FromAttributeMap 函数将其转换为 Document 对象,然后使用 DynamoDBContext.FromDocument 方法将其再次转换为 .NET 类型,如下所示。

      public async Task<IEnumerable<WeatherForecast>> GetAll(string cityName)
      {
          var queryRequest = new QueryRequest()
          {
              TableName = nameof(WeatherForecast),
              KeyConditionExpression = "CityName = :cityName",
              ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
              {
                  {":cityName", new AttributeValue(cityName)},
              }
          };
          var response = await _dynamoDbClient.QueryAsync(queryRequest);
      
          return response.Items.Select(a =>
          {
              var doc = Document.FromAttributeMap(a);
              return _dynamoDbContext.FromDocument<WeatherForecast>(doc);
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2022-11-16
        • 1970-01-01
        • 2021-06-13
        • 2021-07-08
        • 1970-01-01
        • 2017-05-06
        • 2015-03-29
        • 2013-10-26
        • 2021-06-26
        相关资源
        最近更新 更多