Esempio n. 1
0
        private static void ExpandGenericNavigationActionsForAllNavigationProperties(IEnumerable <HttpActionDescriptor> navigationActions,
                                                                                     List <HttpActionDescriptor> expandedDescriptors,
                                                                                     List <HttpActionDescriptor> removeDescriptors,
                                                                                     Func <string, string> actionNameBuilder)
        {
            foreach (HttpActionDescriptor navigationAction in navigationActions)
            {
                ReflectedHttpActionDescriptor reflectedHttpActionDescriptor = navigationAction as ReflectedHttpActionDescriptor;
                if ((reflectedHttpActionDescriptor != null) &&
                    reflectedHttpActionDescriptor.MethodInfo.IsGenericMethodDefinition &&
                    reflectedHttpActionDescriptor.MethodInfo.GetGenericArguments().Length == 1)
                {
                    // Lookup the EntitySet metadata for the controller
                    IContainerMetadata containerMetadata = reflectedHttpActionDescriptor.ControllerDescriptor.GetContainerMetadata();
                    if (containerMetadata != null)
                    {
                        IEntitySetMetadata entitySetMetadata = containerMetadata.GetEntitySet(reflectedHttpActionDescriptor.ControllerDescriptor.ControllerName);
                        if (entitySetMetadata != null)
                        {
                            foreach (IEntityTypeMetadata entityTypeMetadata in entitySetMetadata.ElementTypeHierarchyMetadata)
                            {
                                // Foreach NavigationProperty in all of the entity types, add a new HttpActionDescriptor
                                foreach (var edmNavigationProperty in entityTypeMetadata.EdmType.DeclaredProperties.OfType <IEdmNavigationProperty>())
                                {
                                    IEdmEntityType      toEntityType         = edmNavigationProperty.ToEntityType();
                                    IEntityTypeMetadata propertyTypeMetadata = containerMetadata.GetEntityType(toEntityType);

                                    Type       tProperty          = propertyTypeMetadata.ClrType;
                                    MethodInfo genericMethod      = reflectedHttpActionDescriptor.MethodInfo.MakeGenericMethod(tProperty);
                                    string     expandedActionName = actionNameBuilder(edmNavigationProperty.Name);
                                    expandedDescriptors.Add(new RenamedReflectedHttpActionDescriptor(reflectedHttpActionDescriptor.ControllerDescriptor,
                                                                                                     genericMethod,
                                                                                                     expandedActionName));
                                }
                            }
                        }
                    }

                    removeDescriptors.Add(reflectedHttpActionDescriptor);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add an entity set controller of type <paramref name="controllerType"/> for the specified <paramref name="entitySetName"/> and <paramref name="entityType"/>.
        /// </summary>
        /// <param name="entitySetName"></param>
        /// <param name="entityType"></param>
        /// <param name="controllerType"></param>
        public void AddEntitySetController(string entitySetName, Type entityType, Type controllerType)
        {
            Contract.Requires <ArgumentException>(!string.IsNullOrWhiteSpace(entitySetName));
            Contract.Requires <ArgumentNullException>(entityType != null);
            Contract.Requires <ArgumentNullException>(controllerType != null);
            Contract.Requires <ArgumentException>(RequiredBaseController.IsAssignableFrom(controllerType), "Controller types must derive from System.Web.Http.OData.ODataController");

            IEntitySetMetadata entitySetMetadata = _containerMetadata.GetEntitySet(entitySetName);

            if (entitySetMetadata == null)
            {
                throw new ArgumentException(string.Format("EntitySet named '{0}' not found in container.", entitySetName));
            }
            if (entitySetMetadata.ElementTypeMetadata.ClrType != entityType)
            {
                throw new ArgumentException(string.Format("EntitySet named '{0}' should have type {1}; {2} was passed in.", entitySetName, entitySetMetadata.ElementTypeMetadata.ClrType, entityType));
            }

            var controllerDescriptor = new HttpControllerDescriptor(_webApiConfig, entitySetName, controllerType);

            _controllerSelector.AddController(entitySetName, controllerDescriptor);
        }