<i id='QqG8i'><tr id='QqG8i'><dt id='QqG8i'><q id='QqG8i'><span id='QqG8i'><b id='QqG8i'><form id='QqG8i'><ins id='QqG8i'></ins><ul id='QqG8i'></ul><sub id='QqG8i'></sub></form><legend id='QqG8i'></legend><bdo id='QqG8i'><pre id='QqG8i'><center id='QqG8i'></center></pre></bdo></b><th id='QqG8i'></th></span></q></dt></tr></i><div id='QqG8i'><tfoot id='QqG8i'></tfoot><dl id='QqG8i'><fieldset id='QqG8i'></fieldset></dl></div>

      <legend id='QqG8i'><style id='QqG8i'><dir id='QqG8i'><q id='QqG8i'></q></dir></style></legend>
      1. <tfoot id='QqG8i'></tfoot>

        <small id='QqG8i'></small><noframes id='QqG8i'>

          <bdo id='QqG8i'></bdo><ul id='QqG8i'></ul>

        使用 .NET SDK 在 DynamoDB 中持久化动态对象

        Persisting dynamic object in DynamoDB with .NET SDK(使用 .NET SDK 在 DynamoDB 中持久化动态对象)
        <legend id='ABBUt'><style id='ABBUt'><dir id='ABBUt'><q id='ABBUt'></q></dir></style></legend>
          <bdo id='ABBUt'></bdo><ul id='ABBUt'></ul>
            1. <i id='ABBUt'><tr id='ABBUt'><dt id='ABBUt'><q id='ABBUt'><span id='ABBUt'><b id='ABBUt'><form id='ABBUt'><ins id='ABBUt'></ins><ul id='ABBUt'></ul><sub id='ABBUt'></sub></form><legend id='ABBUt'></legend><bdo id='ABBUt'><pre id='ABBUt'><center id='ABBUt'></center></pre></bdo></b><th id='ABBUt'></th></span></q></dt></tr></i><div id='ABBUt'><tfoot id='ABBUt'></tfoot><dl id='ABBUt'><fieldset id='ABBUt'></fieldset></dl></div>

              <tfoot id='ABBUt'></tfoot>
                    <tbody id='ABBUt'></tbody>
                • <small id='ABBUt'></small><noframes id='ABBUt'>

                • 本文介绍了使用 .NET SDK 在 DynamoDB 中持久化动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 .NET SDK 将以下类持久化到 DynamoDB:

                  I'm trying to persist the following class to DynamoDB using the .NET SDK:

                  public class MyClass
                  {
                      public string Id { get; set; }
                  
                      public string Name { get; set; }
                  
                      public object Settings { get; set; }
                  }
                  

                  问题在于设置属性.它可以是任何类型的对象,我事先不知道可能分配给它什么.当我尝试将其持久化到 DynamoDB 时,出现以下异常:

                  The problem is with the Settings property. It can be any type of object, and I do not know in advance what might be assigned to it. When I try to persist it to DynamoDB, I get the following exception:

                  System.InvalidOperationException: 'Type System.Object is unsupported, it has no supported members'
                  

                  Document Model 和 Object Persistence Model 方法都会导致相同的异常.

                  Both the Document Model and Object Persistence Model methods result in the same exception.

                  有没有办法将这些对象持久保存在 DynamoDB 中?其他数据库(如 MongoDB 和 Azure DocumentDB)可以毫无问题地执行此操作,并且可以将它们反序列化为带有鉴别器的正确类型,或作为动态 JSON 对象.

                  Is there a way to persist these objects in DynamoDB? Other databases like MongoDB and Azure DocumentDB will do this without any issue, and they can be deserialized to either the proper type with a discriminator, or as a dynamic JSON object.

                  推荐答案

                  您可以使用此处记录的一般方法:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBContext.ArbitraryDataMapping.html

                  You can use the general approach documented here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBContext.ArbitraryDataMapping.html

                  这是我对任意对象的实现:

                  Here's my implementation for any arbitrary object:

                  public class DataConverter : IPropertyConverter
                  {
                      public object FromEntry(DynamoDBEntry entry)
                      {
                          var primitive = entry as Primitive;
                          if (primitive == null || !(primitive.Value is String) || string.IsNullOrEmpty((string)primitive.Value))
                              throw new ArgumentOutOfRangeException();
                          object ret = JsonConvert.DeserializeObject(primitive.Value as string);
                          return ret;
                      }
                  
                      public DynamoDBEntry ToEntry(object value)
                      {
                          var jsonString = JsonConvert.SerializeObject(value);
                          DynamoDBEntry ret = new Primitive(jsonString);
                          return ret;
                      }
                  }
                  

                  然后像这样注释您的属性:

                  Then annotate your property like this:

                  [DynamoDBProperty(typeof(DataConverter))]
                  public object data { get; set; }
                  

                  这篇关于使用 .NET SDK 在 DynamoDB 中持久化动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <legend id='lehVQ'><style id='lehVQ'><dir id='lehVQ'><q id='lehVQ'></q></dir></style></legend>
                • <small id='lehVQ'></small><noframes id='lehVQ'>

                • <i id='lehVQ'><tr id='lehVQ'><dt id='lehVQ'><q id='lehVQ'><span id='lehVQ'><b id='lehVQ'><form id='lehVQ'><ins id='lehVQ'></ins><ul id='lehVQ'></ul><sub id='lehVQ'></sub></form><legend id='lehVQ'></legend><bdo id='lehVQ'><pre id='lehVQ'><center id='lehVQ'></center></pre></bdo></b><th id='lehVQ'></th></span></q></dt></tr></i><div id='lehVQ'><tfoot id='lehVQ'></tfoot><dl id='lehVQ'><fieldset id='lehVQ'></fieldset></dl></div>
                    • <bdo id='lehVQ'></bdo><ul id='lehVQ'></ul>

                        <tbody id='lehVQ'></tbody>
                          <tfoot id='lehVQ'></tfoot>