ImplementsInterface() public static method

public static ImplementsInterface ( Type targetType, Type interfaceType ) : bool
targetType System.Type
interfaceType System.Type
return bool
Exemplo n.º 1
0
        /// <summary>
        /// Validates configurations and sets required fields
        /// </summary>
        public void Validate(DynamoDBContext context)
        {
            if (IsVersion)
            {
                Utils.ValidateVersionType(MemberType);    // no conversion is possible, so type must be a nullable primitive
            }
            if (IsHashKey && IsRangeKey)
            {
                throw new InvalidOperationException("Property " + PropertyName + " cannot be both hash and range key");
            }

            if (ConverterType != null)
            {
                if (!Utils.CanInstantiateConverter(ConverterType) || !Utils.ImplementsInterface(ConverterType, typeof(IPropertyConverter)))
                {
                    throw new InvalidOperationException("Converter for " + PropertyName + " must be instantiable with no parameters and must implement IPropertyConverter");
                }

                this.Converter = Utils.InstantiateConverter(ConverterType, context) as IPropertyConverter;
            }

            IPropertyConverter converter;

            if (context.ConverterCache.TryGetValue(MemberType, out converter) && converter != null)
            {
                this.Converter = converter;
            }

            foreach (var index in Indexes)
            {
                IndexNames.AddRange(index.IndexNames);
            }
        }
