Ejemplo n.º 1
0
        /// <summary>
        /// Gets the resource of the given type that the query points to
        /// </summary>
        /// <param name="query">query pointing to a particular resource</param>
        /// <param name="fullTypeName">full type name i.e. Namespace qualified type name of the resource</param>
        /// <returns>object representing a resource of given type and as referenced by the query</returns>
        /// <remarks>This method should obtain a single result from the specified <paramref name="query"/>. It should fail if no or more than one result
        /// can be obtain by evaluating such query.
        /// The result should then be converted to its resource "handle" and that handle should be returned from the method.
        /// The <paramref name="fullTypeName"/> is the expected FullName of the resource type of the resource to be retrieved. If this parameter is null
        /// the method should ignore it. If it's not null, the method should check that the resource returned by the query is of this resource type
        /// and fail if that's not the case.</remarks>
        public object GetResource(System.Linq.IQueryable query, string fullTypeName)
        {
            // Since we're not using resource handles we're going to return the resource itself.
            object resource = null;

            foreach (object r in query)
            {
                if (resource != null)
                {
                    throw new ArgumentException(String.Format("Invalid Uri specified. The query '{0}' must refer to a single resource", query.ToString()));
                }

                resource = r;
            }

            if (resource != null)
            {
                if (fullTypeName != null)
                {
                    ResourceType resourceType;
                    if (!this.metadata.TryResolveResourceType(fullTypeName, out resourceType))
                    {
                        throw new ArgumentException("Unknown resource type '" + fullTypeName + "'.");
                    }

                    if (resource.GetType() != resourceType.InstanceType)
                    {
                        throw new System.ArgumentException(String.Format("Invalid uri specified. ExpectedType: '{0}', ActualType: '{1}'", fullTypeName, resource.GetType().FullName));
                    }
                }

                return(resource);
            }

            return(null);
        }
        /// <summary>
        /// Gets the resource of the specified type identified by a query and type name.
        /// </summary>
        /// <param name="query">Language integrated query (LINQ) pointing to a particular resource.</param>
        /// <param name="fullTypeName">The fully qualified type name of resource.</param>
        /// <returns>
        /// An opaque object representing a resource of the specified type, referenced by the specified query.
        /// </returns>
        public virtual object GetResource(System.Linq.IQueryable query, string fullTypeName)
        {
            if (query == null)
            {
                throw new ArgumentException("The specified IQueryable is null.");
            }

            object resource = null;

            foreach (object r in query)
            {
                if (resource != null)
                {
                    throw new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, "Invalid Uri specified. The query '{0}' must refer to a single resource", query.ToString()));
                }

                resource = r;
            }

            if (resource != null)
            {
                Type resourceType = resource.GetType();
                if (fullTypeName != null)
                {
                    if (resourceType.FullName != fullTypeName)
                    {
                        throw new ArgumentException("Unknown resource type '" + fullTypeName + "'.");
                    }
                }

                this.session.GetTable(resourceType, this.GetTableId(resourceType)).SetSubmitAction(resource, SubmitAction.PossibleUpdate);

                return(resource);
            }

            return(null);
        }