예제 #1
0
        /// <summary>
        /// Serializes an object to a Document.
        /// </summary>
        /// <typeparam name="T">Type to serialize as.</typeparam>
        /// <param name="value">Object to serialize.</param>
        /// <returns>Document with attributes populated from object.</returns>
        public Document ToDocument <T>(T value)
        {
            if (value == null)
            {
                return(null);
            }

            ItemStorage storage = ObjectToItemStorage <T>(value, false, false);

            if (storage == null)
            {
                return(null);
            }

            return(storage.Document);
        }
예제 #2
0
        /// <summary>
        /// Serializes an object to a Document.
        /// </summary>
        /// <typeparam name="T">Type to serialize as.</typeparam>
        /// <param name="value">Object to serialize.</param>
        /// <param name="operationConfig">Config object which can be used to override the table used.</param>
        /// <returns>Document with attributes populated from object.</returns>
        public Document ToDocument <T>(T value, DynamoDBOperationConfig operationConfig)
        {
            if (value == null)
            {
                return(null);
            }

            DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
            ItemStorage        storage    = ObjectToItemStorage <T>(value, false, flatConfig);

            if (storage == null)
            {
                return(null);
            }

            return(storage.Document);
        }
예제 #3
0
        private T LoadHelper <T>(Key key, DynamoDBFlatConfig flatConfig, ItemStorageConfig storageConfig, bool isAsync)
        {
            GetItemOperationConfig getConfig = new GetItemOperationConfig
            {
                ConsistentRead  = flatConfig.ConsistentRead.Value,
                AttributesToGet = storageConfig.AttributesToGet
            };

            Table       table   = GetTargetTable(storageConfig, flatConfig);
            ItemStorage storage = new ItemStorage(storageConfig);

            storage.Document = table.GetItemHelper(key, getConfig, isAsync);

            T instance = DocumentToObject <T>(storage, flatConfig);

            return(instance);
        }
예제 #4
0
        private static object DocumentToObject(Type objectType, ItemStorage storage)
        {
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }

            if (storage.Document == null)
            {
                return(null);
            }

            object instance = Utils.Instantiate(objectType);

            PopulateInstance(storage, instance);
            return(instance);
        }
예제 #5
0
        private object DocumentToObject(Type objectType, ItemStorage storage, DynamoDBFlatConfig flatConfig)
        {
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }

            if (storage.Document == null)
            {
                return(null);
            }

            object instance = Utils.InstantiateConverter(objectType, this);

            PopulateInstance(storage, instance, flatConfig);
            return(instance);
        }
예제 #6
0
        private async Task <T> LoadHelperAsync <T>(Key key, DynamoDBFlatConfig flatConfig, ItemStorageConfig storageConfig, CancellationToken cancellationToken)
        {
            GetItemOperationConfig getConfig = new GetItemOperationConfig
            {
                ConsistentRead  = flatConfig.ConsistentRead.Value,
                AttributesToGet = storageConfig.AttributesToGet
            };

            Table       table   = GetTargetTable(storageConfig, flatConfig);
            ItemStorage storage = new ItemStorage(storageConfig);

            storage.Document = await table.GetItemHelperAsync(key, getConfig, cancellationToken).ConfigureAwait(false);

            T instance = DocumentToObject <T>(storage, flatConfig);

            return(instance);
        }
예제 #7
0
        private static Document CreateExpectedDocumentForVersion(ItemStorage storage)
        {
            Document document = new Document();

            if (storage.Config.HasVersion)
            {
                string versionAttributeName = storage.Config.VersionPropertyStorage.AttributeName;
                if (storage.CurrentVersion == null)
                {
                    document[versionAttributeName] = null;
                }
                else
                {
                    document[versionAttributeName] = storage.CurrentVersion;
                }
            }
            return(document);
        }
예제 #8
0
        // Serializing an object into a DynamoDB document
        private static ItemStorage ObjectToItemStorage <T>(T toStore, bool keysOnly, bool ignoreNullValues)
        {
            if (toStore == null)
            {
                return(null);
            }

            Type objectType          = typeof(T);
            ItemStorageConfig config = ItemStorageConfigCache.GetConfig(objectType);

            if (config == null)
            {
                return(null);
            }

            ItemStorage storage = ObjectToItemStorage <T>(toStore, keysOnly, ignoreNullValues, config);

            return(storage);
        }
예제 #9
0
        // Serializing an object into a DynamoDB document
        private ItemStorage ObjectToItemStorage <T>(T toStore, bool keysOnly, DynamoDBFlatConfig flatConfig)
        {
            if (toStore == null)
            {
                return(null);
            }

            Type objectType = typeof(T);

            ItemStorageConfig config = StorageConfigCache.GetConfig(objectType, flatConfig);

            if (config == null)
            {
                return(null);
            }

            ItemStorage storage = ObjectToItemStorage <T>(toStore, keysOnly, flatConfig.IgnoreNullValues.Value, config);

            return(storage);
        }