Exemplo n.º 2
0
        private static bool IsSupportedDictionaryType(Type type, out ITypeInfo typeWrapper, out Type keyType, out Type valueType)
        {
            keyType     = valueType = null;
            typeWrapper = null;

            // Type must implement both IDictionary<TKey,TValue> and IDictionary
            if (!(Utils.ImplementsInterface(type, typeof(IDictionary <,>)) &&
                  Utils.ImplementsInterface(type, typeof(IDictionary))))
            {
                return(false);
            }

            typeWrapper = TypeFactory.GetTypeInfo(type);
            var genericArguments = typeWrapper.GetGenericArguments();

            if (genericArguments.Length != 2)
            {
                return(false);
            }
            keyType   = genericArguments[0];
            valueType = genericArguments[1];
            if (keyType != typeof(string) || valueType == typeof(object))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates configurations and sets required fields
        /// </summary>
        public void Validate()
        {
            if (IsVersion)
            {
                Utils.ValidateVersionType(MemberType);    // no conversion is possible, so type must be a nullable primitive
            }
            if (IsHashKey && IsRangeKey)
            {
                throw new InvalidOperationException("Property " + PropertyName + " cannot be both hash and range key");
            }

            if (IsKey || IsGSIKey)
            {
                if (Converter == null && !Utils.IsPrimitive(MemberType))
                {
                    throw new InvalidOperationException("Key " + PropertyName + " must be of primitive type");
                }
            }

            foreach (var index in Indexes)
            {
                IndexNames.AddRange(index.IndexNames);
            }

            if (ConverterType != null)
            {
                if (!Utils.CanInstantiate(ConverterType) || !Utils.ImplementsInterface(ConverterType, typeof(IPropertyConverter)))
                {
                    throw new InvalidOperationException("Converter for " + PropertyName + " must be instantiable with no parameters and must implement IPropertyConverter");
                }

                this.Converter = Utils.Instantiate(ConverterType) as IPropertyConverter;
            }
        }
Exemplo n.º 4
0
        private static bool TryToPrimitiveList(Type type, object value, bool canReturnPrimitive, out DynamoDBEntry output)
        {
            var  typeWrapper = TypeFactory.GetTypeInfo(type);
            Type elementType;

            if (!Utils.ImplementsInterface(type, typeof(ICollection <>)) ||
                !Utils.IsPrimitive(elementType = typeWrapper.GetGenericArguments()[0]))
            {
                output = null;
                return(false);
            }

            IEnumerable enumerable = value as IEnumerable;

            Primitive primitive;

            // Strings are collections of chars, don't treat them as collections
            if (enumerable == null || value is string)
            {
                if (canReturnPrimitive &&
                    TypeFactory.GetTypeInfo(value.GetType()).IsAssignableFrom(TypeFactory.GetTypeInfo(elementType)) &&
                    TryToPrimitive(elementType, value, out primitive))
                {
                    output = primitive;
                    return(true);
                }

                output = null;
                return(false);
            }

            PrimitiveList     primitiveList = new PrimitiveList();
            DynamoDBEntryType?listType      = null;

            foreach (var item in enumerable)
            {
                if (TryToPrimitive(elementType, item, out primitive))
                {
                    if (listType.HasValue && listType.Value != primitive.Type)
                    {
                        throw new InvalidOperationException("List cannot contain a mix of different types");
                    }
                    listType = primitive.Type;

                    primitiveList.Entries.Add(primitive);
                }
                else
                {
                    output = null;
                    return(false);
                }
            }
            primitiveList.Type = listType.GetValueOrDefault(DynamoDBEntryType.String);

            output = primitiveList;
            return(true);
        }
        private static bool TryToPrimitiveList(Type type, object value, bool canReturnPrimitive, out DynamoDBEntry output)
        {
            Type elementType;

            if (!Utils.ImplementsInterface(type, typeof(ICollection <>)) ||
                !Utils.IsPrimitive(elementType = type.GetGenericArguments()[0]))
            {
                output = null;
                return(false);
            }

            ICollection collection = value as ICollection;

            Primitive primitive;

            if (collection == null)
            {
                if (canReturnPrimitive &&
                    value.GetType().IsAssignableFrom(elementType) &&
                    TryToPrimitive(elementType, value, out primitive))
                {
                    output = primitive;
                    return(true);
                }

                output = null;
                return(false);
            }

            PrimitiveList     primitiveList = new PrimitiveList();
            DynamoDBEntryType?listType      = null;

            foreach (var item in collection)
            {
                if (TryToPrimitive(elementType, item, out primitive))
                {
                    if (listType.HasValue && listType.Value != primitive.Type)
                    {
                        throw new InvalidOperationException("List cannot contain a mix of different types");
                    }
                    listType = primitive.Type;

                    primitiveList.Entries.Add(primitive);
                }
                else
                {
                    output = null;
                    return(false);
                }
            }
            primitiveList.Type = listType.GetValueOrDefault(DynamoDBEntryType.String);

            output = primitiveList;
            return(true);
        }
Exemplo n.º 6
0
        // PrimitiveList <--> List
        private static bool TryFromPrimitiveList(Type targetType, PrimitiveList value, out object output)
        {
            var  targetTypeWrapper = TypeFactory.GetTypeInfo(targetType);
            Type elementType;

            if ((!Utils.ImplementsInterface(targetType, typeof(ICollection <>)) &&
                 !Utils.ImplementsInterface(targetType, typeof(IList))) ||
                !Utils.CanInstantiate(targetType) ||
                !Utils.IsPrimitive(elementType = targetTypeWrapper.GetGenericArguments()[0]))
            {
                output = null;
                return(false);
            }

            var   collection        = Utils.Instantiate(targetType);
            IList ilist             = collection as IList;
            bool  useIListInterface = ilist != null;

            MethodInfo collectionAdd = null;

            if (!useIListInterface)
            {
                collectionAdd = targetTypeWrapper.GetMethod("Add");
            }

            foreach (Primitive primitive in value.Entries)
            {
                object primitiveValue;
                if (TryFromPrimitive(elementType, primitive, out primitiveValue))
                {
                    if (useIListInterface)
                    {
                        ilist.Add(primitiveValue);
                    }
                    else
                    {
                        collectionAdd.Invoke(collection, new object[] { primitiveValue });
                    }
                }
                else
                {
                    output = null;
                    return(false);
                }
            }

            output = collection;
            return(true);
        }
Exemplo n.º 7
0
        private bool TryFromList(Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, out object output)
        {
            var targetTypeWrapper = TypeFactory.GetTypeInfo(targetType);

            if ((!Utils.ImplementsInterface(targetType, typeof(ICollection <>)) &&
                 !Utils.ImplementsInterface(targetType, typeof(IList))) ||
                !Utils.CanInstantiate(targetType))
            {
                output = null;
                return(false);
            }

            var   elementType       = targetTypeWrapper.GetGenericArguments()[0];
            var   collection        = Utils.Instantiate(targetType);
            IList ilist             = collection as IList;
            bool  useIListInterface = ilist != null;
            var   propertyStorage   = new SimplePropertyStorage(elementType);

            MethodInfo collectionAdd = null;

            if (!useIListInterface)
            {
                collectionAdd = targetTypeWrapper.GetMethod("Add");
            }

            foreach (DynamoDBEntry entry in list.Entries)
            {
                var item = FromDynamoDBEntry(propertyStorage, entry, flatConfig);

                if (useIListInterface)
                {
                    ilist.Add(item);
                }
                else
                {
                    collectionAdd.Invoke(collection, new object[] { item });
                }
            }

            output = collection;
            return(true);
        }
Exemplo n.º 8
0
        private bool TryToList(object value, Type type, DynamoDBFlatConfig flatConfig, out DynamoDBList output)
        {
            var typeWrapper = TypeFactory.GetTypeInfo(type);

            if (!Utils.ImplementsInterface(type, typeof(ICollection <>)))
            {
                output = null;
                return(false);
            }

            IEnumerable enumerable = value as IEnumerable;

            // Strings are collections of chars, don't treat them as collections
            if (enumerable == null || value is string)
            {
                output = null;
                return(false);
            }

            Type elementType = typeWrapper.GetGenericArguments()[0];
            SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType);

            output = new DynamoDBList();
            foreach (var item in enumerable)
            {
                DynamoDBEntry entry;
                if (item == null)
                {
                    entry = DynamoDBNull.Null;
                }
                else
                {
                    entry = ToDynamoDBEntry(propertyStorage, item, flatConfig);
                }

                output.Add(entry);
            }
            return(true);
        }
