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