void ConvertODataActionParametersToTypedModel(IModelTypeBuilder modelTypeBuilder, IEdmAction action, string controllerName)
        {
            Contract.Requires(modelTypeBuilder != null);
            Contract.Requires(action != null);
            Contract.Requires(controllerName != null);

            var apiVersion = new Lazy <ApiVersion>(() => EdmModel.GetAnnotationValue <ApiVersionAnnotation>(EdmModel).ApiVersion);

            for (var i = 0; i < ParameterDescriptions.Count; i++)
            {
                var description = ParameterDescriptions[i];
                var parameter   = description.ParameterDescriptor;

                if (parameter != null && parameter.ParameterType.IsODataActionParameters())
                {
                    description.ParameterDescriptor = new ODataModelBoundParameterDescriptor(parameter, modelTypeBuilder.NewActionParameters(serviceProvider, action, apiVersion.Value, controllerName));
                    break;
                }
            }
        }
        internal ODataRouteBuilderContext(
            HttpConfiguration configuration,
            ApiVersion apiVersion,
            ODataRoute route,
            HttpActionDescriptor actionDescriptor,
            IList <ApiParameterDescription> parameterDescriptions,
            IModelTypeBuilder modelTypeBuilder,
            ODataApiExplorerOptions options)
        {
            ApiVersion            = apiVersion;
            Services              = configuration.GetODataRootContainer(route);
            EdmModel              = Services.GetRequiredService <IEdmModel>();
            routeAttribute        = actionDescriptor.GetCustomAttributes <ODataRouteAttribute>().FirstOrDefault();
            RouteTemplate         = routeAttribute?.PathTemplate;
            Route                 = route;
            ActionDescriptor      = actionDescriptor;
            ParameterDescriptions = parameterDescriptions;
            Options               = options;
            UrlKeyDelimiter       = UrlKeyDelimiterOrDefault(configuration.GetUrlKeyDelimiter() ?? Services.GetService <IODataPathHandler>()?.UrlKeyDelimiter);

            var container = EdmModel.EntityContainer;

            if (container == null)
            {
                IsRouteExcluded = true;
                return;
            }

            EntitySet = container.FindEntitySet(actionDescriptor.ControllerDescriptor.ControllerName);
            Operation = container.FindOperationImports(actionDescriptor.ActionName).FirstOrDefault()?.Operation ??
                        EdmModel.FindDeclaredOperations(container.Namespace + "." + actionDescriptor.ActionName).FirstOrDefault();
            ActionType = GetActionType(EntitySet, Operation);

            if (Operation?.IsAction() == true)
            {
                ConvertODataActionParametersToTypedModel(modelTypeBuilder, (IEdmAction)Operation, actionDescriptor.ControllerDescriptor.ControllerName);
            }
        }
