internal Uri GenerateLocationHeader() { Contract.Assert(_controller.ContainerMetadata != null); Type clrType = Entity.GetType(); Type baseType = clrType.BaseType; IEntitySetMetadata entitySetMetadata = _controller.ContainerMetadata.GetEntitySetFor(clrType); while (entitySetMetadata == null && baseType != null) { entitySetMetadata = _controller.ContainerMetadata.GetEntitySetFor(baseType); baseType = baseType.BaseType; } if (entitySetMetadata == null) { throw new InvalidOperationException("IEntitySetMetadata not found for entity type " + clrType.FullName); } IEntityTypeMetadata entityTypeMetadata = _controller.ContainerMetadata.GetEntityType(clrType); if (entityTypeMetadata == null) { throw new InvalidOperationException("IEntityTypeMetadata not found for entity type " + clrType.FullName); } object keyValue = entityTypeMetadata.SingleClrKeyProperty.GetValue(Entity); string keyString = ODataUriUtils.ConvertToUriLiteral(keyValue, ODataVersion.V3, Request.ODataProperties().Model); string oDataLink = _controller.Url.CreateODataLink(new EntitySetPathSegment(entitySetMetadata.Name), new KeyValuePathSegment(keyString)); return(new Uri(oDataLink)); }
internal NavigationMetadata(IEdmNavigationProperty edmNavigationProperty, IEntitySetMetadata targetEntitySet) { Contract.Assert(edmNavigationProperty != null); Contract.Assert(targetEntitySet != null); EdmNavigationProperty = edmNavigationProperty; TargetEntitySet = targetEntitySet; }
protected EntitySetController(IContainerMetadata containerMetadata, ODataValidationSettings queryValidationSettings, ODataQuerySettings querySettings) { Contract.Requires<ArgumentNullException>(containerMetadata != null); Contract.Requires<ArgumentNullException>(queryValidationSettings != null); Contract.Requires<ArgumentNullException>(querySettings != null); _entitySetMetadata = containerMetadata.GetEntitySetFor(typeof(TEntity)); if (_entitySetMetadata == null) { throw new InvalidOperationException("EntitySet not found in containerMetadata for type " + typeof(TEntity)); } _queryValidationSettings = queryValidationSettings; _querySettings = querySettings; }
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); } } }
/// <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); }
public IEdmEntitySet FindEntitySet(string setName) { IEntitySetMetadata esm = this.GetEntitySet(setName); return(esm == null ? null : esm.EdmEntitySet); }