Example #1
0
        private static void AddNavigationBindings(NavigationSourceConfiguration configuration,
                                                  EdmNavigationSource navigationSource,
                                                  NavigationSourceLinkBuilderAnnotation linkBuilder,
                                                  ODataModelBuilder builder,
                                                  Dictionary <Type, IEdmType> edmTypeMap,
                                                  Dictionary <string, EdmNavigationSource> edmNavigationSourceMap)
        {
            foreach (EntityTypeConfiguration entityType in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType))
            {
                foreach (NavigationPropertyConfiguration navigationProperty in entityType.NavigationProperties)
                {
                    NavigationPropertyBindingConfiguration binding = configuration.FindBinding(navigationProperty);
                    bool isContained = navigationProperty.ContainsTarget;
                    if (binding != null || isContained)
                    {
                        EdmEntityType          edmEntityType         = edmTypeMap[entityType.ClrType] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties()
                                                                       .Single(np => np.Name == navigationProperty.Name);

                        if (!isContained)
                        {
                            navigationSource.AddNavigationTarget(
                                edmNavigationProperty,
                                edmNavigationSourceMap[binding.TargetNavigationSource.Name]);
                        }

                        NavigationLinkBuilder linkBuilderFunc = configuration.GetNavigationPropertyLink(navigationProperty);
                        if (linkBuilderFunc != null)
                        {
                            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                        }
                    }
                }
            }
        }
        /// <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>
        /// <param name="bindingPath">The binding path.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                         NavigationSourceConfiguration targetNavigationSource, IList <MemberInfo> bindingPath)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

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

            if (bindingPath == null || !bindingPath.Any())
            {
                throw Error.ArgumentNull("bindingPath");
            }

            VerifyBindingPath(navigationConfiguration, bindingPath);

            string path = bindingPath.ConvertBindingPath();

            Dictionary <string, NavigationPropertyBindingConfiguration> navigationPropertyBindingMap;
            NavigationPropertyBindingConfiguration navigationPropertyBinding;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBindingMap))
            {
                if (navigationPropertyBindingMap.TryGetValue(path, out navigationPropertyBinding))
                {
                    if (navigationPropertyBinding.TargetNavigationSource != targetNavigationSource)
                    {
                        throw Error.NotSupported(SRResources.RebindingNotSupported);
                    }
                }
                else
                {
                    navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration,
                                                                                           targetNavigationSource, bindingPath);
                    _navigationPropertyBindings[navigationConfiguration][path] = navigationPropertyBinding;
                }
            }
            else
            {
                _navigationPropertyBindings[navigationConfiguration] =
                    new Dictionary <string, NavigationPropertyBindingConfiguration>();
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration,
                                                                                       targetNavigationSource, bindingPath);
                _navigationPropertyBindings[navigationConfiguration][path] = navigationPropertyBinding;
            }

            return(navigationPropertyBinding);
        }
Example #3
0
        private static string ConvertBindingPath(EdmTypeMap edmMap, NavigationPropertyBindingConfiguration binding)
        {
            IList <string> bindings = new List <string>();

            foreach (var bindingInfo in binding.Path)
            {
                Type         typeCast     = bindingInfo as Type;
                PropertyInfo propertyInfo = bindingInfo as PropertyInfo;

                if (typeCast != null)
                {
                    IEdmType edmType = edmMap.EdmTypes[typeCast];
                    bindings.Add(edmType.FullTypeName());
                }
                else if (propertyInfo != null)
                {
                    bindings.Add(edmMap.EdmProperties[propertyInfo].Name);
                }
            }

            return(String.Join("/", bindings));
        }
Example #4
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,
                                                                         INavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

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

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;

            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy,
                                     declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBinding))
            {
                if (navigationPropertyBinding.TargetNavigationSource != targetNavigationSource)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetNavigationSource);
                _navigationPropertyBindings[navigationConfiguration] = navigationPropertyBinding;
            }

            return(navigationPropertyBinding);
        }
Example #5
0
        private static void AddNavigationBindings(EntitySetConfiguration configuration, EdmEntitySet entitySet, EntitySetLinkBuilderAnnotation linkBuilder, ODataModelBuilder builder,
                                                  Dictionary <Type, IEdmType> edmTypeMap, Dictionary <string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (EntityTypeConfiguration entity in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType))
            {
                foreach (NavigationPropertyConfiguration navigation in entity.NavigationProperties)
                {
                    NavigationPropertyBindingConfiguration binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType          edmEntityType         = edmTypeMap[entity.ClrType] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);

                        NavigationLinkBuilder linkBuilderFunc = configuration.GetNavigationPropertyLink(navigation);
                        if (linkBuilderFunc != null)
                        {
                            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                        }
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// Binds the given navigation property to the target entity set.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetEntitySet">The target entity set.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration, EntitySetConfiguration targetEntitySet)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

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

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;

            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy, declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding = null;

            if (_entitySetBindings.ContainsKey(navigationConfiguration))
            {
                navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
                if (navigationPropertyBinding.EntitySet != targetEntitySet)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetEntitySet);
                _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
            }
            return(navigationPropertyBinding);
        }
        /// <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,
            INavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

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

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;
            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy,
                    declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding;
            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBinding))
            {
                if (navigationPropertyBinding.TargetNavigationSource != targetNavigationSource)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetNavigationSource);
                _navigationPropertyBindings[navigationConfiguration] = navigationPropertyBinding;
            }

            return navigationPropertyBinding;
        }
        /// <summary>
        /// Binds the given navigation property to the target entity set.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetEntitySet">The target entity set.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration, EntitySetConfiguration targetEntitySet)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

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

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;
            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy, declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding = null;
            if (_entitySetBindings.ContainsKey(navigationConfiguration))
            {
                navigationPropertyBinding = _entitySetBindings[navigationConfiguration];
                if (navigationPropertyBinding.EntitySet != targetEntitySet)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetEntitySet);
                _entitySetBindings[navigationConfiguration] = navigationPropertyBinding;
            }
            return navigationPropertyBinding;
        }