Exemplo n.º 9
0
        internal static ItemStorageConfig CreateStorageConfig(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var typeWrapper          = TypeFactory.GetTypeInfo(type);
            ItemStorageConfig config = new ItemStorageConfig(type);

            DynamoDBTableAttribute tableAttribute = Utils.GetTableAttribute(type);

            if (tableAttribute == null || string.IsNullOrEmpty(tableAttribute.TableName))
            {
                throw new InvalidOperationException("No DynamoDBTableAttribute on type");
            }

            if (string.IsNullOrEmpty(tableAttribute.TableName))
            {
                throw new InvalidOperationException("DynamoDBTableAttribute.Table is empty or null");
            }
            config.TableName = tableAttribute.TableName;
            config.LowerCamelCaseProperties = tableAttribute.LowerCamelCaseProperties;

            foreach (var member in typeWrapper.GetMembers())
            {
                // filter out non-fields and non-properties
                if (!(member is FieldInfo || member is PropertyInfo))
                {
                    continue;
                }

                // filter out properties that aren't both read and write
                if (!Utils.IsReadWrite(member))
                {
                    continue;
                }

                // prepare basic info
                Type            memberType      = Utils.GetType(member);
                string          attributeName   = GetAccurateCase(config, member.Name);
                string          propertyName    = member.Name;
                PropertyStorage propertyStorage = new PropertyStorage(member, memberType);

                // run through all DDB attributes
                bool ignorePresent = false;
                var  allAttributes = Utils.GetAttributes(member);
                foreach (var attribute in allAttributes)
                {
                    // filter out ignored properties
                    if (attribute is DynamoDBIgnoreAttribute)
                    {
                        ignorePresent |= true;
                    }

                    // if ignore attribute is present, ignore other attributes
                    if (ignorePresent)
                    {
                        continue;
                    }

                    if (attribute is DynamoDBVersionAttribute)
                    {
                        Utils.ValidateVersionType(memberType);    // no conversion is possible, so type must be a nullable primitive

                        propertyStorage.IsVersion = true;
                    }

                    DynamoDBPropertyAttribute propertyAttribute = attribute as DynamoDBPropertyAttribute;
                    if (propertyAttribute != null)
                    {
                        if (!string.IsNullOrEmpty(propertyAttribute.AttributeName))
                        {
                            attributeName = GetAccurateCase(config, propertyAttribute.AttributeName);
                        }

                        if (propertyAttribute is DynamoDBHashKeyAttribute)
                        {
                            if (propertyAttribute.Converter == null && !Utils.IsPrimitive(memberType))
                            {
                                throw new InvalidOperationException("Hash key " + propertyName + " must be of primitive type");
                            }

                            var gsiHashAttribute = propertyAttribute as DynamoDBGlobalSecondaryIndexHashKeyAttribute;
                            if (gsiHashAttribute != null)
                            {
                                propertyStorage.IsGSIHashKey = true;
                                if (gsiHashAttribute.IndexNames == null || gsiHashAttribute.IndexNames.Length == 0)
                                {
                                    throw new InvalidOperationException("Global Secondary Index must not be null or empty");
                                }
                                propertyStorage.Indexes.AddRange(gsiHashAttribute.IndexNames);
                                AddGSIConfigs(config, gsiHashAttribute.IndexNames, propertyName, true);
                            }
                            else
                            {
                                propertyStorage.IsHashKey = true;
                            }
                        }
                        if (propertyAttribute is DynamoDBRangeKeyAttribute)
                        {
                            if (propertyAttribute.Converter == null && !Utils.IsPrimitive(memberType))
                            {
                                throw new InvalidOperationException("Range key " + propertyName + " must be of primitive type");
                            }

                            var gsiRangeAttribute = propertyAttribute as DynamoDBGlobalSecondaryIndexRangeKeyAttribute;
                            if (gsiRangeAttribute != null)
                            {
                                propertyStorage.IsGSIRangeKey = true;
                                if (gsiRangeAttribute.IndexNames == null || gsiRangeAttribute.IndexNames.Length == 0)
                                {
                                    throw new InvalidOperationException("Global Secondary Index must not be null or empty");
                                }
                                propertyStorage.Indexes.AddRange(gsiRangeAttribute.IndexNames);
                                AddGSIConfigs(config, gsiRangeAttribute.IndexNames, propertyName, false);
                            }
                            else
                            {
                                propertyStorage.IsRangeKey = true;
                            }
                        }

                        DynamoDBLocalSecondaryIndexRangeKeyAttribute lsiRangeKeyAttribute = propertyAttribute as DynamoDBLocalSecondaryIndexRangeKeyAttribute;
                        if (lsiRangeKeyAttribute != null)
                        {
                            if (lsiRangeKeyAttribute.IndexNames == null || lsiRangeKeyAttribute.IndexNames.Length == 0)
                            {
                                throw new InvalidOperationException("Local Secondary Index must not be null or empty");
                            }
                            propertyStorage.Indexes.AddRange(lsiRangeKeyAttribute.IndexNames);
                            propertyStorage.IsLSIRangeKey = true;

                            foreach (var index in lsiRangeKeyAttribute.IndexNames)
                            {
                                List <string> properties;
                                if (!config.IndexNameToLSIRangePropertiesMapping.TryGetValue(index, out properties))
                                {
                                    properties = new List <string>();
                                    config.IndexNameToLSIRangePropertiesMapping[index] = properties;
                                }
                                properties.Add(propertyName);
                            }
                        }

                        if (propertyAttribute.Converter != null)
                        {
                            if (!Utils.CanInstantiate(propertyAttribute.Converter) || !Utils.ImplementsInterface(propertyAttribute.Converter, typeof(IPropertyConverter)))
                            {
                                throw new InvalidOperationException("Converter for " + propertyName + " must be instantiable with no parameters and must implement IPropertyConverter");
                            }

                            propertyStorage.Converter = Utils.Instantiate(propertyAttribute.Converter) as IPropertyConverter;
                        }
                    }
                }

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

                // only add property storage if no ignore attribute was present
                if (!ignorePresent)
                {
                    config.AddPropertyStorage(propertyStorage);
                }
            }

            if (config.HashKeyPropertyNames.Count == 0)
            {
                throw new InvalidOperationException("No hash key configured for type " + type.FullName);
            }

            return(config);
        }
