internal bool Save(IJsonEntity entity) { Type[] affectedTypes; object[] addedEntities; object[] removedEntities; var type = entity.GetType(); UpdateData(type, entity, true, out affectedTypes, out addedEntities, out removedEntities); var saved = false; foreach (var updatedType in affectedTypes) { var typeData = GetTypeData(updatedType); var json = new StringBuilder(); json.AppendLine("{"); var keys = typeData.Keys.Select(int.Parse).OrderBy(k => k).ToArray(); for (var i = 0; i < keys.Length; i++) { var id = keys[i].ToString(CultureInfo.InvariantCulture); var rawJson = JsonConvert.SerializeObject(typeData[id], new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); var formattedJson = Regex.Replace(Regex.Replace(Regex.Replace(rawJson, "^{(.*)}$", "{ $1 }"), "\":", "\": "), ",\"", ", \""); json.AppendFormat(" \"{0}\": {1}", id, formattedJson); if (i == keys.Length - 1) { json.AppendLine(); } else { json.AppendLine(","); } } json.AppendLine("}"); if (!Directory.Exists(storagePath)) { Directory.CreateDirectory(storagePath); } var jsonFilePath = Path.Combine(storagePath, type.Name + ".json"); File.WriteAllText(jsonFilePath, json.ToString(), Encoding.UTF8); saved = true; } return(saved); }
private static void FillJsonEntity(BaseDbEntity entity, IJsonEntity newJsonEntity) { List<MapToField> mappedFields = GetMappedFields(entity); Type jsonEntityType = newJsonEntity.GetType(); Type dbEntityType = entity.GetType(); mappedFields.ForEach(mappedAttr => { var jsonEntityProp = jsonEntityType.GetProperty(mappedAttr.JsonModelField); var dbEntityProp = dbEntityType.GetProperty(mappedAttr.JsonModelField); if (jsonEntityProp != null && dbEntityProp != null) { var dbValue = dbEntityProp.GetValue(entity); jsonEntityProp.SetValue(newJsonEntity, dbValue); } }); }
internal void Initialize(IJsonEntity entity) { var type = entity.GetType(); var id = entity.Id; if (!id.HasValue) { throw new InvalidOperationException("Cannot initialize entity of type '" + type.Name + "' with no id."); } Dictionary <string, object> instanceData; var typeData = GetTypeData(type); if (!typeData.TryGetValue(id.Value.ToString(CultureInfo.InvariantCulture), out instanceData)) { throw new InvalidOperationException("Cannot initialize entity: " + type.Name + "|" + id + "."); } InitializeInstance(type, entity, instanceData); }
internal bool IsModified(IJsonEntity entity) { var type = entity.GetType(); var id = entity.Id; if (_deletedEntities.Contains(entity)) { if (!id.HasValue) { throw new Exception("Accessed deleted entity with no id: " + type.Name + "|."); } throw new Exception("Accessed deleted entity: " + type.Name + "|" + id + "."); } if (_pendingDeleteEntities.Contains(entity)) { return(true); } if (_newEntities.Contains(entity)) { return(true); } if (!id.HasValue) { throw new Exception("Found existing entity with no id: " + type.Name + "|."); } var typeData = GetTypeData(type); Dictionary <string, object> instanceData; if (typeData == null || !typeData.TryGetValue(id.Value.ToString(CultureInfo.InvariantCulture), out instanceData)) { throw new Exception("Couldn't find data for existing entity: " + type.Name + "|."); } foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (property.Name == "Id") { continue; } var value = property.GetValue(entity, null); var propertyType = property.PropertyType; object propertyData; var hasPropertyData = instanceData.TryGetValue(property.Name, out propertyData); Type referenceType; bool isReferenceList; if (JsonEntity.TryGetReferenceType(propertyType, supportedTypes, out referenceType, out isReferenceList)) { if (isReferenceList) { var referenceIds = new List <int>(); foreach (IJsonEntity reference in (IEnumerable)value) { var referenceId = reference.Id; if (!referenceId.HasValue) { throw new InvalidOperationException(); } referenceIds.Add(referenceId.Value); } if (!hasPropertyData || IsReferenceListModified(referenceIds.ToArray(), propertyData)) { return(true); } } else { var referenceId = ((IJsonEntity)value).Id; if (!referenceId.HasValue) { throw new InvalidOperationException(); } if (!hasPropertyData || IsReferenceModified(referenceId, propertyData)) { return(true); } } } else if (!hasPropertyData || IsValueModified(value, propertyData)) { return(true); } } return(false); }