Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="api"></param>
        /// <param name="namespaceName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static Type EnsureElementType(this ApiBase api, string namespaceName, string name)
        {
            Type elementType = null;

            var mapper = api.GetApiService <IModelMapper>();

            if (mapper != null)
            {
                var modelContext = new ModelContext(api);
                if (namespaceName == null)
                {
                    mapper.TryGetRelevantType(modelContext, name, out elementType);
                }
                else
                {
                    mapper.TryGetRelevantType(modelContext, namespaceName, name, out elementType);
                }
            }

            if (elementType == null)
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, Resources.ElementTypeNotFound, name));
            }

            return(elementType);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously gets an API model using an API context.
        /// </summary>
        /// <param name="api">An API.</param>
        /// <returns>
        /// A task that represents the asynchronous operation whose result is the API model.
        /// </returns>
        public static IEdmModel GetModel(this ApiBase api)
        {
            Ensure.NotNull(api, nameof(api));

            return(api.GetApiService <IEdmModel>());
            //var registrations = api.GetApiService<IEdmModel>();
            //var entry = registrations.FirstOrDefault(c => c.Value.ApiType == api.GetType());
            //return entry.Value.Model;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Asynchronously gets an API model using an API context.
        /// </summary>
        /// <param name="api">
        /// An API.
        /// </param>
        /// <param name="cancellationToken">
        /// An optional cancellation token.
        /// </param>
        /// <returns>
        /// A task that represents the asynchronous
        /// operation whose result is the API model.
        /// </returns>
        public static async Task <IEdmModel> GetModelAsync(
            this ApiBase api,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNull(api, "api");

            var config = api.Configuration;

            if (config.Model != null)
            {
                return(config.Model);
            }

            var builder = api.GetApiService <IModelBuilder>();

            if (builder == null)
            {
                throw new InvalidOperationException(Resources.ModelBuilderNotRegistered);
            }

            Task <IEdmModel> running;
            var source = config.CompeteModelGeneration(out running);

            if (source == null)
            {
                return(await running);
            }

            try
            {
                var buildContext = new ModelContext(api.ServiceProvider);
                var model        = await builder.GetModelAsync(buildContext, cancellationToken);

                source.SetResult(model);
                return(model);
            }
            catch (AggregateException e)
            {
                source.SetException(e.InnerExceptions);
                throw;
            }
            catch (Exception e)
            {
                source.SetException(e);
                throw;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="api"></param>
 /// <returns></returns>
 private static PropertyBag GetPropertyBag(this ApiBase api)
 {
     Ensure.NotNull(api, nameof(api));
     return(api.GetApiService <PropertyBag>());
 }