Skip to content

Commit ad30298

Browse files
committed
refactored code
1 parent 8dd4791 commit ad30298

File tree

15 files changed

+297
-253
lines changed

15 files changed

+297
-253
lines changed

Example/Data/Context.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public class Context : DbContext
2121
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2222
{
2323
//Default: JSON-Serialize
24-
optionsBuilder.UseFileContext();
24+
//optionsBuilder.UseFileContext();
25+
26+
optionsBuilder.UseFileContext("bson");
2527

2628
//JSON-Serialize + simple Encryption
2729
//optionsBuilder.UseFileContext("json", "encrypted");

Example/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ static void Main(string[] args)
155155
db4.SaveChanges();
156156

157157
users3 = db3.Users.ToList();
158+
159+
Console.WriteLine(db.Users.Count());
160+
161+
db.Users.Add(new User()
162+
{
163+
Name = "Test",
164+
Username = "testname"
165+
});
166+
db.SaveChanges();
167+
168+
Console.WriteLine(db.Users.Count());
169+
170+
Console.ReadKey();
158171
}
159172
}
160173
}

FileContextCore/FileManager/DefaultFileManager.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ public DefaultFileManager(IEntityType _type, string _filetype, string _databasen
2121

2222
public string GetFileName()
2323
{
24-
string name = type.Name;
25-
26-
foreach(char c in Path.GetInvalidFileNameChars())
27-
{
28-
name = name.Replace(c, '_');
29-
}
30-
24+
string name = type.Name.GetValidFileName();
3125
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
3226

3327
Directory.CreateDirectory(path);

FileContextCore/FileManager/EncryptedFileManager.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,35 @@ private string Decrypt(string str)
9696
try
9797
{
9898
str = str.Replace(" ", "+");
99-
byte[] cipherBytes = Convert.FromBase64String(str);
100-
using (Aes encryptor = Aes.Create())
101-
{
102-
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
103-
encryptor.Key = pdb.GetBytes(32);
104-
encryptor.IV = pdb.GetBytes(16);
105-
using (MemoryStream ms = new MemoryStream())
106-
{
107-
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
108-
{
109-
cs.Write(cipherBytes, 0, cipherBytes.Length);
110-
cs.Dispose();
111-
}
112-
str = Encoding.Unicode.GetString(ms.ToArray());
113-
}
114-
}
115-
return str;
99+
return UseAesDecryptor(str);
116100
}
117101
catch
118102
{
119103
return "";
120104
}
121105
}
122106

107+
private string UseAesDecryptor(string str)
108+
{
109+
byte[] cipherBytes = Convert.FromBase64String(str);
110+
using (Aes encryptor = Aes.Create())
111+
{
112+
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
113+
encryptor.Key = pdb.GetBytes(32);
114+
encryptor.IV = pdb.GetBytes(16);
115+
using (MemoryStream ms = new MemoryStream())
116+
{
117+
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
118+
{
119+
cs.Write(cipherBytes, 0, cipherBytes.Length);
120+
cs.Dispose();
121+
}
122+
str = Encoding.Unicode.GetString(ms.ToArray());
123+
}
124+
}
125+
return str;
126+
}
127+
123128
private string Encrypt(string str)
124129
{
125130
byte[] clearBytes = Encoding.Unicode.GetBytes(str);

FileContextCore/FileManager/PrivateFileManager.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ public PrivateFileManager(IEntityType _type, string _filetype, string _databasen
2323

2424
public string GetFileName()
2525
{
26-
string name = type.Name;
27-
28-
foreach(char c in Path.GetInvalidFileNameChars())
29-
{
30-
name = name.Replace(c, '_');
31-
}
32-
26+
string name = type.Name.GetValidFileName();
3327
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
3428

3529
Directory.CreateDirectory(path);

FileContextCore/Query/Internal/MaterializerFactory.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Linq.Expressions;
78
using FileContextCore.Utilities;
@@ -60,8 +61,37 @@ System.Collections.Generic.List<IEntityType> concreteEntityTypes
6061

6162
LabelTarget returnLabelTarget = Expression.Label(typeof(object));
6263

63-
Expression[] blockExpressions
64-
= new Expression[]
64+
Expression[] blockExpressions = CreateExpressions(entityTypeParameter, concreteEntityTypes, returnLabelTarget, valueBufferParameter);
65+
66+
foreach (IEntityType concreteEntityType in concreteEntityTypes.Skip(1))
67+
{
68+
blockExpressions[0] = ProcessExpression(entityTypeParameter, concreteEntityType, returnLabelTarget, valueBufferParameter, blockExpressions);
69+
}
70+
71+
return Expression.Lambda<Func<IEntityType, ValueBuffer, object>>(
72+
Expression.Block(blockExpressions),
73+
entityTypeParameter,
74+
valueBufferParameter);
75+
}
76+
77+
private Expression ProcessExpression(ParameterExpression entityTypeParameter, IEntityType concreteEntityType,
78+
LabelTarget returnLabelTarget, ParameterExpression valueBufferParameter, Expression[] blockExpressions)
79+
{
80+
return Expression.IfThenElse(
81+
Expression.Equal(
82+
entityTypeParameter,
83+
Expression.Constant(concreteEntityType)),
84+
Expression.Return(
85+
returnLabelTarget,
86+
_entityMaterializerSource
87+
.CreateMaterializeExpression(concreteEntityType, valueBufferParameter)),
88+
blockExpressions[0]);
89+
}
90+
91+
private Expression[] CreateExpressions(ParameterExpression entityTypeParameter, List<IEntityType> concreteEntityTypes,
92+
LabelTarget returnLabelTarget, ParameterExpression valueBufferParameter)
93+
{
94+
return new Expression[]
6595
{
6696
Expression.IfThen(
6797
Expression.Equal(
@@ -76,25 +106,6 @@ Expression[] blockExpressions
76106
returnLabelTarget,
77107
Expression.Default(returnLabelTarget.Type))
78108
};
79-
80-
foreach (IEntityType concreteEntityType in concreteEntityTypes.Skip(1))
81-
{
82-
blockExpressions[0]
83-
= Expression.IfThenElse(
84-
Expression.Equal(
85-
entityTypeParameter,
86-
Expression.Constant(concreteEntityType)),
87-
Expression.Return(
88-
returnLabelTarget,
89-
_entityMaterializerSource
90-
.CreateMaterializeExpression(concreteEntityType, valueBufferParameter)),
91-
blockExpressions[0]);
92-
}
93-
94-
return Expression.Lambda<Func<IEntityType, ValueBuffer, object>>(
95-
Expression.Block(blockExpressions),
96-
entityTypeParameter,
97-
valueBufferParameter);
98109
}
99110
}
100111
}

FileContextCore/Serializer/BSONSerializer.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BSONSerializer : ISerializer
1313
{
1414
private IEntityType entityType;
1515
private string[] propertyKeys;
16-
private Type[] typeList;
16+
private readonly Type[] typeList;
1717

1818
public BSONSerializer(IEntityType _entityType)
1919
{
@@ -35,29 +35,35 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey
3535

3636
if (array != null)
3737
{
38-
JProperty current = (JProperty)array.First;
38+
DeserializeValues(array, newList);
3939

40-
while (current != null)
41-
{
42-
JObject json = (JObject)current.Value;
40+
}
41+
}
4342

44-
TKey key = (TKey)json.Value<string>("__Key__").Deserialize(typeof(TKey));
45-
List<object> value = new List<object>();
43+
return newList;
44+
}
45+
46+
private void DeserializeValues<TKey>(JObject array, Dictionary<TKey, object[]> newList)
47+
{
48+
JProperty current = (JProperty)array.First;
4649

47-
for (int i = 0; i < propertyKeys.Length; i++)
48-
{
49-
object val = json.Value<string>(propertyKeys[i]).Deserialize(typeList[i]);
50-
value.Add(val);
51-
}
50+
while (current != null)
51+
{
52+
JObject json = (JObject)current.Value;
5253

53-
newList.Add(key, value.ToArray());
54+
TKey key = (TKey)json.Value<string>("__Key__").Deserialize(typeof(TKey));
55+
List<object> value = new List<object>();
5456

55-
current = (JProperty)current.Next;
56-
}
57+
for (int i = 0; i < propertyKeys.Length; i++)
58+
{
59+
object val = json.Value<string>(propertyKeys[i]).Deserialize(typeList[i]);
60+
value.Add(val);
5761
}
58-
}
5962

60-
return newList;
63+
newList.Add(key, value.ToArray());
64+
65+
current = (JProperty)current.Next;
66+
}
6167
}
6268

6369
public string Serialize<TKey>(Dictionary<TKey, object[]> list)

FileContextCore/Serializer/EXCELSerializer.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(Dictionary<TKey, object[]> n
110110
}
111111

112112
public void Serialize<TKey>(Dictionary<TKey, object[]> list)
113+
{
114+
FillRows(list);
115+
116+
for (int x = 0; x < worksheet.Dimension.Columns; x++)
117+
{
118+
worksheet.Column(x + 1).AutoFit();
119+
}
120+
121+
DeleteUnusedRows(list.Count + 1);
122+
123+
if (!String.IsNullOrEmpty(password))
124+
{
125+
package.Save(password);
126+
}
127+
else
128+
{
129+
package.Save();
130+
}
131+
}
132+
133+
private void FillRows<TKey>(Dictionary<TKey, object[]> list)
113134
{
114135
int y = 2;
115136

@@ -124,28 +145,15 @@ public void Serialize<TKey>(Dictionary<TKey, object[]> list)
124145

125146
y++;
126147
}
148+
}
127149

