Beispiel #1
0
        /// <summary>
        /// This writes the public contents of the Entity in the properties element.
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>XElement representation of the properties element</returns>
        XElement WriteEntityContents(IOfflineEntity entity)
        {
            XElement contentElement = new XElement(FormatterConstants.ODataMetadataNamespace + FormatterConstants.PropertiesElementName);

            // Write only the primary keys if its an tombstone
            PropertyInfo[] properties = ReflectionUtility.GetPropertyInfoMapping(entity.GetType());

            // Write individual properties to the feed,
            foreach (PropertyInfo fi in properties)
            {
                string edmType   = FormatterUtilities.GetEdmType(fi.PropertyType);
                object value     = fi.GetValue(entity, null);
                string valString = value as string;
                if (valString != null)
                {
                    value = AtomHelper.CleanInvalidXmlChars(valString);
                }
                Type propType = fi.PropertyType;
                if (fi.PropertyType.IsGenericType && fi.PropertyType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture))
                {
                    // Its a Nullable<T> property
                    propType = fi.PropertyType.GetGenericArguments()[0];
                }

                if (value == null)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + fi.Name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubIsNullElementName, true)));
                }
                else if (propType == FormatterConstants.DateTimeType ||
                         propType == FormatterConstants.TimeSpanType ||
                         propType == FormatterConstants.DateTimeOffsetType)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + fi.Name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     FormatterUtilities.ConvertDateTimeForType_Atom(value, propType)));
                }
                else if (propType != FormatterConstants.ByteArrayType)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + fi.Name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     value));
                }
                else
                {
                    byte[] bytes = (byte[])value;
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + fi.Name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     Convert.ToBase64String(bytes)));
                }
            }

            return(contentElement);
        }
Beispiel #2
0
        private static object GetValueFromType(Type type, string value)
        {
            if (value == null)
            {
                if (type.IsGenericType)
                {
                    return(null);
                }
                else if (!type.IsPrimitive)
                {
                    return(null);
                }
                else
                {
                    // Error case. Value cannot be null for a non nullable primitive type
                    throw new InvalidOperationException("Error in deserializing type " + type.FullName);
                }
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == FormatterConstants.NullableType)
            {
                type = type.GetGenericArguments()[0];
            }

            if (FormatterConstants.StringType.IsAssignableFrom(type))
            {
                return(value);
            }
            else if (FormatterConstants.ByteArrayType.IsAssignableFrom(type))
            {
                return(Convert.FromBase64String(value));
            }
            else if (FormatterConstants.GuidType.IsAssignableFrom(type))
            {
                return(new Guid(value));
            }
            else if (FormatterConstants.DateTimeType.IsAssignableFrom(type) ||
                     FormatterConstants.DateTimeOffsetType.IsAssignableFrom(type) ||
                     FormatterConstants.TimeSpanType.IsAssignableFrom(type))
            {
                return(FormatterUtilities.ParseDateTimeFromString(value, type));
            }
            else if (FormatterConstants.DecimalType.IsAssignableFrom(type))
            {
                return(Decimal.Parse(value, NumberStyles.Any, NumberFormatInfo.InvariantInfo));
                // return Convert.ChangeType(value + "M", type, null);
            }
            else if (FormatterConstants.FloatType.IsAssignableFrom(type))
            {
                return(float.Parse(value, NumberStyles.Any, NumberFormatInfo.InvariantInfo));
            }
            else if (type.IsPrimitive)
            {
                return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
            }
            return(value);
        }
Beispiel #3
0
        private static object GetValueFromType(Type type, string value)
        {
            if (value == null)
            {
                if (type.IsGenericType())
                {
                    return(null);
                }
                if (!type.IsPrimitive())
                {
                    return(null);
                }

                throw new InvalidOperationException("Error in deserializing type " + type.FullName);
            }

            if (type.IsGenericType() && type.GetGenericTypeDefinition() == FormatterConstants.NullableType)
            {
                type = type.GetGenericArguments()[0];
            }

            if (FormatterConstants.StringType.IsAssignableFrom(type))
            {
                return(value);
            }

            if (FormatterConstants.ByteArrayType.IsAssignableFrom(type))
            {
                return(Convert.FromBase64String(value));
            }

            if (FormatterConstants.GuidType.IsAssignableFrom(type))
            {
                return(new Guid(value));
            }

            if (FormatterConstants.DateTimeType.IsAssignableFrom(type) ||
                FormatterConstants.DateTimeOffsetType.IsAssignableFrom(type) ||
                FormatterConstants.TimeSpanType.IsAssignableFrom(type))
            {
                return(FormatterUtilities.ParseDateTimeFromString(value, type));
            }

            if (type.IsPrimitive() ||
                FormatterConstants.DecimalType.IsAssignableFrom(type) ||
                FormatterConstants.FloatType.IsAssignableFrom(type))
            {
                return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
            }

            return(value);
        }
