1
- using SurveyJSAsFormLibrary . Attributes ;
1
+ using Microsoft . AspNetCore . Http ;
2
+ using SurveyJSAsFormLibrary . Attributes ;
2
3
using System ;
3
4
using System . Collections . Generic ;
4
5
using System . ComponentModel . DataAnnotations ;
9
10
10
11
namespace SurveyJSAsFormLibrary . DomainModels
11
12
{
13
+ public static class DatabaseEmulatorHttpContext
14
+ {
15
+ static IServiceProvider services = null ;
16
+
17
+ public static IServiceProvider Services
18
+ {
19
+ get { return services ; }
20
+ set
21
+ {
22
+ if ( services != null )
23
+ {
24
+ throw new Exception ( "Can't set once a value has already been set." ) ;
25
+ }
26
+ services = value ;
27
+ }
28
+ }
29
+ public static HttpContext Current
30
+ {
31
+ get
32
+ {
33
+ IHttpContextAccessor httpContextAccessor = services . GetService ( typeof ( IHttpContextAccessor ) ) as IHttpContextAccessor ;
34
+ return httpContextAccessor ? . HttpContext ;
35
+ }
36
+ }
37
+
38
+ }
12
39
public class DataStorage
13
40
{
14
- private static Dictionary < string , string > databaseEmulator = new Dictionary < string , string > ( ) ;
41
+ const string idsName = "ids" ;
42
+ private static ISession currentSession {
43
+ get { return DatabaseEmulatorHttpContext . Current . Session ; }
44
+ }
45
+ private static string GetDataFromDatabaseEmulator ( string key )
46
+ {
47
+ return currentSession . GetString ( key ) ;
48
+ }
49
+ private static void SetDataFromDatabaseEmulator ( string key , string data )
50
+ {
51
+ currentSession . SetString ( key , data ) ;
52
+ string ids = currentSession . GetString ( idsName ) ;
53
+ if ( string . IsNullOrEmpty ( ids ) ) ids = string . Empty ;
54
+ key += ";" ;
55
+ if ( ! ids . Contains ( key ) )
56
+ {
57
+ currentSession . SetString ( idsName , ids + key ) ;
58
+ }
59
+ }
60
+ public static IList < string > GetAllIdsByPrefixFromDatabaseEmulator ( string keyPrefix )
61
+ {
62
+ var res = new List < string > ( ) ;
63
+ string ids = currentSession . GetString ( idsName ) ;
64
+ if ( string . IsNullOrEmpty ( ids ) ) return res ;
65
+ string [ ] allKeys = ids . Split ( ';' ) ;
66
+ foreach ( string key in allKeys )
67
+ {
68
+ if ( ! string . IsNullOrEmpty ( key ) && key . StartsWith ( keyPrefix ) )
69
+ {
70
+ res . Add ( GetDataFromDatabaseEmulator ( key ) ) ;
71
+ }
72
+ }
73
+ return res ;
74
+ }
15
75
public static string GetForm ( Type type )
16
76
{
17
- string data ;
18
- DataStorage . databaseEmulator . TryGetValue ( DataStorage . formId ( type ) , out data ) ;
19
- return data ;
77
+ return GetDataFromDatabaseEmulator ( DataStorage . formId ( type ) ) ;
20
78
}
21
79
public static void SaveForm ( Type type , string data )
22
80
{
23
- DataStorage . databaseEmulator [ DataStorage . formId ( type ) ] = data ;
81
+ SetDataFromDatabaseEmulator ( DataStorage . formId ( type ) , data ) ;
24
82
}
25
83
public static string GenerateId ( )
26
84
{
@@ -31,8 +89,7 @@ public static string GenerateId()
31
89
}
32
90
public static string GetData ( Type type , string id )
33
91
{
34
- string data ;
35
- DataStorage . databaseEmulator . TryGetValue ( DataStorage . absoluteId ( type , id ) , out data ) ;
92
+ string data = GetDataFromDatabaseEmulator ( DataStorage . absoluteId ( type , id ) ) ;
36
93
if ( string . IsNullOrEmpty ( data ) )
37
94
{
38
95
data = getJSONWithId ( id ) ;
@@ -42,19 +99,11 @@ public static string GetData(Type type, string id)
42
99
public static void SaveData ( Type type , string id , string data )
43
100
{
44
101
string key = DataStorage . absoluteId ( type , id ) ;
45
- DataStorage . databaseEmulator [ key ] = data ;
102
+ SetDataFromDatabaseEmulator ( key , data ) ;
46
103
}
47
104
public static IList < string > GetAllDataByType ( Type type )
48
105
{
49
- var res = new List < string > ( ) ;
50
- foreach ( KeyValuePair < string , string > item in databaseEmulator )
51
- {
52
- if ( item . Key . StartsWith ( type . Name + "-" ) )
53
- {
54
- res . Add ( item . Value ) ;
55
- }
56
- }
57
- return res ;
106
+ return GetAllIdsByPrefixFromDatabaseEmulator ( type . Name + "-" ) ;
58
107
}
59
108
private static string absoluteId ( Type type , string id )
60
109
{
0 commit comments