Skip to content

Commit 7dd1989

Browse files
committed
added custom location for data
removed redundant key field from serializer
1 parent a574260 commit 7dd1989

File tree

14 files changed

+118
-72
lines changed

14 files changed

+118
-72
lines changed

Example/Data/Context.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public class Context : DbContext
2121
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2222
{
2323
//Default: JSON-Serialize
24-
//optionsBuilder.UseFileContext();
24+
optionsBuilder.UseFileContext();
2525

26-
optionsBuilder.UseFileContext("bson");
26+
//optionsBuilder.UseFileContext("bson");
2727

2828
//JSON-Serialize + simple Encryption
2929
//optionsBuilder.UseFileContext("json", "encrypted");
@@ -33,10 +33,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3333
//optionsBuilder.UseFileContext("xml", "private");
3434

3535
//CSV
36-
//optionsBuilder.UseFileContext("csv");
36+
//optionsBuilder.UseFileContext("csv", location: @"C:\Users\mjanatzek\Documents\Projects\t");
3737

3838
//Excel
39-
//optionsBuilder.UseFileContext("excel");
39+
//optionsBuilder.UseFileContext("excel", databasename: "test", location: @"C:\Users\mjanatzek\Documents\Projects\t");
4040
}
4141

4242
protected override void OnModelCreating(ModelBuilder modelBuilder)

FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static class FileContextDbContextOptionsExtensions
3232
/// <returns> The options builder so that further configuration can be chained. </returns>
3333
public static DbContextOptionsBuilder<TContext> UseFileContext<TContext>(
3434
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
35-
string serializer = "json", string filemanager = "default", string databasename = "")
35+
string serializer = "json", string filemanager = "default", string databasename = "", string location = "")
3636
where TContext : DbContext
3737
=> (DbContextOptionsBuilder<TContext>)UseFileContext(
38-
(DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename);
38+
(DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename, location);
3939

4040
/// <summary>
4141
/// Configures the context to use FileContext.
@@ -49,14 +49,14 @@ public static DbContextOptionsBuilder<TContext> UseFileContext<TContext>(
4949
/// <param name="filemanager">The selection the of the file-manager to encrypt the files for example.</param>
5050
/// <returns> The options builder so that further configuration can be chained. </returns>
5151
public static DbContextOptionsBuilder UseFileContext(
52-
[NotNull] this DbContextOptionsBuilder optionsBuilder, string serializer = "json", string filemanager = "default", string databasename = "")
52+
[NotNull] this DbContextOptionsBuilder optionsBuilder, string serializer = "json", string filemanager = "default", string databasename = "", string location = "")
5353
{
5454
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
5555

5656
FileContextOptionsExtension extension = optionsBuilder.Options.FindExtension<FileContextOptionsExtension>()
5757
?? new FileContextOptionsExtension();
5858

59-
extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename);
59+
extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename, location);
6060

6161
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
6262

FileContextCore/FileContextCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<SignAssembly Condition="'$(OS)' == 'Windows_NT'">true</SignAssembly>
66
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<Version>2.2.6</Version>
8+
<Version>3.0.0</Version>
99
<Company>morrisjdev</Company>
1010
<Authors>morrisjdev</Authors>
1111
<Description>File provider for Entity Framework Core (to be used for development purposes)</Description>
@@ -19,7 +19,7 @@
1919
<PackageProjectUrl>https://github.com/morrisjdev/FileContextCore</PackageProjectUrl>
2020
<NeutralLanguage>en-US</NeutralLanguage>
2121
<DelaySign>false</DelaySign>
22-
<AssemblyVersion>2.2.6.0</AssemblyVersion>
22+
<AssemblyVersion>3.0.0.0</AssemblyVersion>
2323
</PropertyGroup>
2424

2525
<ItemGroup>

FileContextCore/FileManager/DefaultFileManager.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@ class DefaultFileManager : IFileManager
1212
IEntityType type;
1313
private readonly string filetype;
1414
private readonly string databasename;
15+
private readonly string _location;
1516

