private static Entity DeserializeEntity(BsonDocument entityDoc) { var entityId = entityDoc[EntityIdName].AsInt32; var entity = new Entity { Id = entityId }; foreach (var each in entityDoc) { if (!AttributeHelper.TypeMap.ContainsKey(each.Name)) { continue; } Debug.Assert(each.Value.IsBsonDocument, "AttributeDoc should be BsonDocument"); var attributeType = AttributeHelper.TypeMap[each.Name]; var attribute = entity.Attach(attributeType); var attributeDoc = each.Value.AsBsonDocument; foreach (var eachProperty in attributeType.GetProperties()) { if (!eachProperty.CanWrite) { continue; } var propertyName = eachProperty.Name.ToUnderscored(); if (!attributeDoc.Contains(propertyName)) { continue; } var propertyValue = attributeDoc[propertyName].AsString; propertyValue = TryTypeCorrection(eachProperty.PropertyType, propertyValue); eachProperty.SetValue(attribute, TypeChanger.ChangeType(propertyValue, eachProperty.PropertyType)); } } return(entity); }
// ReSharper disable PossibleNullReferenceException private static XmlDocument SerializeEntity(Entity entity) { var entityDoc = new XmlDocument(); var entityNode = entityDoc.AppendChild(entityDoc.CreateElement("entity")) as XmlElement; entityNode.SetAttribute("id", entity.Id.ToString(CultureInfo.InvariantCulture)); foreach (var attributePair in entity.Attributes) { var attributeType = attributePair.Key; // skip when volatile class var classVolatiles = attributeType.GetCustomAttributes(typeof(VolatileAttribute), false); if (classVolatiles.Length > 0) { continue; } var attribute = attributePair.Value; var attributeNode = entityNode.AppendChild(entityDoc.CreateElement("attribute")) as XmlElement; attributeNode.SetAttribute("name", attributeType.Name.ToUnderscored()); foreach (var eachProperty in attributeType.GetProperties()) { // skip when volatile property var propertyVolatiles = eachProperty.GetCustomAttributes(typeof(VolatileAttribute), false); if (propertyVolatiles.Length > 0) { continue; } var eachValue = eachProperty.GetValue(attribute); var valueNode = attributeNode.AppendChild(entityDoc.CreateElement("field")) as XmlElement; valueNode.SetAttribute("name", eachProperty.Name.ToUnderscored()); valueNode.SetAttribute("value", TypeChanger.ToString(eachValue)); } } return(entityDoc); }
private static Entity DeserializeEntity(XmlDocument entityDoc) { var entityNode = entityDoc.DocumentElement; var entityId = entityNode.GetAttribute("id").As <int>(); var entity = new Entity { Id = entityId }; foreach (var attributeNode in entityNode.ChildNodes.OfType <XmlElement>()) { var attributeName = attributeNode.GetAttribute("name"); if (!AttributeHelper.TypeMap.ContainsKey(attributeName)) { continue; } var attributeType = AttributeHelper.TypeMap[attributeName]; var attribute = entity.Attach(attributeType); foreach (var eachProperty in attributeType.GetProperties()) { var propertyName = eachProperty.Name.ToUnderscored(); var propertyNode = attributeNode.SelectSingleNode(string.Format("field[@name='{0}']", propertyName)) as XmlElement; if (propertyNode == null) { continue; } var propertyValue = propertyNode.GetAttribute("value"); if (!eachProperty.CanWrite) { continue; } eachProperty.SetValue(attribute, TypeChanger.ChangeType(propertyValue, eachProperty.PropertyType)); } } return(entity); }
private static BsonDocument SerializeEntity(Entity entity) { var entityDoc = new BsonDocument { { EntityIdName, entity.Id } }; foreach (var attributePair in entity.Attributes) { var attributeType = attributePair.Key; // skip when volatile class var classVolatiles = attributeType.GetCustomAttributes(typeof(VolatileAttribute), false); if (classVolatiles.Length > 0) { continue; } var attribute = attributePair.Value; var attributeDoc = new BsonDocument(); foreach (var eachProperty in attributeType.GetProperties()) { // skip when volatile property var propertyVolatiles = eachProperty.GetCustomAttributes(typeof(VolatileAttribute), false); if (propertyVolatiles.Length > 0) { continue; } var eachValue = eachProperty.GetValue(attribute); attributeDoc.Add(eachProperty.Name.ToUnderscored(), TypeChanger.ToString(eachValue)); } entityDoc.Add(attributeType.Name.ToUnderscored(), attributeDoc); } return(entityDoc); }