/// <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); }
/// <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))); } } }