128-
for (int x = 0; x < worksheet.Dimension.Columns; x++)
129-
{
130-
worksheet.Column(x + 1).AutoFit();
131-
}
132-
133-
int lastRow = list.Count + 1;
134-
150+
private void DeleteUnusedRows(int lastRow)
151+
{
135152
if (worksheet.Dimension.Rows > lastRow)
136153
{
137154
int count = worksheet.Dimension.Rows - lastRow;
138155
worksheet.DeleteRow(lastRow + 1, count);
139156
}
140-
141-
if (!String.IsNullOrEmpty(password))
142-
{
143-
package.Save(password);
144-
}
145-
else
146-
{
147-
package.Save();
148-
}
149157
}
150158
}
151159
}

FileContextCore/Serializer/SerializerHelper.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ static class SerializerHelper
1010
public static object Deserialize(this string input, Type type)
1111
{
1212
if (String.IsNullOrEmpty(input))
13-
{
1413
return type.GetDefaultValue();
15-
}
1614

1715
if (type == typeof(TimeSpan))
18-
{
1916
return TimeSpan.Parse(input, CultureInfo.InvariantCulture);
20-
}
2117
else if (type == typeof(Guid))
22-
{
2318
return Guid.Parse(input);
24-
}
2519
else if (type.IsArray)
2620
{
2721
Type arrType = type.GetElementType();
@@ -35,9 +29,7 @@ public static object Deserialize(this string input, Type type)
3529
return arr.ToArray();
3630
}
3731
else
38-
{
3932
return Convert.ChangeType(input, type, CultureInfo.InvariantCulture);
40-
}
4133
}
4234