예제 #10
0
        // Searching
        private IEnumerable <T> FromSearch <T>(Search search)
        {
            if (search == null)
            {
                throw new ArgumentNullException("search");
            }

            ItemStorageConfig storageConfig = ItemStorageConfigCache.GetConfig <T>();

            while (!search.IsDone)
            {
                List <Document> set = search.GetNextSet();
                foreach (var document in set)
                {
                    ItemStorage storage = new ItemStorage(storageConfig);
                    storage.Document = document;
                    T instance = DocumentToObject <T>(storage);
                    yield return(instance);
                }
            }
        }
예제 #11
0
        private void SaveHelper <T>(T value, DynamoDBOperationConfig operationConfig, bool isAsync)
        {
            if (value == null)
            {
                return;
            }

            DynamoDBFlatConfig currentConfig = new DynamoDBFlatConfig(operationConfig, this.config);
            ItemStorage        storage       = ObjectToItemStorage <T>(value, false, currentConfig.IgnoreNullValues.Value);

            if (storage == null)
            {
                return;
            }

            Table table = GetTargetTable(storage.Config, currentConfig);

            if (
                (currentConfig.SkipVersionCheck.HasValue && currentConfig.SkipVersionCheck.Value) ||
                !storage.Config.HasVersion)
            {
                table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), null, isAsync);
            }
            else
            {
                Document expectedDocument = CreateExpectedDocumentForVersion(storage);
                SetNewVersion(storage);
                table.UpdateHelper(
                    storage.Document,
                    table.MakeKey(storage.Document),
                    new UpdateItemOperationConfig {
                    Expected = expectedDocument, ReturnValues = ReturnValues.None
                },
                    true);
                PopulateInstance(storage, value);
            }
        }
예제 #12
0
        private async Task SaveHelperAsync <T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
        {
            if (value == null)
            {
                return;
            }

            DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
            ItemStorage        storage    = ObjectToItemStorage(value, false, flatConfig);

            if (storage == null)
            {
                return;
            }

            Table table = GetTargetTable(storage.Config, flatConfig);

            if (
                (flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) ||
                !storage.Config.HasVersion)
            {
                await table.UpdateHelperAsync(storage.Document, table.MakeKey(storage.Document), null, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                Document expectedDocument = CreateExpectedDocumentForVersion(storage);
                SetNewVersion(storage);
                await table.UpdateHelperAsync(
                    storage.Document,
                    table.MakeKey(storage.Document),
                    new UpdateItemOperationConfig { Expected = expectedDocument, ReturnValues = ReturnValues.None },
                    cancellationToken).ConfigureAwait(false);

                PopulateInstance(storage, value, flatConfig);
            }
        }
예제 #13
0
        // Deserializing DynamoDB document into an object
        private T DocumentToObject <T>(ItemStorage storage, DynamoDBFlatConfig flatConfig)
        {
            Type type = typeof(T);

            return((T)DocumentToObject(type, storage, flatConfig));
        }
예제 #14
0
        private static void PopulateItemStorage(object toStore, bool keysOnly, bool ignoreNullValues, ItemStorage storage)
        {
            ItemStorageConfig config   = storage.Config;
            Document          document = storage.Document;

            foreach (PropertyStorage propertyStorage in config.AllPropertyStorage)
            {
                // if only keys are being serialized, skip non-key properties
                // still include version, however, to populate the storage.CurrentVersion field
                if (keysOnly && !propertyStorage.IsHashKey && !propertyStorage.IsRangeKey && !propertyStorage.IsVersion)
                {
                    continue;
                }

                string propertyName  = propertyStorage.PropertyName;
                string attributeName = propertyStorage.AttributeName;

                object value;
                if (TryGetValue(toStore, propertyStorage.Member, out value))
                {
                    DynamoDBEntry dbe = ToDynamoDBEntry(propertyStorage, value);

                    if (ShouldSave(dbe, ignoreNullValues))
                    {
                        if (propertyStorage.IsHashKey || propertyStorage.IsRangeKey || propertyStorage.IsVersion || propertyStorage.IsLSIRangeKey)
                        {
                            if (dbe is PrimitiveList)
                            {
                                throw new InvalidOperationException("Property " + propertyName + " is a hash key, range key or version property and cannot be PrimitiveList");
                            }
                        }
                        document[attributeName] = dbe;

                        if (propertyStorage.IsVersion)
                        {
                            storage.CurrentVersion = dbe as Primitive;
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("Unable to retrieve value from property " + propertyName);
                }
            }
        }
예제 #15
0
        // Deserializing DynamoDB document into an object
        private static T DocumentToObject <T>(ItemStorage storage)
        {
            Type type = typeof(T);

            return((T)DocumentToObject(type, storage));
        }