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

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

            NavigationPropertyConfiguration navigation = this._structuralType.HasOptional(navigationExpression);

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

            bindingPath.Add(navigation.PropertyInfo);

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

            return(this._navigationSource.AddBinding(navigation, entitySet, bindingPath));
        }
Ejemplo n.º 2
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);
                        }
                    }
                }
            }
        }
        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>
            {
                typeof(TDerivedEntityType),
                navigation.PropertyInfo
            };

            return(this.Configuration.AddBinding(navigation, targetSingleton.Configuration, bindingPath));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BindingPathConfiguration{TStructuralType}"/> class.
        /// </summary>
        /// <param name="modelBuilder">The model builder.</param>
        /// <param name="structuralType">The type configuration.</param>
        /// <param name="navigationSource">The navigation source configuration.</param>
        /// <param name="bindingPath">The binding path.</param>
        public BindingPathConfiguration(ODataModelBuilder modelBuilder,
                                        StructuralTypeConfiguration <TStructuralType> structuralType,
                                        NavigationSourceConfiguration navigationSource,
                                        IList <MemberInfo> bindingPath)
        {
            if (modelBuilder == null)
            {
                throw Error.ArgumentNull("modelBuilder");
            }

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

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

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

            _modelBuilder     = modelBuilder;
            _navigationSource = navigationSource;
            _structuralType   = structuralType;
            _bindingPath      = bindingPath;
        }
Ejemplo n.º 5
0
        private static void AddCoreVocabularyAnnotations(this EdmModel model, IEnumerable <NavigationSourceAndAnnotations> navigationSources, EdmTypeMap edmTypeMap)
        {
            Contract.Assert(model != null);
            Contract.Assert(edmTypeMap != null);

            if (navigationSources == null)
            {
                return;
            }

            foreach (NavigationSourceAndAnnotations source in navigationSources)
            {
                IEdmVocabularyAnnotatable navigationSource = source.NavigationSource as IEdmVocabularyAnnotatable;
                if (navigationSource == null)
                {
                    continue;
                }

                NavigationSourceConfiguration navigationSourceConfig = source.Configuration as NavigationSourceConfiguration;
                if (navigationSourceConfig == null)
                {
                    continue;
                }

                model.AddOptimisticConcurrencyAnnotation(navigationSource, navigationSourceConfig.EntityType, edmTypeMap);
            }
        }
Ejemplo n.º 6
0
        private static void AddOptimisticConcurrencyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target,
                                                               NavigationSourceConfiguration navigationSourceConfiguration, EdmTypeMap edmTypeMap)
        {
            EntityTypeConfiguration entityTypeConfig = navigationSourceConfiguration.EntityType;

            IEnumerable <StructuralPropertyConfiguration> concurrencyPropertyies =
                entityTypeConfig.Properties.OfType <StructuralPropertyConfiguration>().Where(property => property.ConcurrencyToken);

            IList <IEdmStructuralProperty> edmProperties = new List <IEdmStructuralProperty>();

            foreach (StructuralPropertyConfiguration property in concurrencyPropertyies)
            {
                IEdmProperty value;
                if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
                {
                    var item = value as IEdmStructuralProperty;
                    if (item != null)
                    {
                        edmProperties.Add(item);
                    }
                }
            }

            if (edmProperties.Any())
            {
                IEdmCollectionExpression collectionExpression = new EdmCollectionExpression(edmProperties.Select(p => new EdmPropertyPathExpression(p.Name)).ToArray());
                IEdmTerm term = Microsoft.OData.Edm.Vocabularies.V1.CoreVocabularyModel.ConcurrencyTerm;
                EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(target, term, collectionExpression);
                annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
                model.SetVocabularyAnnotation(annotation);
            }
        }
