private static object DocumentToObject(Type objectType, ItemStorage storage) { if (storage == null) { throw new ArgumentNullException(); } if (storage.Document == null) { return(null); } object instance = EntityUtils.Instantiate(objectType); PopulateInstance(storage, instance); return(instance); }
private static void PopulateItemStorage(object toStore, ItemStorage storage, bool keysOnly) { 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.MemberType, value, propertyStorage.Converter); if (ShouldSave(dbe)) { if (propertyStorage.IsHashKey || propertyStorage.IsRangeKey || propertyStorage.IsVersion) { 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); } } }
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); }
// 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); }
// 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); } } }
// Deserializing DynamoDB document into an object private static T DocumentToObject <T>(ItemStorage storage) { Type type = typeof(T); return((T)DocumentToObject(type, storage)); }