Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumTypeConfiguration"/> class.
        /// </summary>
        public EnumTypeConfiguration(ODataModelBuilder builder, Type clrType)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }
            if (clrType == null)
            {
                throw Error.ArgumentNull("clrType");
            }

            if (!TypeHelper.IsEnum(clrType))
            {
                throw Error.Argument("clrType", SRResources.TypeCannotBeEnum, clrType.FullName);
            }

            ClrType        = clrType;
            IsFlags        = TypeHelper.AsMemberInfo(clrType).GetCustomAttributes(typeof(FlagsAttribute), false).Any();
            UnderlyingType = Enum.GetUnderlyingType(clrType);
            ModelBuilder   = builder;
            _name          = clrType.EdmName();

            // Use the namespace if one was provided in builder by the user, otherwise fallback to CLR Namespace.
            // If CLR Namespace is null we fallback to "Default"
            // This can still be overriden by using DataContract attribute.
            _namespace = builder.HasAssignedNamespace ? builder.Namespace : clrType.Namespace ?? builder.Namespace;

            ExplicitMembers = new Dictionary <Enum, EnumMemberConfiguration>();
            RemovedMembers  = new List <Enum>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes the property from the edm type.
        /// </summary>
        /// <param name="edmProperty">The property being removed.</param>
        /// <param name="structuralTypeConfiguration">The edm type from which the property is being removed.</param>
        /// <param name="attribute">The <see cref="Attribute"/> found on this type.</param>
        /// <param name="model">The ODataConventionModelBuilder used to build the model.</param>
        public override void Apply(PropertyConfiguration edmProperty,
                                   StructuralTypeConfiguration structuralTypeConfiguration,
                                   Attribute attribute,
                                   ODataConventionModelBuilder model)
        {
            if (structuralTypeConfiguration == null)
            {
                throw Error.ArgumentNull("structuralTypeConfiguration");
            }

            if (edmProperty == null)
            {
                throw Error.ArgumentNull("edmProperty");
            }

            if (!edmProperty.AddedExplicitly)
            {
                bool isTypeDataContract   = TypeHelper.AsMemberInfo(structuralTypeConfiguration.ClrType).GetCustomAttributes(typeof(DataContractAttribute), inherit: true).Any();
                bool isPropertyDataMember = edmProperty.PropertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), inherit: true).Any();

                if (isTypeDataContract && isPropertyDataMember)
                {
                    // both Datamember and IgnoreDataMember. DataMember wins as this a DataContract
                    return;
                }
                else
                {
                    structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumTypeConfiguration"/> class.
        /// </summary>
        public EnumTypeConfiguration(ODataModelBuilder builder, Type clrType)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }
            if (clrType == null)
            {
                throw Error.ArgumentNull("clrType");
            }

            if (!TypeHelper.IsEnum(clrType))
            {
                throw Error.Argument("clrType", SRResources.TypeCannotBeEnum, clrType.FullName);
            }

            ClrType         = clrType;
            IsFlags         = TypeHelper.AsMemberInfo(clrType).GetCustomAttributes(typeof(FlagsAttribute), false).Any();
            UnderlyingType  = Enum.GetUnderlyingType(clrType);
            ModelBuilder    = builder;
            _name           = clrType.EdmName();
            _namespace      = clrType.Namespace ?? DefaultNamespace;
            ExplicitMembers = new Dictionary <Enum, EnumMemberConfiguration>();
            RemovedMembers  = new List <Enum>();
        }
        public NavigationPropertyBindingConfiguration HasManyBinding <TTargetType, TDerivedEntityType>(
            Expression <Func <TDerivedEntityType, IEnumerable <TTargetType> > > navigationExpression, string entitySetName)
            where TTargetType : class
            where TDerivedEntityType : class, TEntityType
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

            if (String.IsNullOrEmpty(entitySetName))
            {
                throw Error.ArgumentNullOrEmpty("entitySetName");
            }

            EntityTypeConfiguration <TDerivedEntityType> derivedEntityType =
                _modelBuilder.EntityType <TDerivedEntityType>().DerivesFrom <TEntityType>();

            NavigationPropertyConfiguration navigation = derivedEntityType.HasMany(navigationExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>
            {
                TypeHelper.AsMemberInfo(typeof(TDerivedEntityType)),
                navigation.PropertyInfo
            };

            return(this.Configuration.AddBinding(navigation,
                                                 _modelBuilder.EntitySet <TTargetType>(entitySetName)._configuration, bindingPath));
        }
        public NavigationPropertyBindingConfiguration HasSingletonBinding <TTargetType, TDerivedEntityType>(
            Expression <Func <TDerivedEntityType, TTargetType> > navigationExpression,
            NavigationSourceConfiguration <TTargetType> targetSingleton)
            where TTargetType : class
            where TDerivedEntityType : class, TEntityType
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

            if (targetSingleton == null)
            {
                throw Error.ArgumentNull("targetSingleton");
            }

            EntityTypeConfiguration <TDerivedEntityType> derivedEntityType =
                _modelBuilder.EntityType <TDerivedEntityType>().DerivesFrom <TEntityType>();

            NavigationPropertyConfiguration navigation = derivedEntityType.HasRequired(navigationExpression);
            IList <MemberInfo> bindingPath             = new List <MemberInfo>
            {
                TypeHelper.AsMemberInfo(typeof(TDerivedEntityType)),
                navigation.PropertyInfo
            };

            return(this.Configuration.AddBinding(navigation, targetSingleton.Configuration, bindingPath));
        }
        public BindingPathConfiguration <TTargetType> HasSinglePath <TTargetType, TDerivedType>(
            Expression <Func <TDerivedType, TTargetType> > pathExpression,
            bool required,
            bool contained)
            where TTargetType : class
            where TDerivedType : class, TStructuralType
        {
            if (pathExpression == null)
            {
                throw Error.ArgumentNull("pathExpression");
            }

            PropertyInfo pathProperty = PropertySelectorVisitor.GetSelectedProperty(pathExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(TypeHelper.AsMemberInfo(typeof(TDerivedType)));
            bindingPath.Add(pathProperty);

            // make sure the derived type has the same type kind with the resource type.
            StructuralTypeConfiguration <TDerivedType> derivedConfiguration;

            if (_structuralType.Configuration.Kind == EdmTypeKind.Entity)
            {
                derivedConfiguration = _modelBuilder.EntityType <TDerivedType>().DerivesFrom <TStructuralType>();
            }
            else
            {
                derivedConfiguration = _modelBuilder.ComplexType <TDerivedType>().DerivesFrom <TStructuralType>();
            }

            StructuralTypeConfiguration <TTargetType> target;

            if (contained)
            {
                target = _modelBuilder.EntityType <TTargetType>();

                if (required)
                {
                    derivedConfiguration.ContainsRequired(pathExpression);
                }
                else
                {
                    derivedConfiguration.ContainsOptional(pathExpression);
                }
            }
            else
            {
                target = _modelBuilder.ComplexType <TTargetType>();
                derivedConfiguration.ComplexProperty(pathExpression).OptionalProperty = !required;
            }

            return(new BindingPathConfiguration <TTargetType>(_modelBuilder, target, _navigationSource, bindingPath));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Applies the convention.
        /// </summary>
        /// <param name="edmTypeConfiguration">The edm type to apply the convention to.</param>
        /// <param name="model">The model that this edm type belongs to.</param>
        public void Apply(TEdmTypeConfiguration edmTypeConfiguration, ODataConventionModelBuilder model)
        {
            if (edmTypeConfiguration == null)
            {
                throw Error.ArgumentNull("edmTypeConfiguration");
            }

            foreach (Attribute attribute in GetAttributes(TypeHelper.AsMemberInfo(edmTypeConfiguration.ClrType)))
            {
                Apply(edmTypeConfiguration, model, attribute);
            }
        }
Ejemplo n.º 8
0
        public override void Apply(PropertyConfiguration edmProperty,
                                   StructuralTypeConfiguration structuralTypeConfiguration,
                                   Attribute attribute,
                                   ODataConventionModelBuilder model)
        {
            if (structuralTypeConfiguration == null)
            {
                throw Error.ArgumentNull("structuralTypeConfiguration");
            }

            if (edmProperty == null)
            {
                throw Error.ArgumentNull("edmProperty");
            }

            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            bool isTypeDataContract        = TypeHelper.AsMemberInfo(structuralTypeConfiguration.ClrType).GetCustomAttributes(typeof(DataContractAttribute), inherit: true).Any();
            DataMemberAttribute dataMember = attribute as DataMemberAttribute;

            if (isTypeDataContract && dataMember != null && !edmProperty.AddedExplicitly)
            {
                // set the name alias
                if (model.ModelAliasingEnabled &&
                    !String.IsNullOrWhiteSpace(dataMember.Name))
                {
                    edmProperty.Name = dataMember.Name;
                }

                StructuralPropertyConfiguration structuralProperty = edmProperty as StructuralPropertyConfiguration;
                if (structuralProperty != null)
                {
                    structuralProperty.NullableProperty = !dataMember.IsRequired;
                }

                NavigationPropertyConfiguration navigationProperty = edmProperty as NavigationPropertyConfiguration;
                if (navigationProperty != null && navigationProperty.Multiplicity != EdmMultiplicity.Many)
                {
                    if (dataMember.IsRequired)
                    {
                        navigationProperty.Required();
                    }
                    else
                    {
                        navigationProperty.Nullable();
                    }
                }
            }
        }
        private static void FindAllNavigationPropertiesRecursive(this ODataModelBuilder builder,
                                                                 StructuralTypeConfiguration configuration,
                                                                 IList <Tuple <StructuralTypeConfiguration, IList <MemberInfo>, NavigationPropertyConfiguration> > navigations,
                                                                 Stack <MemberInfo> path,
                                                                 HashSet <Type> typesAlreadyProcessed)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (navigations == null)
            {
                throw Error.ArgumentNull("navigations");
            }

            if (path == null)
            {
                throw Error.ArgumentNull("path");
            }

            IEnumerable <StructuralTypeConfiguration> thisAndBaseTypes = configuration.ThisAndBaseTypes();

            foreach (var config in thisAndBaseTypes)
            {
                builder.FindNavigationProperties(config, navigations, path, typesAlreadyProcessed);
            }

            IEnumerable <StructuralTypeConfiguration> derivedTypes = builder.DerivedTypes(configuration);

            foreach (var config in derivedTypes)
            {
                if (path.OfType <Type>().Any(p => p == config.ClrType))
                {
                    continue;
                }

                path.Push(TypeHelper.AsMemberInfo(config.ClrType));

                builder.FindNavigationProperties(config, navigations, path, typesAlreadyProcessed);

                path.Pop();
            }
        }
        public NavigationPropertyBindingConfiguration HasOptionalBinding <TTargetType, TDerivedType>(
            Expression <Func <TDerivedType, TTargetType> > navigationExpression,
            string targetEntitySet)
            where TTargetType : class
            where TDerivedType : class, TStructuralType
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

            if (String.IsNullOrEmpty(targetEntitySet))
            {
                throw Error.ArgumentNullOrEmpty("targetEntitySet");
            }

            StructuralTypeConfiguration <TDerivedType> derivedConfiguration;

            if (this._structuralType.Configuration.Kind == EdmTypeKind.Entity)
            {
                derivedConfiguration = _modelBuilder.EntityType <TDerivedType>().DerivesFrom <TStructuralType>();
            }
            else
            {
                derivedConfiguration = _modelBuilder.ComplexType <TDerivedType>().DerivesFrom <TStructuralType>();
            }

            NavigationPropertyConfiguration navigation = derivedConfiguration.HasOptional(navigationExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(TypeHelper.AsMemberInfo(typeof(TDerivedType)));
            bindingPath.Add(navigation.PropertyInfo);

            NavigationSourceConfiguration entitySet = _modelBuilder.EntitySet <TTargetType>(targetEntitySet).Configuration;

            return(this._navigationSource.AddBinding(navigation, entitySet, bindingPath));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Binds the given navigation property to the target navigation source.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetNavigationSource">The target navigation source.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                         NavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetNavigationSource == null)
            {
                throw Error.ArgumentNull("targetNavigationSource");
            }

            IList <MemberInfo> bindingPath = new List <MemberInfo> {
                navigationConfiguration.PropertyInfo
            };

            if (navigationConfiguration.DeclaringType != EntityType)
            {
                bindingPath.Insert(0, TypeHelper.AsMemberInfo(navigationConfiguration.DeclaringType.ClrType));
            }

            return(AddBinding(navigationConfiguration, targetNavigationSource, bindingPath));
        }