static void WriteHalResource(Representation representation, XmlWriter writer, string propertyName = null) { if (representation == null) { return; } // First write the well-known HAL properties writer.WriteStartElement("resource"); writer.WriteAttributeString("rel", representation.Rel); writer.WriteAttributeString("href", representation.Href); if (representation.LinkName != null || propertyName != null) { writer.WriteAttributeString("name", representation.LinkName = representation.LinkName ?? propertyName); } // Second, determine if resource is of Generic Resource List Type , list out all the items var representationList = representation as IRepresentationList; if (representationList != null) { foreach (var item in representationList.Cast <Representation>()) { WriteHalResource(item, writer); } } //Third write out the links of the resource foreach (var link in representation.Links) { writer.WriteStartElement("link"); writer.WriteAttributeString("rel", link.Rel); writer.WriteAttributeString("href", link.Href); if (!string.IsNullOrEmpty(link.Title)) { writer.WriteAttributeString("title", link.Title); } writer.WriteEndElement(); } // Fourth, write the rest of the properties WriteResourceProperties(representation, writer); writer.WriteEndElement(); }
static void WriteHalResource(Representation representation, XmlWriter writer, string propertyName = null) { if (representation == null) { return; } representation.RepopulateHyperMedia(); // First write the well-known HAL properties writer.WriteStartElement("resource"); writer.WriteAttributeString("rel", representation.Rel); writer.WriteAttributeString("href", representation.Href); if (representation.LinkName != null || propertyName != null) { writer.WriteAttributeString("name", representation.LinkName = representation.LinkName ?? propertyName); } // Second, determine if resource is of Generic Resource List Type , list out all the items var representationList = representation as IRepresentationList; if (representationList != null) { foreach (var item in representationList.Cast <Representation>()) { WriteHalResource(item, writer); } } //Third write out the links of the resource var links = new HashSet <Link>(representation.Links.Where(link => link.Rel != "self"), new LinkEqualityComparer()); foreach (var link in links) { writer.WriteStartElement("link"); writer.WriteAttributeString("rel", link.Rel); writer.WriteAttributeString("href", link.Href); writer.WriteEndElement(); } // Fourth, write the rest of the properties WriteResourceProperties(representation, writer); writer.WriteEndElement(); }
private static void SetProperties(Type type, XElement xml, Representation representation) { foreach (var property in type.GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { type.SetPropertyValue(property.Name, xml.Element(property.Name), representation); } else if (typeof(Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var resourceXml = xml.Elements("resource").SingleOrDefault(x => x.Attribute("name").Value == property.Name); var halResource = ReadHalResource(property.PropertyType, resourceXml); property.SetValue(representation, halResource, null); } } }
static void WriteResourceProperties(Representation representation, XmlWriter writer) { // Only simple type and nested ApiResource type will be handled : for any other type, exception will be thrown // including List<ApiResource> as representation of List would require properties rel, href and linkname // To overcome the issue, use "RepresentationList<T>" foreach (var property in representation.GetType().GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { var propertyString = GetPropertyString(property, representation); if (propertyString != null) { writer.WriteElementString(property.Name, propertyString); } } else if (typeof(Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var halResource = property.GetValue(representation, null); WriteHalResource((Representation)halResource, writer, property.Name); } } }
private static void CreateSelfHypermedia(Type type, XElement xml, Representation representation) { type.GetProperty("Rel").SetValue(representation, xml.Attribute("rel").Value, null); type.SetPropertyValue("Href", xml.Attribute("href"), representation); type.SetPropertyValue("LinkName", xml.Attribute("name"), representation); }
static void WriteResourceProperties(Representation representation, XmlWriter writer) { // Only simple type and nested ApiResource type will be handled : for any other type, exception will be thrown // including List<ApiResource> as representation of List would require properties rel, href and linkname // To overcome the issue, use "RepresentationList<T>" foreach (var property in representation.GetType().GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { var propertyString = GetPropertyString(property, representation); if (propertyString != null) { writer.WriteElementString(property.Name, propertyString); } } else if (typeof (Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var halResource = property.GetValue(representation, null); WriteHalResource((Representation) halResource, writer, property.Name); } else if (typeof (IEnumerable<Representation>).IsAssignableFrom(property.PropertyType)) { var halResourceList = property.GetValue(representation, null) as IEnumerable<Representation>; if (halResourceList != null) foreach (var item in halResourceList) WriteHalResource(item, writer); } } }
static void WriteHalResource(Representation representation, XmlWriter writer, string propertyName = null) { if (representation == null) { return; } representation.RepopulateHyperMedia(); // First write the well-known HAL properties writer.WriteStartElement("resource"); writer.WriteAttributeString("rel", representation.Rel); writer.WriteAttributeString("href", representation.Href); if (representation.LinkName != null || propertyName != null) { writer.WriteAttributeString("name", representation.LinkName = representation.LinkName ?? propertyName); } // Second, determine if resource is of Generic Resource List Type , list out all the items var representationList = representation as IRepresentationList; if (representationList != null) { foreach (var item in representationList.Cast<Representation>()) { WriteHalResource(item, writer); } } //Third write out the links of the resource foreach (var link in representation.Links.Where(link => link.Rel != "self")) { writer.WriteStartElement("link"); writer.WriteAttributeString("rel", link.Rel); writer.WriteAttributeString("href", link.Href); writer.WriteEndElement(); } // Fourth, write the rest of the properties WriteResourceProperties(representation, writer); writer.WriteEndElement(); }
static void CreateSelfHypermedia(Type type, XElement xml, Representation representation) { type.GetProperty("Rel").SetValue(representation, xml.Attribute("rel").Value, null); type.SetPropertyValue("Href", xml.Attribute("href"), representation); type.SetPropertyValue("LinkName", xml.Attribute("name"), representation); }
static void SetProperties(Type type, XElement xml, Representation representation) { foreach (var property in type.GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { type.SetPropertyValue(property.Name, xml.Element(property.Name), representation); } else if (typeof(Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var resourceXml = xml.Elements("resource").SingleOrDefault(x => x.Attribute("name").Value == property.Name); var halResource = ReadHalResource(property.PropertyType, resourceXml); property.SetValue(representation, halResource, null); } } }
private static void WriteResourceProperties(Representation value, XmlWriter writer) { var resourceType = value.GetType(); var allProps = resourceType.GetPublicInstanceProperties(); if (resourceType.IsResourceTyped()) { var modelProp = allProps.First(p => p.IsModelProperty()); var modelValue = modelProp.GetValue(value, null); var modelType = modelProp.PropertyType; WriteResourceProperties(modelValue, writer, modelType.GetPublicInstanceProperties()); var propsOthers = allProps.Where(p => !p.IsModelProperty()); WriteResourceProperties(value, writer, propsOthers); } else { WriteResourceProperties(value, writer, allProps); } }