Esempio n. 1
0
        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);
        }
Esempio n. 3
0
        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 bool HasListAssociation(List <PropertyInfo> storageSets, Type modelType, SerializedModel stringModel)
        {
            var found = false;

            foreach (var prop in modelType.GetProperties())
            {
                if (StorageManagerUtil.IsListInContext(storageSets, prop))
                {
                    found = true;
                    break;
                }
            }
            return(found);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 private static bool HasListAssociation(List <PropertyInfo> storageSets, Type modelType)
 {
     return(modelType.GetProperties().Any(prop => StorageManagerUtil.IsListInContext(storageSets, prop)));
 }