/// <summary>
        /// Adds an interface the data type should inherit from
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="addInheritedFields"></param>
        internal void AddSuperInterface(Type interfaceType, bool addInheritedFields)
        {
            if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData))
            {
                return;
            }

            _superInterfaces.Add(interfaceType);

            if (addInheritedFields)
            {
                foreach (PropertyInfo propertyInfo in interfaceType.GetProperties())
                {
                    if (propertyInfo.Name == nameof(IPageData.PageId) && interfaceType == typeof(IPageData))
                    {
                        continue;
                    }

                    DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true);

                    this.Fields.Add(dataFieldDescriptor);
                }
            }

            foreach (string propertyName in interfaceType.GetKeyPropertyNames())
            {
                if (KeyPropertyNames.Contains(propertyName))
                {
                    continue;
                }

                PropertyInfo property = ReflectionBasedDescriptorBuilder.FindProperty(interfaceType, propertyName);

                if (DynamicTypeReflectionFacade.IsKeyField(property))
                {
                    this.KeyPropertyNames.Add(propertyName, false);
                }
            }

            foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType))
            {
                if (!this.DataScopes.Contains(dataScopeIdentifier))
                {
                    this.DataScopes.Add(dataScopeIdentifier);
                }
            }

            var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t));

            foreach (Type superSuperInterfaceType in superInterfaces)
            {
                AddSuperInterface(superSuperInterfaceType, addInheritedFields);
            }
        }
        /// <summary>
        /// Removes a super interface
        /// </summary>
        /// <param name="interfaceType">Type to remove</param>
        public void RemoveSuperInterface(Type interfaceType)
        {
            if (interfaceType == typeof(IData))
            {
                return;
            }

            if (_superInterfaces.Contains(interfaceType))
            {
                _superInterfaces.Remove(interfaceType);
            }

            foreach (PropertyInfo propertyInfo in interfaceType.GetProperties())
            {
                var dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true);

                if (this.Fields.Contains(dataFieldDescriptor))
                {
                    this.Fields.Remove(dataFieldDescriptor);
                }

                if (DynamicTypeReflectionFacade.IsKeyField(propertyInfo) &&
                    this.KeyPropertyNames.Contains(propertyInfo.Name))
                {
                    this.KeyPropertyNames.Remove(propertyInfo.Name);
                }
            }


            foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType))
            {
                if (this.DataScopes.Contains(dataScopeIdentifier))
                {
                    this.DataScopes.Remove(dataScopeIdentifier);
                }
            }

            var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t));

            foreach (Type superInterfaceType in superInterfaces)
            {
                RemoveSuperInterface(superInterfaceType);
            }
        }
Example #3
0
        /// <summary>
        /// Adds an interface the data type should inherit from
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="addInheritedFields"></param>
        internal void AddSuperInterface(Type interfaceType, bool addInheritedFields)
        {
            if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData))
            {
                return;
            }

            _superInterfaces.Add(interfaceType);

            if (addInheritedFields)
            {
                foreach (PropertyInfo propertyInfo in interfaceType.GetProperties())
                {
                    if (propertyInfo.Name == "PageId" && interfaceType == typeof(IPageData))
                    {
                        continue;
                    }

                    DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true);

                    this.Fields.Add(dataFieldDescriptor);
                }
            }

            foreach (string propertyName in interfaceType.GetKeyPropertyNames())
            {
                if (KeyPropertyNames.Contains(propertyName))
                {
                    continue;
                }

                PropertyInfo property = interfaceType.GetProperty(propertyName);
                if (property == null)
                {
                    List <Type> superInterfaces = interfaceType.GetInterfacesRecursively(t => typeof(IData).IsAssignableFrom(t) && t != typeof(IData));

                    foreach (Type superInterface in superInterfaces)
                    {
                        property = superInterface.GetProperty(propertyName);
                        if (property != null)
                        {
                            break;
                        }
                    }
                }

                Verify.IsNotNull(property, "Missing property '{0}' on type '{1}' or one of its interfaces".FormatWith(propertyName, interfaceType));

                if (DynamicTypeReflectionFacade.IsKeyField(property))
                {
                    this.KeyPropertyNames.Add(propertyName, false);
                }
            }

            foreach (DataScopeIdentifier dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType))
            {
                if (!this.DataScopes.Contains(dataScopeIdentifier))
                {
                    this.DataScopes.Add(dataScopeIdentifier);
                }
            }


            foreach (Type superSuperInterfaceType in interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t)))
            {
                AddSuperInterface(superSuperInterfaceType, addInheritedFields);
            }
        }