Example #1
0
        public static string GetTableName(this Base entity)
        {
            var type = entity.GetType();

            var attributes = type.GetCustomAttributes(true).OfType <DynamoDBTableAttribute>().ToList();

            if (attributes.Count != 1)
            {
                TableKeyAttributeException ex = new TableKeyAttributeException(type);
                throw ex;
            }

            return(attributes.First().TableName);
        }
Example #2
0
        public static Dictionary <string, AttributeValue> GetKey(this Base entity, object hashKeyValue = null, object rangeKeyValue = null)
        {
            var keys = new Dictionary <string, AttributeValue>();

            var type       = entity.GetType();
            var properties = type.GetProperties();
            var hashKeys   = properties.Where(property => Attribute.IsDefined(property, typeof(DynamoDBHashKeyAttribute)));
            var rangeKeys  = properties.Where(property => Attribute.IsDefined(property, typeof(DynamoDBRangeKeyAttribute)));

            if (!hashKeys.Any() && !rangeKeys.Any())
            {
                TableKeyAttributeException ex = new TableKeyAttributeException(type);
                throw ex;
            }
            else if (!hashKeys.Any())
            {
                TableKeyAttributeException ex = new TableKeyAttributeException(type, EnumKeyType.Hash);
                throw ex;
            }
            else if (!rangeKeys.Any())
            {
                TableKeyAttributeException ex = new TableKeyAttributeException(type, EnumKeyType.Range);
                throw ex;
            }


            if (hashKeys.Count() == 1)
            {
                var propertyInfo  = hashKeys.First();
                var attributeName = propertyInfo.Name;

                keys.Add(attributeName, hashKeyValue != null
                    ? AttributeValueConverter.ConvertToAttributeValue[propertyInfo.PropertyType](hashKeyValue)
                    : AttributeValueConverter.ConvertToAttributeValue[propertyInfo.PropertyType](propertyInfo.GetValue(entity)));
            }

            if (rangeKeys.Count() == 1)
            {
                var propertyInfo  = rangeKeys.First();
                var attributeName = propertyInfo.Name;

                keys.Add(attributeName, rangeKeyValue != null
                    ? AttributeValueConverter.ConvertToAttributeValue[propertyInfo.PropertyType](rangeKeyValue)
                    : AttributeValueConverter.ConvertToAttributeValue[propertyInfo.PropertyType](propertyInfo.GetValue(entity)));
            }

            return(keys);
        }