Example #1
0
        /// <summary>
        /// Creates a new instance of ODataObject of the specified type.
        /// </summary>
        /// <remarks>
        /// If Type is Item or
        /// Principal, the appropriate sub-type is instantiated based on ID. This is not kosher from
        /// ODATA perspective: the Type Cast parameter in metadata should be used instead..
        /// The method will use reflection to find the type constructor. Objects must have a
        /// constructor (string, context) or (string, context, string) to be found. Otherwise,
        /// a ODataObject instance is created instead.
        /// </remarks>
        /// <param name="type"></param>
        /// <param name="cast"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ODataObject Create(Type type, string cast = null, ODataObject oDataObject = null, JsonSerializer serializer = null)
        {
            type = FindModelType(type, cast);
            if (oDataObject != null && type == oDataObject.GetType())
            {
                return(oDataObject);
            }

            var obj = InvokeConstructor(type, oDataObject, serializer);

            return(obj);
        }
Example #2
0
        public ODataObject CreateFromMetadata(string metadata, Type knownType, ODataObject odataObject, JsonSerializer serializer)
        {
            var parser = new JsonLightMetadataParser();
            var result = parser.Parse(metadata);

            if (result == null)
            {
                return(null);
            }

            ODataObject o = null;

            if (result.HasFeedEntity)
            {
                var  type = FindModelType(knownType, result.FeedEntity);
                Type specificType;
                if (type.IsGenericType() && type.GetGenericTypeDefinition() == ODataFeedType)
                {
                    specificType = type;
                }
                else
                {
                    specificType = ODataFeedType.MakeGenericType(new[] { type });
                }

                if (specificType == knownType)
                {
                    return(odataObject);
                }
                o = InvokeConstructor(specificType, odataObject, serializer);

                o.SetMetadata(new Uri(result.MetadataBaseUri), result.FeedEntity, null, Models.ODataObjectType.ComplexType);
            }
            else
            {
                string metadataType = result.HasCast ? result.Cast : result.Entity;
                o = Create(knownType, metadataType, odataObject, serializer);
                if (result.HasEntity)
                {
                    o.SetMetadata(new Uri(result.MetadataBaseUri), result.Entity, result.Cast, Models.ODataObjectType.Entity);
                }
                else
                {
                    o.SetMetadata(new Uri(result.MetadataBaseUri), null, o.GetType().FullName, Models.ODataObjectType.ComplexType);
                }
            }
            return(o);
        }
Example #3
0
        public ODataObject CreateFromUrl(string Url, ODataObject oDataObject, JsonSerializer serializer)
        {
            var odataExpression = @"^(?<cast>[^$\(]+)\((?<id>[^\)]+)\)";

            Uri    uri         = new Uri(Url);
            string type        = null;
            string id          = null;
            var    uriSegments = uri.GetSegments();

            foreach (var segment in uriSegments)
            {
                var match = Regex.Match(segment, odataExpression, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    type = match.Groups["cast"].Value;
                    id   = match.Groups["id"].Value;
                }
            }
            if (type == null && uriSegments.Length > 0)
            {
                type = uriSegments[uriSegments.Length - 1];
            }

            if (type != null)
            {
                var o = Create(oDataObject.GetType(), type, oDataObject, serializer);
                if (!string.IsNullOrEmpty(id))
                {
                    o.Id = id;
                }
                return(o);
            }

            if (oDataObject != null)
            {
                return(oDataObject);
            }

            return(null);
        }