private static void EnsureAllAssociationsHaveIds(StorageContext context, object storageSetValue, Type modelType, List <PropertyInfo> storageSets, IReadOnlyDictionary <string, Metadata> metadataMap) { var storageSetType = StorageManagerUtil.GenericStorageSetType.MakeGenericType(modelType); var method = storageSetType.GetMethod(StorageManagerUtil.GetEnumerator); var enumerator = (IEnumerator)method.Invoke(storageSetValue, new object[] { }); while (enumerator.MoveNext()) { var model = enumerator.Current; foreach (var prop in model.GetType().GetProperties()) { if (prop.GetValue(model) == null || !StorageManagerUtil.IsInContext(storageSets, prop) && !StorageManagerUtil.IsListInContext(storageSets, prop)) { continue; } if (StorageManagerUtil.IsInContext(storageSets, prop)) { EnsureOneAssociationHasId(context, prop.GetValue(model), prop.PropertyType, storageSets, metadataMap); } if (StorageManagerUtil.IsListInContext(storageSets, prop)) { EnsureManyAssociationHasId(context, prop.GetValue(model), prop, storageSets, metadataMap); } } } }
private string FixAssociationsInStringModels(SerializedModel stringModel, Type modelType, List <PropertyInfo> storageSets, Dictionary <Type, Dictionary <int, SerializedModel> > stringModels) { var result = stringModel.StringModel; foreach (var prop in modelType.GetProperties()) { if (StorageManagerUtil.IsInContext(storageSets, prop)) { if (TryGetIdFromSerializedModel(result, prop.Name, out var id)) { var updated = GetAssociatedStringModel(stringModels, prop.PropertyType, id); result = ReplaceIdWithAssociation(result, prop.Name, id, updated); } } if (StorageManagerUtil.IsListInContext(storageSets, prop)) { if (TryGetIdListFromSerializedModel(result, prop.Name, out var idList)) { var sb = new StringBuilder(); foreach (var id in idList) { var updated = GetAssociatedStringModel(stringModels, prop.PropertyType.GetGenericArguments()[0], id); sb.Append(updated).Append(","); } var strList = sb.ToString().Substring(0, sb.ToString().Length - 1); result = ReplaceListWithAssociationList(result, prop.Name, strList); } } } return(result); }
private string ScanModelForAssociations(object model, List <PropertyInfo> storageSets, string serializedModel) { var result = serializedModel; foreach (var prop in model.GetType().GetProperties()) { if (prop.GetValue(model) != null && StorageManagerUtil.IsInContext(storageSets, prop)) { var associatedModel = prop.GetValue(model); var idProp = associatedModel.GetType().GetProperty(StorageManagerUtil.ID); //TODO: Handle missing Id prop var id = Convert.ToString(idProp.GetValue(associatedModel)); var serializedItem = JsonUtil.Serialize(model); result = ReplaceModelWithId(result, serializedItem, id); } if (prop.GetValue(model) != null && StorageManagerUtil.IsListInContext(storageSets, prop)) { var modelList = (IEnumerable)prop.GetValue(model); foreach (var item in modelList) { var idProp = item.GetType().GetProperty(StorageManagerUtil.ID); //TODO: Handle missing Id prop var id = Convert.ToString(idProp.GetValue(item)); var serializedItem = JsonUtil.Serialize(item); result = ReplaceModelWithId(result, serializedItem, id); } } } return(result); }
private static Dictionary <Type, Dictionary <int, SerializedModel> > DeserializeModels( Dictionary <Type, Dictionary <int, SerializedModel> > stringModels, List <PropertyInfo> storageSets) { foreach (var map in stringModels) { var modelType = map.Key; foreach (var sm in map.Value) { var stringModel = sm.Value; if (!stringModel.HasAssociation) { stringModel.Model = DeserializeModel(modelType, stringModel.StringModel); } } } foreach (var map in stringModels) //TODO: Fix associations that are more than one level deep { var modelType = map.Key; foreach (var sm in map.Value) { var stringModel = sm.Value; if (stringModel.Model != null) { continue; } var model = DeserializeModel(modelType, stringModel.StringModel); foreach (var prop in model.GetType().GetProperties()) { if (StorageManagerUtil.IsInContext(storageSets, prop) && prop.GetValue(model) != null) { var associatedLocalModel = prop.GetValue(model); var localIdProp = associatedLocalModel.GetType() .GetProperty(StorageManagerUtil.Id); if (localIdProp == null) { throw new ArgumentException("Model must have Id property"); } var localId = Convert.ToInt32(localIdProp.GetValue(associatedLocalModel)); var associatdRemoteModel = GetModelFromStringModels(stringModels, associatedLocalModel.GetType(), localId) .Model; prop.SetValue(model, associatdRemoteModel); } } stringModel.Model = model; } } return(stringModels); }
private bool HasAssociation(List <PropertyInfo> storageSets, Type modelType, SerializedModel stringModel) { var found = false; foreach (var prop in modelType.GetProperties()) { if (StorageManagerUtil.IsInContext(storageSets, prop)) { found = true; break; } } return(found); }
private static string ScanModelForAssociations(object model, List <PropertyInfo> storageSets, string serializedModel) { var result = serializedModel; foreach (var prop in model.GetType().GetProperties()) { if (prop.GetValue(model) == null || !StorageManagerUtil.IsInContext(storageSets, prop) && !StorageManagerUtil.IsListInContext(storageSets, prop)) { continue; } if (StorageManagerUtil.IsInContext(storageSets, prop)) { result = FixOneAssociation(model, prop, result); } if (StorageManagerUtil.IsListInContext(storageSets, prop)) { result = FixManyAssociation(model, prop, result); } } return(result); }
private static bool HasAssociation(List <PropertyInfo> storageSets, Type modelType) { return(modelType.GetProperties().Any(prop => StorageManagerUtil.IsInContext(storageSets, prop))); }