Ejemplo n.º 7
0
        private static void AddNavigationBindings(EdmTypeMap edmMap,
                                                  NavigationSourceConfiguration navigationSourceConfiguration,
                                                  EdmNavigationSource navigationSource,
                                                  NavigationSourceLinkBuilderAnnotation linkBuilder,
                                                  Dictionary <string, EdmNavigationSource> edmNavigationSourceMap)
        {
            foreach (var binding in navigationSourceConfiguration.Bindings)
            {
                NavigationPropertyConfiguration navigationProperty = binding.NavigationProperty;
                bool isContained = navigationProperty.ContainsTarget;

                IEdmType               edmType               = edmMap.EdmTypes[navigationProperty.DeclaringType.ClrType];
                IEdmStructuredType     structuraType         = edmType as IEdmStructuredType;
                IEdmNavigationProperty edmNavigationProperty = structuraType.NavigationProperties()
                                                               .Single(np => np.Name == navigationProperty.Name);

                string bindingPath = ConvertBindingPath(edmMap, binding);
                if (!isContained)
                {
                    // calculate the binding path
                    navigationSource.AddNavigationTarget(
                        edmNavigationProperty,
                        edmNavigationSourceMap[binding.TargetNavigationSource.Name],
                        new EdmPathExpression(bindingPath));
                }

                NavigationLinkBuilder linkBuilderFunc = navigationSourceConfiguration.GetNavigationPropertyLink(navigationProperty);
                if (linkBuilderFunc != null)
                {
                    linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Constructs an instance of an <see cref="NavigationSourceLinkBuilderAnnotation" /> from an <see cref="NavigationSourceConfiguration" />.
        /// </summary>
        public NavigationSourceLinkBuilderAnnotation(NavigationSourceConfiguration navigationSource)
        {
            if (navigationSource == null)
            {
                throw Error.ArgumentNull("navigationSource");
            }

            _idLinkBuilder   = navigationSource.GetIdLink();
            _editLinkBuilder = navigationSource.GetEditLink();
            _readLinkBuilder = navigationSource.GetReadLink();
        }
        /// <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);
        }
        private void ApplyNavigationSourceConventions(NavigationSourceConfiguration navigationSourceConfiguration)
        {
            if (!_configuredNavigationSources.Contains(navigationSourceConfiguration))
            {
                _configuredNavigationSources.Add(navigationSourceConfiguration);

                foreach (INavigationSourceConvention convention in _conventions.OfType <INavigationSourceConvention>())
                {
                    if (convention != null)
                    {
                        convention.Apply(navigationSourceConfiguration, this);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public NavigationPropertyBindingConfiguration HasManyBinding <TTargetType>(
            Expression <Func <TEntityType, IEnumerable <TTargetType> > > navigationExpression,
            NavigationSourceConfiguration <TTargetType> targetEntitySet) where TTargetType : class
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

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

            return(this.Configuration.AddBinding(EntityType.HasMany(navigationExpression), targetEntitySet.Configuration));
        }
Ejemplo n.º 12
0
        public NavigationPropertyBindingConfiguration HasSingletonBinding <TTargetType>(
            Expression <Func <TEntityType, TTargetType> > navigationExpression,
            NavigationSourceConfiguration <TTargetType> targetSingleton) where TTargetType : class
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

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

            return(this.Configuration.AddBinding(EntityType.HasRequired(navigationExpression), targetSingleton.Configuration));
        }
Ejemplo n.º 13
0
        internal NavigationSourceConfiguration(ODataModelBuilder modelBuilder, NavigationSourceConfiguration configuration)
        {
            if (modelBuilder == null)
            {
                throw Error.ArgumentNull("modelBuilder");
            }

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

            _configuration = configuration;
            _modelBuilder  = modelBuilder;
            _entityType    = new EntityTypeConfiguration <TEntityType>(modelBuilder, _configuration.EntityType);
        }
Ejemplo n.º 14
0
        public NavigationPropertyBindingConfiguration HasManyBinding <TTargetType, TDerivedEntityType>(
            Expression <Func <TDerivedEntityType, IEnumerable <TTargetType> > > navigationExpression,
            NavigationSourceConfiguration <TTargetType> targetEntitySet)
            where TTargetType : class
            where TDerivedEntityType : class, TEntityType
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

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

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

            return(this.Configuration.AddBinding(derivedEntityType.HasMany(navigationExpression), targetEntitySet.Configuration));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationPropertyBindingConfiguration"/> class.
        /// </summary>
        /// <param name="navigationProperty">The navigation property for the binding.</param>
        /// <param name="navigationSource">The target navigation source of the binding.</param>
        /// <param name="path">The path of current binding.</param>
        public NavigationPropertyBindingConfiguration(NavigationPropertyConfiguration navigationProperty,
                                                      NavigationSourceConfiguration navigationSource, IList <MemberInfo> path)
        {
            if (navigationProperty == null)
            {
                throw new ArgumentNullException("navigationProperty");
            }

            if (navigationSource == null)
            {
                throw new ArgumentNullException("navigationSource");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            NavigationProperty     = navigationProperty;
            TargetNavigationSource = navigationSource;
            Path = path;
        }
        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(typeof(TDerivedType));
            bindingPath.Add(navigation.PropertyInfo);

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

            return(this._navigationSource.AddBinding(navigation, entitySet, bindingPath));
        }
        /// <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, navigationConfiguration.DeclaringType.ClrType);
            }

            return(AddBinding(navigationConfiguration, targetNavigationSource, bindingPath));
        }
        /// <summary>
        /// Constructs an instance of an <see cref="NavigationSourceLinkBuilderAnnotation" /> from an <see cref="NavigationSourceConfiguration" />.
        /// </summary>
        public NavigationSourceLinkBuilderAnnotation(NavigationSourceConfiguration navigationSource)
        {
            if (navigationSource == null)
            {
                throw Error.ArgumentNull("navigationSource");
            }

            _navigationSourceName = navigationSource.Name;
            _idLinkBuilder = navigationSource.GetIdLink();
            _editLinkBuilder = navigationSource.GetEditLink();
            _readLinkBuilder = navigationSource.GetReadLink();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BindingPathConfiguration{TStructuralType}"/> class.
 /// </summary>
 /// <param name="modelBuilder">The model builder.</param>
 /// <param name="structuralType">The type configuration.</param>
 /// <param name="navigationSource">The navigation source configuration.</param>
 public BindingPathConfiguration(ODataModelBuilder modelBuilder,
                                 StructuralTypeConfiguration <TStructuralType> structuralType,
                                 NavigationSourceConfiguration navigationSource)
     : this(modelBuilder, structuralType, navigationSource, new List <MemberInfo>())
 {
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NavigationPropertyBindingConfiguration"/> class.
 /// </summary>
 /// <param name="navigationProperty">The navigation property for the binding.</param>
 /// <param name="navigationSource">The target navigation source of the binding.</param>
 public NavigationPropertyBindingConfiguration(NavigationPropertyConfiguration navigationProperty,
                                               NavigationSourceConfiguration navigationSource)
     : this(navigationProperty, navigationSource, new MemberInfo[] { navigationProperty.PropertyInfo })
 {
 }
Ejemplo n.º 21
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>
        /// Constructs an instance of an <see cref="NavigationSourceLinkBuilderAnnotation" /> from an <see cref="NavigationSourceConfiguration" />.
        /// </summary>
        public NavigationSourceLinkBuilderAnnotation(NavigationSourceConfiguration navigationSource)
        {
            if (navigationSource == null)
            {
                throw Error.ArgumentNull("navigationSource");
            }

            _navigationSourceName = navigationSource.Name;
            EntitySetConfiguration entitySet = navigationSource as EntitySetConfiguration;
            if (entitySet != null)
            {
                _feedSelfLinkBuilder = entitySet.GetFeedSelfLink();
            }
            else
            {
                _feedSelfLinkBuilder = null;
            }

            _idLinkBuilder = navigationSource.GetIdLink();
            _editLinkBuilder = navigationSource.GetEditLink();
            _readLinkBuilder = navigationSource.GetReadLink();
        }