Beispiel #4
0
        /// <summary>
        /// This writes the public contents of the Entity to the passed in XElement.
        /// </summary>
        /// <param name="contentElement">The XElement to which the type properties is added to</param>
        /// <param name="entity">Entity</param>
        /// <returns>XElement representation of the properties element</returns>
        void WriteEntityContentsToElement(XElement contentElement, IOfflineEntity entity)
        {
            PropertyInfo[] properties = ReflectionUtility.GetPropertyInfoMapping(entity.GetType());

            // Write individual properties to the feed,
            foreach (PropertyInfo fi in properties)
            {
                object objValue = fi.GetValue(entity, null);
                if (objValue == null)
                {
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.Null),
                                     objValue));
                }
                else if (fi.PropertyType == FormatterConstants.CharType ||
                         fi.PropertyType == FormatterConstants.StringType ||
                         fi.PropertyType == FormatterConstants.GuidType)
                {
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.String),
                                     objValue));
                }
                else if (fi.PropertyType == FormatterConstants.DateTimeType ||
                         fi.PropertyType == FormatterConstants.TimeSpanType ||
                         fi.PropertyType == FormatterConstants.DateTimeOffsetType)
                {
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.String),
                                     FormatterUtilities.ConvertDateTimeForType_Json(objValue, fi.PropertyType)));
                }
                else if (fi.PropertyType == FormatterConstants.BoolType)
                {
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.Boolean),
                                     objValue));
                }
                else if (fi.PropertyType == FormatterConstants.ByteArrayType)
                {
                    byte[] bytes = (byte[])objValue;
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.String),
                                     Convert.ToBase64String(bytes)));
                }
                else if (fi.PropertyType.IsGenericType && fi.PropertyType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture))
                {
                    // Its a Nullable<T> property
                    Type genericParamType = fi.PropertyType.GetGenericArguments()[0];

                    string elementType = JsonElementTypes.Number;
                    if (genericParamType == FormatterConstants.BoolType)
                    {
                        elementType = JsonElementTypes.Boolean;
                    }
                    else if (genericParamType == FormatterConstants.DateTimeType ||
                             genericParamType == FormatterConstants.TimeSpanType ||
                             genericParamType == FormatterConstants.DateTimeOffsetType)
                    {
                        contentElement.Add(
                            new XElement(fi.Name,
                                         new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.String),
                                         FormatterUtilities.ConvertDateTimeForType_Json(objValue, genericParamType)));
                        continue;
                    }
                    else if (genericParamType == FormatterConstants.CharType ||
                             genericParamType == FormatterConstants.GuidType)
                    {
                        elementType = JsonElementTypes.String;
                    }

                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, elementType),
                                     objValue));
                }
                else // Its a number
                {
                    contentElement.Add(
                        new XElement(fi.Name,
                                     new XAttribute(FormatterConstants.JsonTypeAttributeName, JsonElementTypes.Number),
                                     objValue));
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// This writes the public contents of the Entity in the properties element.
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>XElement representation of the properties element</returns>
        XElement WriteEntityContents(IOfflineEntity entity)
        {
            XElement contentElement = new XElement(FormatterConstants.ODataMetadataNamespace + FormatterConstants.PropertiesElementName);

            // Write only the primary keys if its an tombstone
            var        ent  = (IEntity)entity;
            EntityType type = ent.EntityType;

            // Write individual properties to the feed,
            foreach (EntityField field in type.Fields)
            {
                Type   propType = field.Type;
                string name     = field.Name;
                object value    = ent.GetValue(field.Name);
                if (propType == typeof(IDbRef))
                {
                    propType = typeof(Guid);
                    var dbRef = (IDbRef)value;
                    value = dbRef.Id;
                    name  = "__" + name;
                }

                string edmType = FormatterUtilities.GetEdmType(propType);
                if (edmType == null)
                {
                    continue;
                }


                if (propType.IsGenericType && propType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture))
                {
                    // Its a Nullable<T> property
                    propType = propType.GetGenericArguments()[0];
                }

                if (value == null)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubIsNullElementName, true)));
                }
                else if (propType == FormatterConstants.DateTimeType ||
                         propType == FormatterConstants.TimeSpanType ||
                         propType == FormatterConstants.DateTimeOffsetType)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     FormatterUtilities.ConvertDateTimeForType_Atom(value, propType)));
                }
                else if (propType != FormatterConstants.ByteArrayType)
                {
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     value));
                }
                else
                {
                    byte[] bytes = (byte[])value;
                    contentElement.Add(
                        new XElement(FormatterConstants.ODataDataNamespace + name,
                                     new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType),
                                     Convert.ToBase64String(bytes)));
                }
            }

            return(contentElement);
        }
Beispiel #6
0
        /// <summary>
        /// This writes the public contents of the Entity in the properties element.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="entity">Entity</param>
        /// <returns>XElement representation of the properties element</returns>
        void WriteEntityContents(XElement element, IOfflineEntity entity)
        {
            // Write only the primary keys if its an tombstone
            PropertyInfo[] properties = ReflectionUtility.GetPropertyInfoMapping(entity.GetType());

            // Write individual properties to the feed,
            foreach (PropertyInfo fi in properties.OrderBy(val => val.Name.StartsWith("__") ? val.Name.Substring(2) : val.Name))
            {
                object value    = fi.GetValue(entity, null);
                Type   propType = fi.PropertyType;
                if (fi.PropertyType.IsGenericType && fi.PropertyType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture))
                {
                    // Its a Nullable<T> property
                    propType = fi.PropertyType.GetGenericArguments()[0];
                }

                if (value == null)
                {
                    element.Add(new XElement(FormatterConstants.AtomXmlNamespace + C.NullableProperty));
                }
                else if (propType == FormatterConstants.DateTimeType ||
                         propType == FormatterConstants.TimeSpanType ||
                         propType == FormatterConstants.DateTimeOffsetType)
                {
                    element.Add(new XElement(FormatterConstants.AtomXmlNamespace + C.Property, FormatterUtilities.ConvertDateTimeForType_Atom(value, propType)));
                }
                else if (propType != FormatterConstants.ByteArrayType)
                {
                    element.Add(new XElement(FormatterConstants.AtomXmlNamespace + C.Property, value));
                }
                else
                {
                    byte[] bytes = (byte[])value;
                    element.Add(new XElement(FormatterConstants.AtomXmlNamespace + C.Property, Convert.ToBase64String(bytes)));
                }
            }
        }