33using System . Linq ;
44using System . Reflection ;
55using System . Text ;
6- using Microsoft . AspNetCore . Blazor ;
7- using Microsoft . Extensions . DependencyInjection ;
6+ using System . Threading . Tasks ;
7+ using Microsoft . JSInterop ;
88
99namespace BlazorDB . Storage
1010{
1111 internal class StorageManagerLoad
1212 {
13- public void LoadContextFromStorageOrCreateNew ( IServiceCollection serviceCollection , Type contextType )
13+ public async Task LoadContextFromLocalStorage ( StorageContext context )
1414 {
15- Logger . StartContextType ( contextType ) ;
15+ var contextType = context . GetType ( ) ;
16+ await Logger . StartContextType ( contextType ) ;
1617 var storageSets = StorageManagerUtil . GetStorageSets ( contextType ) ;
17- var stringModels = LoadStringModels ( contextType , storageSets ) ;
18+ var stringModels = await LoadStringModels ( contextType , storageSets ) ;
1819 //PrintStringModels(stringModels);
1920 stringModels = ScanNonAssociationModels ( storageSets , stringModels ) ;
2021 stringModels = ScanAssociationModels ( storageSets , stringModels ) ;
2122 stringModels = DeserializeModels ( stringModels , storageSets ) ;
2223 //PrintStringModels(stringModels);
23- var context = CreateContext ( contextType , stringModels ) ;
24- RegisterContext ( serviceCollection , contextType , context ) ;
24+ await EnrichContext ( context , contextType , stringModels ) ;
2525 Logger . EndGroup ( ) ;
2626 }
2727
28- private static object CreateContext ( Type contextType ,
28+ private static async Task EnrichContext ( StorageContext context , Type contextType ,
2929 IReadOnlyDictionary < Type , Dictionary < int , SerializedModel > > stringModels )
3030 {
31- var context = Activator . CreateInstance ( contextType ) ;
3231 foreach ( var prop in contextType . GetProperties ( ) )
3332 if ( prop . PropertyType . IsGenericType &&
3433 prop . PropertyType . GetGenericTypeDefinition ( ) == typeof ( StorageSet < > ) )
3534 {
3635 var modelType = prop . PropertyType . GetGenericArguments ( ) [ 0 ] ;
3736 var storageSetType = StorageManagerUtil . GenericStorageSetType . MakeGenericType ( modelType ) ;
3837 var storageTableName = Util . GetStorageTableName ( contextType , modelType ) ;
39- var metadata = StorageManagerUtil . LoadMetadata ( storageTableName ) ;
38+ var metadata = await StorageManagerUtil . LoadMetadata ( storageTableName ) ;
4039 if ( stringModels . ContainsKey ( modelType ) )
4140 {
4241 var map = stringModels [ modelType ] ;
@@ -46,14 +45,11 @@ private static object CreateContext(Type contextType,
4645 {
4746 Logger . LoadModelInContext ( modelType , 0 ) ;
4847 }
49-
5048 var storageSet = metadata != null
5149 ? LoadStorageSet ( storageSetType , contextType , modelType , stringModels [ modelType ] )
5250 : CreateNewStorageSet ( storageSetType , contextType ) ;
5351 prop . SetValue ( context , storageSet ) ;
5452 }
55-
56- return context ;
5753 }
5854
5955 private static Dictionary < Type , Dictionary < int , SerializedModel > > DeserializeModels (
@@ -251,7 +247,7 @@ private static string FixAssociationsInStringModels(SerializedModel stringModel,
251247
252248 private static string ReplaceListWithAssociationList ( string serializedModel , string propName , string strList )
253249 {
254- var propStart = serializedModel . IndexOf ( $ "\" { propName } \" :[", StringComparison . Ordinal ) ;
250+ var propStart = serializedModel . IndexOf ( $ "\" { propName . ToCamelCase ( ) } \" :[", StringComparison . Ordinal ) ;
255251 var start = serializedModel . IndexOf ( '[' , propStart ) + 1 ;
256252 var end = serializedModel . IndexOf ( ']' , start ) ;
257253 var result = StorageManagerUtil . ReplaceString ( serializedModel , start , end , strList ) ;
@@ -262,13 +258,13 @@ private static bool TryGetIdListFromSerializedModel(string serializedModel, stri
262258 out List < int > idList )
263259 {
264260 var list = new List < int > ( ) ;
265- if ( serializedModel . IndexOf ( $ "\" { propName } \" :null", StringComparison . Ordinal ) != - 1 )
261+ if ( serializedModel . IndexOf ( $ "\" { propName . ToCamelCase ( ) } \" :null", StringComparison . Ordinal ) != - 1 )
266262 {
267263 idList = list ;
268264 return false ;
269265 }
270266
271- var propStart = serializedModel . IndexOf ( $ "\" { propName } \" :[", StringComparison . Ordinal ) ;
267+ var propStart = serializedModel . IndexOf ( $ "\" { propName . ToCamelCase ( ) } \" :[", StringComparison . Ordinal ) ;
272268 var start = serializedModel . IndexOf ( '[' , propStart ) + 1 ;
273269 var end = serializedModel . IndexOf ( ']' , start ) ;
274270 var stringlist = serializedModel . Substring ( start , end - start ) ;
@@ -297,7 +293,7 @@ private static bool IsScanDone(Dictionary<Type, Dictionary<int, SerializedModel>
297293 return done ;
298294 }
299295
300- private static Dictionary < Type , Dictionary < int , SerializedModel > > LoadStringModels ( Type contextType ,
296+ private static async Task < Dictionary < Type , Dictionary < int , SerializedModel > > > LoadStringModels ( Type contextType ,
301297 IEnumerable < PropertyInfo > storageSets )
302298 {
303299 var stringModels = new Dictionary < Type , Dictionary < int , SerializedModel > > ( ) ;
@@ -306,12 +302,12 @@ private static Dictionary<Type, Dictionary<int, SerializedModel>> LoadStringMode
306302 var modelType = prop . PropertyType . GetGenericArguments ( ) [ 0 ] ;
307303 var map = new Dictionary < int , SerializedModel > ( ) ;
308304 var storageTableName = Util . GetStorageTableName ( contextType , modelType ) ;
309- var metadata = StorageManagerUtil . LoadMetadata ( storageTableName ) ;
305+ var metadata = await StorageManagerUtil . LoadMetadata ( storageTableName ) ;
310306 if ( metadata == null ) continue ;
311307 foreach ( var guid in metadata . Guids )
312308 {
313309 var name = $ "{ storageTableName } -{ guid } ";
314- var serializedModel = BlazorDBInterop . GetItem ( name , false ) ;
310+ var serializedModel = await BlazorDBInterop . GetItem ( name , false ) ;
315311 var id = FindIdInSerializedModel ( serializedModel ) ;
316312 map . Add ( id , new SerializedModel { StringModel = serializedModel } ) ;
317313 }
@@ -325,13 +321,13 @@ private static Dictionary<Type, Dictionary<int, SerializedModel>> LoadStringMode
325321 //TODO: Verify that the found id is at the top level in case of nested objects
326322 private static bool TryGetIdFromSerializedModel ( string serializedModel , string propName , out int id )
327323 {
328- if ( serializedModel . IndexOf ( $ "\" { propName } \" :null", StringComparison . Ordinal ) != - 1 )
324+ if ( serializedModel . IndexOf ( $ "\" { propName . ToCamelCase ( ) } \" :null", StringComparison . Ordinal ) != - 1 )
329325 {
330326 id = - 1 ;
331327 return false ;
332328 }
333329
334- var propStart = serializedModel . IndexOf ( $ "\" { propName } \" :", StringComparison . Ordinal ) ;
330+ var propStart = serializedModel . IndexOf ( $ "\" { propName . ToCamelCase ( ) } \" :", StringComparison . Ordinal ) ;
335331 var start = serializedModel . IndexOf ( ':' , propStart ) ;
336332 id = GetIdFromString ( serializedModel , start ) ;
337333 return true ;
@@ -340,7 +336,7 @@ private static bool TryGetIdFromSerializedModel(string serializedModel, string p
340336 //TODO: Verify that the found id is at the top level in case of nested objects
341337 private static int FindIdInSerializedModel ( string serializedModel )
342338 {
343- var start = serializedModel . IndexOf ( $ "\" { StorageManagerUtil . Id } \" :", StringComparison . Ordinal ) ;
339+ var start = serializedModel . IndexOf ( $ "\" { StorageManagerUtil . Id . ToCamelCase ( ) } \" :", StringComparison . Ordinal ) ;
344340 return GetIdFromString ( serializedModel , start ) ;
345341 }
346342
@@ -368,7 +364,7 @@ private static int GetIdFromString(string stringToSearch, int startFrom = 0)
368364
369365 private static string ReplaceIdWithAssociation ( string result , string name , int id , string stringModel )
370366 {
371- var stringToFind = $ "\" { name } \" :{ id } ";
367+ var stringToFind = $ "\" { name . ToCamelCase ( ) } \" :{ id } ";
372368 var nameIndex = result . IndexOf ( stringToFind , StringComparison . Ordinal ) ;
373369 var index = result . IndexOf ( id . ToString ( ) , nameIndex , StringComparison . Ordinal ) ;
374370 result = StorageManagerUtil . ReplaceString ( result , index , index + id . ToString ( ) . Length , stringModel ) ;
@@ -398,20 +394,13 @@ private static object DeserializeModel(Type modelType, string value)
398394 var model = genericMethod . Invoke ( new JsonWrapper ( ) , new object [ ] { value } ) ;
399395 return model ;
400396 }
401-
402- private static void RegisterContext ( IServiceCollection serviceCollection , Type type , object context )
403- {
404- serviceCollection . AddSingleton (
405- type ,
406- context ) ;
407- }
408397 }
409398
410399 internal class JsonWrapper
411400 {
412401 public T Deserialize < T > ( string value )
413402 {
414- return JsonUtil . Deserialize < T > ( value ) ;
403+ return Json . Deserialize < T > ( value ) ;
415404 }
416405 }
417406}
0 commit comments