Example #3
0
        internal ClassProperty(IServiceProvider services, IEnumerable <Assembly> assemblies, IEdmOperationParameter parameter, IModelTypeBuilder typeBuilder)
        {
            Contract.Requires(services != null);
            Contract.Requires(assemblies != null);
            Contract.Requires(parameter != null);
            Contract.Requires(typeBuilder != null);

            Name = parameter.Name;
            var context = new TypeSubstitutionContext(services, assemblies, typeBuilder);

            if (parameter.Type.IsCollection())
            {
                var collectionType  = parameter.Type.AsCollection();
                var elementType     = collectionType.ElementType().Definition.GetClrType(assemblies);
                var substitutedType = elementType.SubstituteIfNecessary(context);

                Type = typeof(IEnumerable <>).MakeGenericType(substitutedType);
            }
            else
            {
                var parameterType = parameter.Type.Definition.GetClrType(assemblies);

                Type = parameterType.SubstituteIfNecessary(context);
            }

            Attributes = AttributesFromOperationParameter(parameter);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
 /// </summary>
 /// <param name="serviceProvider">The <see cref="IServiceProvider">service provider</see> that the
 /// <see cref="IEdmModel">EDM model</see> can be resolved from.</param>
 /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
 public TypeSubstitutionContext(IServiceProvider serviceProvider, IModelTypeBuilder modelTypeBuilder)
 {
     model            = new Lazy <IEdmModel>(serviceProvider.GetRequiredService <IEdmModel>);
     apiVersion       = new Lazy <ApiVersion>(() => Model.GetAnnotationValue <ApiVersionAnnotation>(Model)?.ApiVersion ?? ApiVersion.Default);
     ModelTypeBuilder = modelTypeBuilder;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
 /// </summary>
 /// <param name="model">The <see cref="IEdmModel">EDM model</see> to compare against.</param>
 /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
 public TypeSubstitutionContext(IEdmModel model, IModelTypeBuilder modelTypeBuilder)
 {
     this.model       = new Lazy <IEdmModel>(() => model);
     apiVersion       = new Lazy <ApiVersion>(() => Model.GetAnnotationValue <ApiVersionAnnotation>(Model)?.ApiVersion ?? ApiVersion.Default);
     ModelTypeBuilder = modelTypeBuilder;
 }
Example #6
0
        internal ClassProperty(IServiceProvider services, IEdmOperationParameter parameter, IModelTypeBuilder typeBuilder)
        {
            Name = parameter.Name;

            var context  = new TypeSubstitutionContext(services, typeBuilder);
            var edmModel = services.GetRequiredService <IEdmModel>();

            if (parameter.Type.IsCollection())
            {
                var collectionType  = parameter.Type.AsCollection();
                var elementType     = collectionType.ElementType().Definition.GetClrType(edmModel) !;
                var substitutedType = elementType.SubstituteIfNecessary(context);

                Type = typeof(IEnumerable <>).MakeGenericType(substitutedType);
            }
            else
            {
                var parameterType = parameter.Type.Definition.GetClrType(edmModel) !;

                Type = parameterType.SubstituteIfNecessary(context);
            }

            Attributes = AttributesFromOperationParameter(parameter).ToArray();
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
        /// </summary>
        /// <param name="serviceProvider">The <see cref="IServiceProvider">service provider</see> that the
        /// <see cref="IEdmModel">EDM model</see> can be resolved from.</param>
        /// <param name="assemblies">The <see cref="IEnumerable{T}">sequence</see> of application <see cref="Assembly">assemblies</see>.</param>
        /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
        public TypeSubstitutionContext(IServiceProvider serviceProvider, IEnumerable <Assembly> assemblies, IModelTypeBuilder modelTypeBuilder)
        {
            Arg.NotNull(serviceProvider, nameof(serviceProvider));
            Arg.NotNull(assemblies, nameof(assemblies));
            Arg.NotNull(modelTypeBuilder, nameof(modelTypeBuilder));

            model            = new Lazy <IEdmModel>(serviceProvider.GetRequiredService <IEdmModel>);
            apiVersion       = new Lazy <ApiVersion>(() => Model.GetAnnotationValue <ApiVersionAnnotation>(Model)?.ApiVersion ?? ApiVersion.Default);
            Assemblies       = assemblies;
            ModelTypeBuilder = modelTypeBuilder;
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel">EDM model</see> to compare against.</param>
        /// <param name="assemblies">The <see cref="IEnumerable{T}">sequence</see> of application <see cref="Assembly">assemblies</see>.</param>
        /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
        public TypeSubstitutionContext(IEdmModel model, IEnumerable <Assembly> assemblies, IModelTypeBuilder modelTypeBuilder)
        {
            Arg.NotNull(model, nameof(model));
            Arg.NotNull(assemblies, nameof(assemblies));
            Arg.NotNull(modelTypeBuilder, nameof(modelTypeBuilder));

            this.model       = new Lazy <IEdmModel>(() => model);
            apiVersion       = new Lazy <ApiVersion>(() => Model.GetAnnotationValue <ApiVersionAnnotation>(Model)?.ApiVersion ?? ApiVersion.Default);
            Assemblies       = assemblies;
            ModelTypeBuilder = modelTypeBuilder;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
 /// </summary>
 /// <param name="serviceProvider">The <see cref="IServiceProvider">service provider</see> that the
 /// <see cref="IEdmModel">EDM model</see> can be resolved from.</param>
 /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
 /// <param name="apiVersion">The current <see cref="ApiVersion">API version</see>.</param>
 public TypeSubstitutionContext(IServiceProvider serviceProvider, IModelTypeBuilder modelTypeBuilder, ApiVersion apiVersion)
 {
     this.apiVersion      = apiVersion;
     this.serviceProvider = serviceProvider;
     ModelTypeBuilder     = modelTypeBuilder;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeSubstitutionContext"/> class.
 /// </summary>
 /// <param name="model">The <see cref="IEdmModel">EDM model</see> to compare against.</param>
 /// <param name="modelTypeBuilder">The associated <see cref="IModelTypeBuilder">model type builder</see>.</param>
 public TypeSubstitutionContext(IEdmModel model, IModelTypeBuilder modelTypeBuilder)
 {
     this.model       = model;
     ModelTypeBuilder = modelTypeBuilder;
 }