16-
public DefaultFileManager(IEntityType _type, string _filetype, string _databasename)
17+
public DefaultFileManager(IEntityType _type, string _filetype, string _databasename, string location)
1718
{
1819
type = _type;
1920
filetype = _filetype;
2021
databasename = _databasename;
22+
_location = location;
2123
}
2224

2325
public string GetFileName()
2426
{
2527
string name = type.Relational().TableName.GetValidFileName();
26-
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
27-
28+
29+
string path = string.IsNullOrEmpty(_location)
30+
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
31+
: _location;
32+
2833
Directory.CreateDirectory(path);
2934

3035
return Path.Combine(path, name + "." + filetype);

FileContextCore/FileManager/EncryptedFileManager.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@ class EncryptedFileManager : IFileManager
1515
private readonly string filetype;
1616
private readonly string key;
1717
private readonly string databasename;
18+
private readonly string _location;
1819

19-
public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename)
20+
public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename, string _location)
2021
{
2122
type = _type;
2223
filetype = _filetype;
2324
key = _key;
2425
databasename = _databasename;
26+
this._location = _location;
2527
}
2628

2729
public string GetFileName()
2830
{
29-
string name = type.Relational().TableName;
31+
string name = type.Relational().TableName.GetValidFileName();
3032

31-
foreach(char c in Path.GetInvalidFileNameChars())
32-
{
33-
name = name.Replace(c, '_');
34-
}
35-
36-
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
33+
string path = string.IsNullOrEmpty(_location)
34+
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
35+
: _location;
3736

38-
Directory.CreateDirectory(path);
37+
Directory.CreateDirectory(path);
3938

4039
return Path.Combine(path, name + "." + filetype + ".crypted");
4140
}

FileContextCore/FileManager/PrivateFileManager.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@ class PrivateFileManager : IFileManager
1414
IEntityType type;
1515
private readonly string filetype;
1616
private readonly string databasename;
17+
private readonly string _location;
1718

18-
public PrivateFileManager(IEntityType _type, string _filetype, string _databasename)
19+
public PrivateFileManager(IEntityType _type, string _filetype, string _databasename, string _location)
1920
{
2021
type = _type;
2122
filetype = _filetype;
2223
databasename = _databasename;
24+
this._location = _location;
2325
}
2426

2527
public string GetFileName()
2628
{
2729
string name = type.Relational().TableName.GetValidFileName();
28-
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
2930

30-
Directory.CreateDirectory(path);
31+
string path = string.IsNullOrEmpty(_location)
32+
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
33+
: _location;
34+
35+
Directory.CreateDirectory(path);
3136

3237
return Path.Combine(path, name + ".private." + filetype);
3338
}

FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class FileContextOptionsExtension : IDbContextOptionsExtension
1818
{
1919
private string _serializer = "json";
2020
private string _filemanager = "default";
21+
private string _location = "";
2122
private string _databasename = "";
2223

2324
private string _logFragment;
@@ -38,6 +39,7 @@ protected FileContextOptionsExtension([NotNull] FileContextOptionsExtension copy
3839
{
3940
_serializer = copyFrom._serializer;
4041
_filemanager = copyFrom._filemanager;
42+
_location = copyFrom._location;
4143
}
4244

4345
/// <summary>
@@ -56,17 +58,20 @@ protected FileContextOptionsExtension([NotNull] FileContextOptionsExtension copy
5658

5759
public virtual string DatabaseName => _databasename;
5860

61+
public virtual string Location => _location;
62+
5963
/// <summary>
6064
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
6165
/// directly from your code. This API may change or be removed in future releases.
6266
/// </summary>
63-
public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename)
67+
public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename, string location)
6468
{
6569
FileContextOptionsExtension clone = Clone();
6670

6771
clone._serializer = serializer;
6872
clone._filemanager = filemanager;
6973
clone._databasename = databasename;
74+
clone._location = location;
7075

7176
return clone;
7277
}
@@ -110,7 +115,11 @@ public virtual string LogFragment
110115
{
111116
StringBuilder builder = new StringBuilder();
112117

113-
builder.Append("serializer=").Append(_serializer).Append(";filemanager=").Append(_filemanager).Append(";databasename=").Append(_databasename).Append(' ');
118+
builder
119+
.Append("serializer=").Append(_serializer)
120+
.Append(";filemanager=").Append(_filemanager)
121+
.Append(";databasename=").Append(_databasename)
122+
.Append(";location=").Append(_location).Append(' ');
114123

115124
_logFragment = builder.ToString();
116125
}

FileContextCore/Serializer/BSONSerializer.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
using System.Linq;
88
using System.Text;
99
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
1011

1112
namespace FileContextCore.Serializer
1213
{
13-
class BSONSerializer : ISerializer
14+
class BSONSerializer<T> : ISerializer
1415
{
1516
private IEntityType entityType;
17+
private readonly IPrincipalKeyValueFactory<T> _keyValueFactory;
1618
private string[] propertyKeys;
1719
private readonly Type[] typeList;
1820

19-
public BSONSerializer(IEntityType _entityType)
21+
public BSONSerializer(IEntityType _entityType, IPrincipalKeyValueFactory<T> _keyValueFactory)
2022
{
2123
entityType = _entityType;
24+
this._keyValueFactory = _keyValueFactory;
2225
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
2326
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
2427
}
@@ -52,7 +55,9 @@ private void DeserializeValues<TKey>(JObject array, Dictionary<TKey, object[]> n
5255
{
5356
JObject json = (JObject)current.Value;
5457

55-
TKey key = (TKey)json.Value<string>("__Key__").Deserialize(typeof(TKey));
58+
TKey key = SerializerHelper.GetKey<TKey, T>(_keyValueFactory, entityType,
59+
propertyName => json.Value<string>(propertyName));
60+
5661
List<object> value = new List<object>();
5762

5863
for (int i = 0; i < propertyKeys.Length; i++)
@@ -79,9 +84,6 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)
7984
{
8085
writer.WriteStartObject();
8186

82-
writer.WritePropertyName("__Key__");
83-
writer.WriteValue(val.Key.Serialize());
84-
8587
for (int i = 0; i < propertyKeys.Length; i++)
8688
{
8789
writer.WritePropertyName(propertyKeys[i]);

FileContextCore/Serializer/CSVSerializer.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
using System.Linq;
77
using System.Text;
88
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
910

1011
namespace FileContextCore.Serializer
1112
{
12-
class CSVSerializer : ISerializer
13+
class CSVSerializer<T> : ISerializer
1314
{
1415
private IEntityType entityType;
16+
private readonly IPrincipalKeyValueFactory<T> _keyValueFactory;
1517
private string[] propertyKeys;
1618
private readonly Type[] typeList;
1719

18-
public CSVSerializer(IEntityType _entityType)
20+
public CSVSerializer(IEntityType _entityType, IPrincipalKeyValueFactory<T> _keyValueFactory)
1921
{
2022
entityType = _entityType;
23+
this._keyValueFactory = _keyValueFactory;
2124
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
2225
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
2326
}
@@ -37,7 +40,6 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey
3740

3841
while (reader.Read())
3942
{
40-
TKey key = (TKey)reader.GetField(0).Deserialize(typeof(TKey));
4143
List<object> value = new List<object>();
4244

4345
for (int i = 0; i < propertyKeys.Length; i++)
@@ -46,6 +48,9 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey
4648
value.Add(val);
4749
}
4850

51+
TKey key = SerializerHelper.GetKey<TKey, T>(_keyValueFactory, entityType,
52+
propertyName => reader.GetField(propertyName));
53+
4954
newList.Add(key, value.ToArray());
5055
}
5156

@@ -57,8 +62,6 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)
5762
StringWriter sw = new StringWriter();
5863
CsvWriter writer = new CsvWriter(sw);
5964

60-
writer.WriteField("Key");
61-
6265
for (int i = 0; i < propertyKeys.Length; i++)
6366
{
6467
writer.WriteField(propertyKeys[i]);
@@ -68,8 +71,6 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)
6871

6972
foreach (KeyValuePair<TKey, object[]> val in list)
7073
{
71-
writer.WriteField(val.Key.Serialize());
72-
7374
for (int i = 0; i < propertyKeys.Length; i++)
7475
{
7576
writer.WriteField(val.Value[i].Serialize());

0 commit comments

Comments
 (0)