Пример #1
0
        private static Dictionary <Type, Dictionary <int, SerializedModel> > LoadStringModels(Type contextType,
                                                                                              IEnumerable <PropertyInfo> storageSets)
        {
            var stringModels = new Dictionary <Type, Dictionary <int, SerializedModel> >();

            foreach (var prop in storageSets)
            {
                var modelType        = prop.PropertyType.GetGenericArguments()[0];
                var map              = new Dictionary <int, SerializedModel>();
                var storageTableName = Util.GetStorageTableName(contextType, modelType);
                var metadata         = StorageManagerUtil.LoadMetadata(storageTableName);
                if (metadata == null)
                {
                    continue;
                }
                foreach (var guid in metadata.Guids)
                {
                    var name            = $"{storageTableName}-{guid}";
                    var serializedModel = BlazorDBInterop.GetItem(name, false);
                    var id = FindIdInSerializedModel(serializedModel);
                    map.Add(id, new SerializedModel {
                        StringModel = serializedModel
                    });
                }

                stringModels.Add(modelType, map);
            }

            return(stringModels);
        }
Пример #2
0
        private static object CreateContext(Type contextType,
                                            IReadOnlyDictionary <Type, Dictionary <int, SerializedModel> > stringModels)
        {
            var context = Activator.CreateInstance(contextType);

            foreach (var prop in contextType.GetProperties())
            {
                if (prop.PropertyType.IsGenericType &&
                    prop.PropertyType.GetGenericTypeDefinition() == typeof(StorageSet <>))
                {
                    var modelType        = prop.PropertyType.GetGenericArguments()[0];
                    var storageSetType   = StorageManagerUtil.GenericStorageSetType.MakeGenericType(modelType);
                    var storageTableName = Util.GetStorageTableName(contextType, modelType);
                    var metadata         = StorageManagerUtil.LoadMetadata(storageTableName);
                    if (stringModels.ContainsKey(modelType))
                    {
                        var map = stringModels[modelType];
                        Logger.LoadModelInContext(modelType, map.Count);
                    }
                    else
                    {
                        Logger.LoadModelInContext(modelType, 0);
                    }

                    var storageSet = metadata != null
                        ? LoadStorageSet(storageSetType, contextType, modelType, stringModels[modelType])
                        : CreateNewStorageSet(storageSetType, contextType);

                    prop.SetValue(context, storageSet);
                }
            }

            return(context);
        }
Пример #3
0
 private int SaveStorageSets(StorageContext context, int total, Type contextType, List <PropertyInfo> storageSets)
 {
     foreach (var prop in storageSets)
     {
         var storageSetValue  = prop.GetValue(context);
         var modelType        = prop.PropertyType.GetGenericArguments()[0];
         var storageTableName = Util.GetStorageTableName(contextType, modelType);
         var guids            = SaveModels(storageSetValue, modelType, storageTableName, storageSets);
         total += guids.Count;
         var oldMetadata = StorageManagerUtil.LoadMetadata(storageTableName);
         SaveMetadata(storageTableName, guids, contextType, modelType);
         if (oldMetadata != null)
         {
             DeleteOldModelsFromStorage(oldMetadata, storageTableName);
         }
         Logger.StorageSetSaved(modelType, guids.Count);
     }
     return(total);
 }
Пример #4
0
        private static IReadOnlyDictionary <string, Metadata> LoadMetadataList(StorageContext context,
                                                                               IEnumerable <PropertyInfo> storageSets, Type contextType)
        {
            var map = new Dictionary <string, Metadata>();

            foreach (var prop in storageSets)
            {
                var modelType        = prop.PropertyType.GetGenericArguments()[0];
                var storageTableName = Util.GetStorageTableName(contextType, modelType);
                var metadata         = StorageManagerUtil.LoadMetadata(storageTableName) ?? new Metadata
                {
                    Guids       = new List <Guid>(),
                    ContextName = Util.GetFullyQualifiedTypeName(context.GetType()),
                    ModelName   = Util.GetFullyQualifiedTypeName(modelType)
                };
                map.Add(Util.GetFullyQualifiedTypeName(modelType), metadata);
            }

            return(map);
        }