Exemplo n.º 10
0
        internal static ItemStorageConfig CreateStorageConfig(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ItemStorageConfig config = new ItemStorageConfig(type);

            DynamoDBTableAttribute tableAttribute = Utils.GetTableAttribute(type);

            if (tableAttribute == null || string.IsNullOrEmpty(tableAttribute.TableName))
            {
                throw new InvalidOperationException("No DynamoDBTableAttribute on type");
            }

            if (string.IsNullOrEmpty(tableAttribute.TableName))
            {
                throw new InvalidOperationException("DynamoDBTableAttribute.Table is empty or null");
            }
            config.TableName = tableAttribute.TableName;
            config.LowerCamelCaseProperties = tableAttribute.LowerCamelCaseProperties;

            foreach (var member in type.GetMembers())
            {
                // filter out non-fields and non-properties
                if (!(member is FieldInfo || member is PropertyInfo))
                {
                    continue;
                }

                // filter out properties that aren't both read and write
                if (!Utils.IsReadWrite(member))
                {
                    continue;
                }

                DynamoDBAttribute attribute = Utils.GetAttribute(member);

                // filter out ignored properties
                if (attribute is DynamoDBIgnoreAttribute)
                {
                    continue;
                }

                Type   memberType    = Utils.GetType(member);
                string attributeName = GetAccurateCase(config, member.Name);
                string propertyName  = member.Name;

                PropertyStorage propertyStorage = new PropertyStorage
                {
                    Member     = member,
                    MemberType = memberType,
                };

                if (attribute is DynamoDBVersionAttribute)
                {
                    Utils.ValidateVersionType(memberType);    // no conversion is possible, so type must be a nullable primitive

                    propertyStorage.IsVersion = true;
                }

                DynamoDBPropertyAttribute propertyAttribute = attribute as DynamoDBPropertyAttribute;
                if (propertyAttribute != null)
                {
                    if (!string.IsNullOrEmpty(propertyAttribute.AttributeName))
                    {
                        attributeName = GetAccurateCase(config, propertyAttribute.AttributeName);
                    }

                    if (propertyAttribute is DynamoDBHashKeyAttribute)
                    {
                        if (propertyAttribute.Converter == null && !Utils.IsPrimitive(memberType))
                        {
                            throw new InvalidOperationException("Hash key " + propertyName + " must be of primitive type");
                        }

                        propertyStorage.IsHashKey = true;
                    }
                    if (propertyAttribute is DynamoDBRangeKeyAttribute)
                    {
                        if (propertyAttribute.Converter == null && !Utils.IsPrimitive(memberType))
                        {
                            throw new InvalidOperationException("Range key " + propertyName + " must be of primitive type");
                        }

                        propertyStorage.IsRangeKey = true;
                    }
                    if (propertyAttribute is DynamoDBLocalSecondaryIndexRangeKeyAttribute)
                    {
                        DynamoDBLocalSecondaryIndexRangeKeyAttribute lsiRangeKeyAttribute = propertyAttribute as DynamoDBLocalSecondaryIndexRangeKeyAttribute;
                        if (lsiRangeKeyAttribute.IndexNames == null || lsiRangeKeyAttribute.IndexNames.Length == 0)
                        {
                            throw new InvalidOperationException("Local Secondary Index must not be null or empty");
                        }
                        propertyStorage.Indexes.AddRange(lsiRangeKeyAttribute.IndexNames);
                    }

                    if (propertyAttribute.Converter != null)
                    {
                        if (!Utils.CanInstantiate(propertyAttribute.Converter) || !Utils.ImplementsInterface(propertyAttribute.Converter, typeof(IPropertyConverter)))
                        {
                            throw new InvalidOperationException("Converter for " + propertyName + " must be instantiable with no parameters and must implement IPropertyConverter");
                        }

                        propertyStorage.Converter = Utils.Instantiate(propertyAttribute.Converter) as IPropertyConverter;
                    }
                }

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

                config.AddPropertyStorage(propertyStorage);
            }

            if (config.HashKeyPropertyNames.Count == 0)
            {
                throw new InvalidOperationException("No hash key configured for type " + type.FullName);
            }

            return(config);
        }