繁体   English   中英

DynamoDB - 使用适用于 .NET 的 AWS 开发工具包映射任意 C# 数据类型

[英]DynamoDB - Mapping Arbitrary C# Data Type With AWS SDK for .NET

我试图将一个复杂的 C# 对象持久化到 DynamoDB 中,它看起来像这样。

  [DynamoDBTable("some_table")]
  public class User
  {
    [DynamoDBHashKey]  
    public string user_id { get; set; }
    [DynamoDBRangeKey]
    public string client_name { get; set; }
    public string client_location { get; set; }
    public DateTime signup_date { get; set; }
    [DynamoDBProperty(typeof(KycConverter))]
    public KycAttributes kyc_attributes { get; set; }
  }

  public class KycAttributes
  {
      public string kyc_id { get; set; }
  }

KycAttributes 类型属性需要转换为 DynamoDB 接受的格式。 这是遵循本指南https://dotnetcodr.com/2015/02/02/using-amazon-dynamodb-with-the-aws-net-api-part-4-record-insertion/的转换器实现:

  public class KycConverter : IPropertyConverter
  {
      public object FromEntry(DynamoDBEntry entry)
      {
          Primitive primitive = entry as Primitive;
          if (primitive == null) return new KycAttributes();

          if (primitive.Type != DynamoDBEntryType.String)
          {
              throw new InvalidCastException(string.Format("KycAttributes cannot be converted as its type is {0} with a value of {1}"
                  , primitive.Type, primitive.Value));
          }

          string json = primitive.AsString();
          return JsonConvert.DeserializeObject<KycAttributes>(json);
      }

      public DynamoDBEntry ToEntry(object value)
      {
          Trace.TraceInformation("Invoked");
          KycAttributes attributes = value as KycAttributes;
          if (attributes == null) return null;

          string json = JsonConvert.SerializeObject(attributes);
          return new Primitive(json);
      }
  }

然后,当DynamoDBContext.SaveAsync<User>被调用时,将引发具有以下DynamoDBContext.SaveAsync<User>跟踪的异常:

Object reference not set to an instance of an object.
at Amazon.DynamoDBv2.Model.Internal.MarshallTransformations.AttributeValueMarshaller.Marshall(AttributeValue requestObject, JsonMarshallerContext context) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Generated\Model\Internal\MarshallTransformations\AttributeValueMarshaller.cs:line 48
   at Amazon.DynamoDBv2.Model.Internal.MarshallTransformations.UpdateItemRequestMarshaller.Marshall(UpdateItemRequest publicRequest) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Generated\Model\Internal\MarshallTransformations\UpdateItemRequestMarshaller.cs:line 165
   at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\Marshaller.cs:line 79
   at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\Marshaller.cs:line 51
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CallbackHandler.cs:line 60
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.DynamoDBv2.DocumentModel.Table.UpdateHelperAsync(Document doc, Key key, UpdateItemOperationConfig config, CancellationToken cancellationToken) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Custom\DocumentModel\Table.cs:line 871
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Custom\DataModel\Context.cs:line 285

请注意,场景如下:这是一个已存在于数据库中的对象。 但是,该对象没有 kyc_attributes 密钥,并且正在尝试向其中添加该密钥。 这可能吗? 如果是这样,如何修复错误?

就我而言,我尝试使用Save写入 DynamoDB 的对象Save HashKey设置为null 我想这可能也是使用SaveAsync时出错的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM