Skip to content

Commit aea9a25

Browse files
committed
Migrate to session storage
1 parent b8b6040 commit aea9a25

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

DomainModels/DataStorage.cs

+67-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SurveyJSAsFormLibrary.Attributes;
1+
using Microsoft.AspNetCore.Http;
2+
using SurveyJSAsFormLibrary.Attributes;
23
using System;
34
using System.Collections.Generic;
45
using System.ComponentModel.DataAnnotations;
@@ -9,18 +10,75 @@
910

1011
namespace SurveyJSAsFormLibrary.DomainModels
1112
{
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+
}
1239
public class DataStorage
1340
{
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+
}
1575
public static string GetForm(Type type)
1676
{
17-
string data;
18-
DataStorage.databaseEmulator.TryGetValue(DataStorage.formId(type), out data);
19-
return data;
77+
return GetDataFromDatabaseEmulator(DataStorage.formId(type));
2078
}
2179
public static void SaveForm(Type type, string data)
2280
{
23-
DataStorage.databaseEmulator[DataStorage.formId(type)] = data;
81+
SetDataFromDatabaseEmulator(DataStorage.formId(type), data);
2482
}
2583
public static string GenerateId()
2684
{
@@ -31,8 +89,7 @@ public static string GenerateId()
3189
}
3290
public static string GetData(Type type, string id)
3391
{
34-
string data;
35-
DataStorage.databaseEmulator.TryGetValue(DataStorage.absoluteId(type, id), out data);
92+
string data = GetDataFromDatabaseEmulator(DataStorage.absoluteId(type, id));
3693
if (string.IsNullOrEmpty(data))
3794
{
3895
data = getJSONWithId(id);
@@ -42,19 +99,11 @@ public static string GetData(Type type, string id)
4299
public static void SaveData(Type type, string id, string data)
43100
{
44101
string key = DataStorage.absoluteId(type, id);
45-
DataStorage.databaseEmulator[key] = data;
102+
SetDataFromDatabaseEmulator(key, data);
46103
}
47104
public static IList<string> GetAllDataByType(Type type)
48105
{
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 + "-");
58107
}
59108
private static string absoluteId(Type type, string id)
60109
{

Startup.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.HttpsPolicy;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Hosting;
8+
using SurveyJSAsFormLibrary.DomainModels;
79
using System;
810
using System.Collections.Generic;
911
using System.Linq;
@@ -24,6 +26,9 @@ public Startup(IConfiguration configuration)
2426
public void ConfigureServices(IServiceCollection services)
2527
{
2628
services.AddControllersWithViews();
29+
//We need these two lines to emulate database by storing data in user session
30+
services.AddSession();
31+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2732
}
2833

2934
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -41,8 +46,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4146
}
4247
app.UseHttpsRedirection();
4348
app.UseStaticFiles();
44-
4549
app.UseRouting();
50+
//We need these two lines to emulate database by storing data in user session
51+
app.UseSession();
52+
DatabaseEmulatorHttpContext.Services = app.ApplicationServices;
4653

4754
app.UseAuthorization();
4855

0 commit comments

Comments
 (0)