4335
public static string Serialize(this object input)

FileContextCore/Serializer/XMLSerializer.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class XMLSerializer : ISerializer
1414
{
1515
private IEntityType entityType;
1616
private string[] propertyKeys;
17-
private Type[] typeList;
17+
private readonly Type[] typeList;
1818

1919
public XMLSerializer(IEntityType _entityType)
2020
{
@@ -67,6 +67,17 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)
6767
string[] nameParts = entityType.Name.Split('.');
6868
string name = nameParts[nameParts.Length - 1].Replace("<", "").Replace(">", "");
6969

70+
WriteList(writer, name, list);
71+
72+
writer.WriteEndElement();
73+
writer.WriteEndDocument();
74+
writer.Close();
75+
76+
return sw.ToString();
77+
}
78+
79+
private void WriteList<TKey>(XmlWriter writer, string name, Dictionary<TKey, object[]> list)
80+
{
7081
foreach (KeyValuePair<TKey, object[]> val in list)
7182
{
7283
writer.WriteStartElement(name);
@@ -81,12 +92,6 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)
8192

8293
writer.WriteEndElement();
8394
}
84-
85-
writer.WriteEndElement();
86-
writer.WriteEndDocument();
87-
writer.Close();
88-
89-
return sw.ToString();
9095
}
9196
}
9297
}

0 commit comments

Comments
 (0)