Skip to content

Commit aee913b

Browse files
committed
refactoring of usage and configuration of serializer and file manager
1 parent f11ba5c commit aee913b

20 files changed

+597
-548
lines changed

Example/Data/Context.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Microsoft.EntityFrameworkCore;
33
using Example.Data.Entities;
44
using FileContextCore;
5+
using FileContextCore.FileManager;
6+
using FileContextCore.Serializer;
57

68
namespace Example.Data
79
{
@@ -22,7 +24,9 @@ public class Context : DbContext
2224
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2325
{
2426
//Default: JSON-Serialize
25-
optionsBuilder.UseFileContextDatabase();
27+
// optionsBuilder.UseFileContextDatabase();
28+
29+
// optionsBuilder.UseFileContextDatabase<JSONSerializer, DefaultFileManager>();
2630

2731
//optionsBuilder.UseFileContextDatabase("bson");
2832

FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 182 additions & 58 deletions
Large diffs are not rendered by default.

FileContextCore/FileManager/DefaultFileManager.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
using Microsoft.EntityFrameworkCore.Metadata;
22
using System;
33
using System.IO;
4+
using FileContextCore.Infrastructure.Internal;
45
using Microsoft.EntityFrameworkCore;
56

67
namespace FileContextCore.FileManager
78
{
8-
class DefaultFileManager : IFileManager
9+
public class DefaultFileManager : IFileManager
910
{
10-
private readonly object thisLock = new object();
11+
private readonly object _thisLock = new object();
1112

12-
IEntityType type;
13-
private readonly string filetype;
14-
private readonly string databasename;
15-
private readonly string _location;
13+
IEntityType _type;
14+
private string _filetype;
15+
private string _databasename;
16+
private string _location;
1617

17-
public DefaultFileManager(IEntityType _type, string _filetype, string _databasename, string location)
18+
public DefaultFileManager() { }
19+
20+
public void Initialize(IFileContextScopedOptions options, IEntityType entityType, string fileType)
1821
{
19-
type = _type;
20-
filetype = _filetype;
21-
databasename = _databasename ?? "";
22-
_location = location;
22+
_type = entityType;
23+
_filetype = fileType;
24+
_databasename = options.DatabaseName ?? "";
25+
_location = options.Location;
2326
}
2427

2528
public string GetFileName()
2629
{
27-
string name = type.GetTableName().GetValidFileName();
30+
string name = _type.GetTableName().GetValidFileName();
2831

2932
string path = string.IsNullOrEmpty(_location)
30-
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
33+
? Path.Combine(AppContext.BaseDirectory, "appdata", _databasename)
3134
: _location;
3235

3336
Directory.CreateDirectory(path);
3437

35-
return Path.Combine(path, name + "." + filetype);
38+
return Path.Combine(path, name + "." + _filetype);
3639
}
3740

3841
public string LoadContent()
3942
{
40-
lock (thisLock)
43+
lock (_thisLock)
4144
{
4245
string path = GetFileName();
4346

@@ -52,7 +55,7 @@ public string LoadContent()
5255

5356
public void SaveContent(string content)
5457
{
55-
lock (thisLock)
58+
lock (_thisLock)
5659
{
5760
string path = GetFileName();
5861
File.WriteAllText(path, content);
@@ -61,7 +64,7 @@ public void SaveContent(string content)
6164

6265
public bool Clear()
6366
{
64-
lock (thisLock)
67+
lock (_thisLock)
6568
{
6669
FileInfo fi = new FileInfo(GetFileName());
6770

@@ -70,16 +73,14 @@ public bool Clear()
7073
fi.Delete();
7174
return true;
7275
}
73-
else
74-
{
75-
return false;
76-
}
76+
77+
return false;
7778
}
7879
}
7980

8081
public bool FileExists()
8182
{
82-
lock (thisLock)
83+
lock (_thisLock)
8384
{
8485
FileInfo fi = new FileInfo(GetFileName());
8586

FileContextCore/FileManager/EncryptedFileManager.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,46 @@
33
using System.IO;
44
using System.Security.Cryptography;
55
using System.Text;
6+
using FileContextCore.Infrastructure.Internal;
67
using Microsoft.EntityFrameworkCore;
78

89
namespace FileContextCore.FileManager
910
{
10-
class EncryptedFileManager : IFileManager
11+
public class EncryptedFileManager : IFileManager
1112
{
12-
private readonly object thisLock = new object();
13+
private readonly object _thisLock = new object();
1314

14-
IEntityType type;
15-
private readonly string filetype;
16-
private readonly string key;
17-
private readonly string databasename;
18-
private readonly string _location;
15+
IEntityType _type;
16+
private string _filetype;
17+
private string _key;
18+
private string _databasename;
19+
private string _location;
1920

20-
public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename, string _location)
21+
public void Initialize(IFileContextScopedOptions options, IEntityType entityType, string fileType)
2122
{
22-
type = _type;
23-
filetype = _filetype;
24-
key = _key;
25-
databasename = _databasename ?? "";
26-
this._location = _location;
23+
_type = entityType;
24+
_filetype = fileType;
25+
_key = options.Password;
26+
_databasename = options.DatabaseName ?? "";
27+
_location = options.Location;
2728
}
28-
29+
2930
public string GetFileName()
3031
{
31-
string name = type.GetTableName().GetValidFileName();
32+
string name = _type.GetTableName().GetValidFileName();
3233

3334
string path = string.IsNullOrEmpty(_location)
34-
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
35+
? Path.Combine(AppContext.BaseDirectory, "appdata", _databasename)
3536
: _location;
3637

3738
Directory.CreateDirectory(path);
3839

39-
return Path.Combine(path, name + "." + filetype + ".encrypted");
40+
return Path.Combine(path, name + "." + _filetype + ".encrypted");
4041
}
4142

4243
public string LoadContent()
4344
{
44-
lock (thisLock)
45+
lock (_thisLock)
4546
{
4647
string path = GetFileName();
4748

@@ -56,7 +57,7 @@ public string LoadContent()
5657

5758
public void SaveContent(string content)
5859
{
59-
lock (thisLock)
60+
lock (_thisLock)
6061
{
6162
string path = GetFileName();
6263
File.WriteAllText(path, Encrypt(content));
@@ -65,7 +66,7 @@ public void SaveContent(string content)
6566

6667
public bool Clear()
6768
{
68-
lock (thisLock)
69+
lock (_thisLock)
6970
{
7071
FileInfo fi = new FileInfo(GetFileName());
7172

@@ -83,7 +84,7 @@ public bool Clear()
8384

8485
public bool FileExists()
8586
{
86-
lock (thisLock)
87+
lock (_thisLock)
8788
{
8889
FileInfo fi = new FileInfo(GetFileName());
8990

@@ -109,7 +110,7 @@ private string UseAesDecryptor(string str)
109110
byte[] cipherBytes = Convert.FromBase64String(str);
110111
using (Aes encryptor = Aes.Create())
111112
{
112-
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
113+
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
113114
encryptor.Key = pdb.GetBytes(32);
114115
encryptor.IV = pdb.GetBytes(16);
115116
using (MemoryStream ms = new MemoryStream())
@@ -130,7 +131,7 @@ private string Encrypt(string str)
130131
byte[] clearBytes = Encoding.Unicode.GetBytes(str);
131132
using (Aes encryptor = Aes.Create())
132133
{
133-
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
134+
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
134135
encryptor.Key = pdb.GetBytes(32);
135136
encryptor.IV = pdb.GetBytes(16);
136137
using (MemoryStream ms = new MemoryStream())

FileContextCore/FileManager/IFileManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace FileContextCore.FileManager
1+
using FileContextCore.Infrastructure.Internal;
2+
using Microsoft.EntityFrameworkCore.Metadata;
3+
4+
namespace FileContextCore.FileManager
25
{
36
public interface IFileManager
47
{
@@ -11,5 +14,7 @@ public interface IFileManager
1114
bool Clear();
1215

1316
bool FileExists();
17+
18+
void Initialize(IFileContextScopedOptions options, IEntityType entityType, string fileType);
1419
}
1520
}
Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
using Microsoft.EntityFrameworkCore.Metadata;
2-
using System;
1+
using System;
32
using System.IO;
4-
using System.Security.Cryptography;
5-
using System.Text;
3+
using FileContextCore.Infrastructure.Internal;
64
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Metadata;
76

87
namespace FileContextCore.FileManager
98
{
10-
class PrivateFileManager : IFileManager
9+
public class PrivateFileManager : IFileManager
1110
{
12-
private readonly object thisLock = new object();
11+
private readonly object _thisLock = new object();
1312

14-
IEntityType type;
15-
private readonly string filetype;
16-
private readonly string databasename;
17-
private readonly string _location;
13+
IEntityType _type;
14+
private string _filetype;
15+
private string _databasename;
16+
private string _location;
1817

19-
public PrivateFileManager(IEntityType _type, string _filetype, string _databasename, string _location)
18+
public void Initialize(IFileContextScopedOptions options, IEntityType entityType, string fileType)
2019
{
21-
type = _type;
22-
filetype = _filetype;
23-
databasename = _databasename ?? "";
24-
this._location = _location;
20+
_type = entityType;
21+
_filetype = fileType;
22+
_databasename = options.DatabaseName ?? "";
23+
_location = options.Location;
2524
}
26-
25+
2726
public string GetFileName()
2827
{
29-
string name = type.GetTableName().GetValidFileName();
28+
string name = _type.GetTableName().GetValidFileName();
3029

3130
string path = string.IsNullOrEmpty(_location)
32-
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
31+
? Path.Combine(AppContext.BaseDirectory, "appdata", _databasename)
3332
: _location;
3433

3534
Directory.CreateDirectory(path);
3635

37-
return Path.Combine(path, name + ".private." + filetype);
36+
return Path.Combine(path, name + ".private." + _filetype);
3837
}
3938

4039
public string LoadContent()
4140
{
42-
lock (thisLock)
41+
lock (_thisLock)
4342
{
4443
string path = GetFileName();
4544

@@ -54,7 +53,7 @@ public string LoadContent()
5453

5554
public void SaveContent(string content)
5655
{
57-
lock (thisLock)
56+
lock (_thisLock)
5857
{
5958
string path = GetFileName();
6059
File.WriteAllText(path, content);
@@ -64,7 +63,7 @@ public void SaveContent(string content)
6463

6564
public bool Clear()
6665
{
67-
lock (thisLock)
66+
lock (_thisLock)
6867
{
6968
FileInfo fi = new FileInfo(GetFileName());
7069

@@ -82,24 +81,12 @@ public bool Clear()
8281

8382
public bool FileExists()
8483
{
85-
lock (thisLock)
84+
lock (_thisLock)
8685
{
8786
FileInfo fi = new FileInfo(GetFileName());
8887

8988
return fi.Exists;
9089
}
9190
}
92-
93-
private void AddEncryption()
94-
{
95-
FileInfo fi = new FileInfo(GetFileName());
96-
97-
if (!fi.Exists)
98-
{
99-
fi.Create();
100-
}
101-
102-
fi.Encrypt();
103-
}
10491
}
10592
}

0 commit comments

Comments
 (0)