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);
        }
Exemplo n.º 2
0
        private Dictionary <Type, Dictionary <int, SerializedModel> > DeserializeModels(
            Dictionary <Type, Dictionary <int, SerializedModel> > stringModels, List <PropertyInfo> storageSets)
        {
            foreach (KeyValuePair <Type, Dictionary <int, SerializedModel> > map in stringModels)
            {
                Type modelType = map.Key;
                foreach (KeyValuePair <int, SerializedModel> sm in map.Value)
                {
                    SerializedModel stringModel = sm.Value;
                    if (!stringModel.HasAssociation)
                    {
                        stringModel.Model = DeserializeModel(modelType, stringModel.StringModel);
                    }
                }
            }

            foreach (KeyValuePair <Type, Dictionary <int, SerializedModel> > map in stringModels) //TODO: Fix associations that are more than one level deep
            {
                Type modelType = map.Key;
                foreach (KeyValuePair <int, SerializedModel> sm in map.Value)
                {
                    SerializedModel stringModel = sm.Value;
                    if (stringModel.Model != null)
                    {
                        continue;
                    }

                    object model = DeserializeModel(modelType, stringModel.StringModel);
                    foreach (PropertyInfo prop in model.GetType().GetProperties())
                    {
                        if (_storageManagerUtil.IsInContext(storageSets, prop) && prop.GetValue(model) != null)
                        {
                            object       associatedLocalModel = prop.GetValue(model);
                            PropertyInfo localIdProp          =
                                associatedLocalModel.GetType()
                                .GetProperty(StorageManagerUtil.Id);
                            if (localIdProp == null)
                            {
                                throw new ArgumentException("Model must have Id property");
                            }

                            int    localId = Convert.ToInt32(localIdProp.GetValue(associatedLocalModel));
                            object 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);
        }
Exemplo n.º 4
0
        private object LoadStorageSet(Type storageSetType, Type contextType, Type modelType,
                                      Dictionary <int, SerializedModel> map)
        {
            object instance        = CreateNewStorageSet(storageSetType, contextType);
            Type   listGenericType = StorageManagerUtil.GenericListType.MakeGenericType(modelType);
            object list            = Activator.CreateInstance(listGenericType);

            foreach (KeyValuePair <int, SerializedModel> sm in map)
            {
                SerializedModel stringModel = sm.Value;
                MethodInfo      addMethod   = listGenericType.GetMethod(StorageManagerUtil.Add);
                addMethod.Invoke(list, new[] { stringModel.Model });
            }

            return(SetList(instance, list));
        }
Exemplo n.º 5
0
        //TODO: The snippet below should also check to see that the model itself has no more associations to fix, not just if it has properties.

        /*
         * if(HasAssociation(storageSets, modelType, stringModel))
         *  {
         *      stringModel.StringModel = FixAssociationsInStringModels(stringModel, modelType, storageSets, stringModels);
         *      stringModel.ScanDone = true;
         *  }*/
        private Dictionary <Type, Dictionary <int, SerializedModel> > ScanAssociationModels(
            List <PropertyInfo> storageSets,
            Dictionary <Type, Dictionary <int, SerializedModel> > stringModels)
        {
            int count = 0;

            do
            {
                count++;
                foreach (KeyValuePair <Type, Dictionary <int, SerializedModel> > map in stringModels)
                {
                    Type modelType = map.Key;
                    foreach (KeyValuePair <int, SerializedModel> sm in map.Value)
                    {
                        SerializedModel stringModel = sm.Value;
                        if (stringModel.ScanDone)
                        {
                            continue;
                        }

                        if (HasAssociation(storageSets, modelType) ||
                            HasListAssociation(storageSets, modelType))
                        {
                            stringModel.StringModel =
                                FixAssociationsInStringModels(stringModel, modelType, storageSets, stringModels);
                            stringModel.ScanDone = true;
                        }
                        else
                        {
                            stringModel.ScanDone = true;
                        }
                    }
                }

                if (count == 20)
                {
                    break; //Go 20 deep throw exception here?
                }
            } while (IsScanDone(stringModels));

            return(stringModels);
        }
Exemplo n.º 6
0
        private Dictionary <Type, Dictionary <int, SerializedModel> > ScanNonAssociationModels(
            List <PropertyInfo> storageSets, Dictionary <Type, Dictionary <int, SerializedModel> > stringModels)
        {
            foreach (KeyValuePair <Type, Dictionary <int, SerializedModel> > map in stringModels)
            {
                Type modelType = map.Key;
                foreach (KeyValuePair <int, SerializedModel> sm in map.Value)
                {
                    SerializedModel stringModel = sm.Value;
                    if (!HasAssociation(storageSets, modelType) &&
                        !HasListAssociation(storageSets, modelType))
                    {
                        stringModel.HasAssociation = false;
                        stringModel.ScanDone       = true;
                    }
                    else
                    {
                        stringModel.HasAssociation = true;
                    }
                }
            }

            return(stringModels);
        }