Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseCollectionResult" /> class.
        /// </summary>
        /// <param name="query">The query that returns a collection of objects.</param>
        /// <param name="edmType">The EDM type reference of the objects.</param>
        /// <param name="context">The context where the action is executed.</param>
        protected BaseCollectionResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
            : base(edmType, context)
        {
            Ensure.NotNull(query, "query");

            this.Query = query;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseSingleResult" /> class.
        /// </summary>
        /// <param name="query">The query that returns an object.</param>
        /// <param name="edmType">The EDM type reference of the object.</param>
        /// <param name="context">The context where the action is executed.</param>
        protected BaseSingleResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
            : base(edmType, context)
        {
            Ensure.NotNull(query, "query");

            this.Result = query.SingleOrDefault();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseResult" /> class.
        /// </summary>
        /// <param name="edmType">The EDM type reference of the OData result.</param>
        /// <param name="context">The context where the action is executed.</param>
        protected BaseResult(IEdmTypeReference edmType, ApiContext context)
        {
            Ensure.NotNull(edmType, "edmType");
            Ensure.NotNull(context, "context");

            this.edmType = edmType;
            this.Context = context;
        }
        /// <summary>
        /// Applies initialization routines from any API configurator
        /// attributes specified on an API type to an API context.
        /// </summary>
        /// <param name="type">
        /// An API type.
        /// </param>
        /// <param name="instance">
        /// An API instance, if applicable.
        /// </param>
        /// <param name="context">
        /// An API context.
        /// </param>
        public static void ApplyInitialization(
            Type type, object instance, ApiContext context)
        {
            Ensure.NotNull(type, "type");
            Ensure.NotNull(context, "context");
            if (type.BaseType != null)
            {
                ApiConfiguratorAttribute.ApplyInitialization(
                    type.BaseType, instance, context);
            }

            var attributes = type.GetCustomAttributes(
                typeof(ApiConfiguratorAttribute), false);
            foreach (ApiConfiguratorAttribute attribute in attributes)
            {
                attribute.Initialize(context, type, instance);
            }
        }
Exemplo n.º 5
0
 public bool TryGetRelevantType(ApiContext context, string name, out Type relevantType)
 {
     if (name == "Products")
     {
         relevantType = typeof(Product);
     }
     else if (name == "Customers")
     {
         relevantType = typeof(Customer);
     }
     else if (name == "Stores")
     {
         relevantType = typeof(Store);
     }
     else
     {
         relevantType = null;
     }
     
     return true;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Tries to get the relevant type of an entity
        /// set, singleton, or composable function import.
        /// </summary>
        /// <param name="context">
        /// An API context.
        /// </param>
        /// <param name="name">
        /// The name of an entity set, singleton or composable function import.
        /// </param>
        /// <param name="relevantType">
        /// When this method returns, provides the
        /// relevant type of the queryable source.
        /// </param>
        /// <returns>
        /// <c>true</c> if the relevant type was
        /// provided; otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetRelevantType(
            ApiContext context,
            string name,
            out Type relevantType)
        {
            // Cannot await as cannot make method async
            var model = context.GetModelAsync().Result;
            var element = model.EntityContainer.Elements.Where(e => e.Name == name).FirstOrDefault();

            if (element != null)
            {
                IEdmType entityType = null;
                var entitySet = element as EdmEntitySet;
                if (entitySet != null)
                {
                    var entitySetType = entitySet.Type as EdmCollectionType;
                    entityType = entitySetType.ElementType.Definition;
                }
                else
                {
                    var singleton = element as EdmSingleton;
                    if (singleton != null)
                    {
                        entityType = singleton.Type;
                    }
                }

                if (entityType != null)
                {
                    ClrTypeAnnotation annotation = model.GetAnnotationValue<ClrTypeAnnotation>(entityType);
                    if (annotation != null)
                    {
                        relevantType = annotation.ClrType;
                        return true;
                    }
                }
            }

            return InnerMapper.TryGetRelevantType(context, name, out relevantType);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Tries to get the relevant type of an entity
        /// set, singleton, or composable function import.
        /// </summary>
        /// <param name="context">
        /// An API context.
        /// </param>
        /// <param name="name">
        /// The name of an entity set, singleton or composable function import.
        /// </param>
        /// <param name="relevantType">
        /// When this method returns, provides the
        /// relevant type of the queryable source.
        /// </param>
        /// <returns>
        /// <c>true</c> if the relevant type was
        /// provided; otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetRelevantType(
            ApiContext context,
            string name,
            out Type relevantType)
        {
            // TODO GitHubIssue#39 : support something beyond entity sets
            relevantType = null;
            var property = this.dbContextType.GetProperty(name);
            if (property != null)
            {
                var type = property.PropertyType;
#if EF7
                var genericType = type.FindGenericType(typeof(DbSet<>));
#else
                var genericType = type.FindGenericType(typeof(IDbSet<>));
#endif
                if (genericType != null)
                {
                    relevantType = genericType.GetGenericArguments()[0];
                }
            }

            return relevantType != null;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntityResult" /> class.
 /// </summary>
 /// <param name="query">The query that returns an entity.</param>
 /// <param name="edmType">The EDM type reference of the entity.</param>
 /// <param name="context">The context where the action is executed.</param>
 public EntityResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
     : base(query, edmType, context)
 {
 }
Exemplo n.º 9
0
 /// <summary>
 /// Releases the unmanaged resources that are used by the
 /// object and, optionally, releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 /// <c>true</c> to release both managed and unmanaged resources;
 /// <c>false</c> to release only unmanaged resources.
 /// </param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.apiContext = null;
         this.IsDisposed = true;
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Applies disposal routines from any API configurator
        /// attributes specified on an API type to an API context.
        /// </summary>
        /// <param name="type">
        /// An API type.
        /// </param>
        /// <param name="instance">
        /// An API instance, if applicable.
        /// </param>
        /// <param name="context">
        /// An API context.
        /// </param>
        public static void ApplyDisposal(
            Type type, object instance, ApiContext context)
        {
            Ensure.NotNull(type, "type");
            Ensure.NotNull(context, "context");
            var attributes = type.GetCustomAttributes(
                typeof(ApiConfiguratorAttribute), false);
            foreach (ApiConfiguratorAttribute attribute in attributes.Reverse())
            {
                attribute.Dispose(context, type, instance);
            }

            if (type.BaseType != null)
            {
                ApiConfiguratorAttribute.ApplyDisposal(
                    type.BaseType, instance, context);
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Releases the unmanaged resources that are used by the
 /// object and, optionally, releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 /// <c>true</c> to release both managed and unmanaged resources;
 /// <c>false</c> to release only unmanaged resources.
 /// </param>
 protected virtual void Dispose(bool disposing)
 {
     this.IsDisposed = true;
     if (this.apiContext != null)
     {
         this.apiContext.DisposeScope();
         this.apiContext = null;
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Disposes an API context.
 /// </summary>
 /// <param name="context">
 /// An API context.
 /// </param>
 /// <param name="type">
 /// The API type on which this attribute was placed.
 /// </param>
 /// <param name="instance">
 /// An API instance, if applicable.
 /// </param>
 public virtual void Dispose(
     ApiContext context,
     Type type,
     object instance)
 {
 }
Exemplo n.º 13
0
 public bool TryGetRelevantType(ApiContext context, string namespaceName, string name, out Type relevantType)
 {
     relevantType = typeof(Product);
     return true;
 }
Exemplo n.º 14
0
        public bool TryGetRelevantType(ApiContext context, string name, out Type relevantType)
        {
            relevantType = name == "Person" ? typeof(Person) : typeof(Order);

            return true;
        }
Exemplo n.º 15
0
            /// <inheritdoc/>
            public bool TryGetRelevantType(
                ApiContext context,
                string name,
                out Type relevantType)
            {
                if (this.InnerModelMapper != null &&
                    this.InnerModelMapper.TryGetRelevantType(context, name, out relevantType))
                {
                    return true;
                }

                relevantType = null;
                var entitySetProperty = this.ModelCache.entitySetProperties.SingleOrDefault(p => p.Name == name);
                if (entitySetProperty != null)
                {
                    relevantType = entitySetProperty.PropertyType.GetGenericArguments()[0];
                }

                if (relevantType == null)
                {
                    var singletonProperty = this.ModelCache.singletonProperties.SingleOrDefault(p => p.Name == name);
                    if (singletonProperty != null)
                    {
                        relevantType = singletonProperty.PropertyType;
                    }
                }

                return relevantType != null;
            }
Exemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceSetResult" /> class.
 /// </summary>
 /// <param name="query">The query that returns a collection of resources.</param>
 /// <param name="edmType">The EDM type reference of the entities or complex.</param>
 /// <param name="context">The context where the action is executed.</param>
 public ResourceSetResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
     : base(query, edmType, context)
 {
 }
Exemplo n.º 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InvocationContext" /> class.
        /// </summary>
        /// <param name="apiContext">
        /// An API context.
        /// </param>
        public InvocationContext(ApiContext apiContext)
        {
            Ensure.NotNull(apiContext, "apiContext");

            this.ApiContext = apiContext;
        }
Exemplo n.º 18
0
            /// <inheritdoc/>
            public bool TryGetRelevantType(
                ApiContext context,
                string namespaceName,
                string name,
                out Type relevantType)
            {
                if (this.InnerModelMapper != null &&
                    this.InnerModelMapper.TryGetRelevantType(context, namespaceName, name, out relevantType))
                {
                    return true;
                }

                relevantType = null;
                return false;
            }
Exemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NonEntityCollectionResult" /> class.
 /// </summary>
 /// <param name="query">The query that returns a collection of non-entity values.</param>
 /// <param name="edmType">The EDM type reference of the values.</param>
 /// <param name="context">The context where the action is executed.</param>
 public NonEntityCollectionResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
     : base(query, edmType, context)
 {
 }
Exemplo n.º 20
0
 /// <summary>
 /// Tries to get the relevant type of a composable function.
 /// </summary>
 /// <param name="context">
 /// An API context.
 /// </param>
 /// <param name="namespaceName">
 /// The name of a namespace containing a composable function.
 /// </param>
 /// <param name="name">
 /// The name of composable function.
 /// </param>
 /// <param name="relevantType">
 /// When this method returns, provides the
 /// relevant type of the composable function.
 /// </param>
 /// <returns>
 /// <c>true</c> if the relevant type was
 /// provided; otherwise, <c>false</c>.
 /// </returns>
 public bool TryGetRelevantType(
     ApiContext context,
     string namespaceName,
     string name,
     out Type relevantType)
 {
     // TODO GitHubIssue#39 : support composable function imports
     relevantType = null;
     return false;
 }
Exemplo n.º 21
0
 public bool TryGetRelevantType(ApiContext context, string namespaceName, string name, out Type relevantType)
 {
     return TryGetRelevantType(context, name, out relevantType);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Initializes an API context.
 /// </summary>
 /// <param name="context">
 /// An API context.
 /// </param>
 /// <param name="type">
 /// The API type on which this attribute was placed.
 /// </param>
 /// <param name="instance">
 /// An API instance, if applicable.
 /// </param>
 public virtual void Initialize(
     ApiContext context,
     Type type,
     object instance)
 {
 }
Exemplo n.º 23
0
        /// <summary>
        /// Performs application-defined tasks associated with
        /// freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (this.IsDisposed)
            {
                return;
            }

            this.IsDisposed = true;

            if (this.apiContext != null)
            {
                this.apiContext = null;
            }

            GC.SuppressFinalize(this);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InvocationContext" /> class.
        /// </summary>
        /// <param name="apiContext">
        /// An API context.
        /// </param>
        public InvocationContext(ApiContext apiContext)
        {
            Ensure.NotNull(apiContext, "apiContext");

            this.ApiContext = apiContext;
        }