Skip to content

Commit

Permalink
added custom location for data
Browse files Browse the repository at this point in the history
removed redundant key field from serializer
  • Loading branch information
morrisjdev committed Aug 23, 2019
1 parent a574260 commit 7dd1989
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 72 deletions.
8 changes: 4 additions & 4 deletions Example/Data/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class Context : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//Default: JSON-Serialize
//optionsBuilder.UseFileContext();
optionsBuilder.UseFileContext();

optionsBuilder.UseFileContext("bson");
//optionsBuilder.UseFileContext("bson");

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

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

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static class FileContextDbContextOptionsExtensions
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseFileContext<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
string serializer = "json", string filemanager = "default", string databasename = "")
string serializer = "json", string filemanager = "default", string databasename = "", string location = "")
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseFileContext(
(DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename);
(DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databasename, location);

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

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

extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename);
extension = extension.WithSerializerAndFileManager(serializer, filemanager, databasename, location);

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

Expand Down
4 changes: 2 additions & 2 deletions FileContextCore/FileContextCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SignAssembly Condition="'$(OS)' == 'Windows_NT'">true</SignAssembly>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.2.6</Version>
<Version>3.0.0</Version>
<Company>morrisjdev</Company>
<Authors>morrisjdev</Authors>
<Description>File provider for Entity Framework Core (to be used for development purposes)</Description>
Expand All @@ -19,7 +19,7 @@
<PackageProjectUrl>https://github.com/morrisjdev/FileContextCore</PackageProjectUrl>
<NeutralLanguage>en-US</NeutralLanguage>
<DelaySign>false</DelaySign>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 8 additions & 3 deletions FileContextCore/FileManager/DefaultFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ class DefaultFileManager : IFileManager
IEntityType type;
private readonly string filetype;
private readonly string databasename;
private readonly string _location;

public DefaultFileManager(IEntityType _type, string _filetype, string _databasename)
public DefaultFileManager(IEntityType _type, string _filetype, string _databasename, string location)
{
type = _type;
filetype = _filetype;
databasename = _databasename;
_location = location;
}

public string GetFileName()
{
string name = type.Relational().TableName.GetValidFileName();
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);


string path = string.IsNullOrEmpty(_location)
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
: _location;

Directory.CreateDirectory(path);

return Path.Combine(path, name + "." + filetype);
Expand Down
17 changes: 8 additions & 9 deletions FileContextCore/FileManager/EncryptedFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ class EncryptedFileManager : IFileManager
private readonly string filetype;
private readonly string key;
private readonly string databasename;
private readonly string _location;

public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename)
public EncryptedFileManager(IEntityType _type, string _filetype, string _key, string _databasename, string _location)
{
type = _type;
filetype = _filetype;
key = _key;
databasename = _databasename;
this._location = _location;
}

public string GetFileName()
{
string name = type.Relational().TableName;
string name = type.Relational().TableName.GetValidFileName();

foreach(char c in Path.GetInvalidFileNameChars())
{
name = name.Replace(c, '_');
}

string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);
string path = string.IsNullOrEmpty(_location)
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
: _location;

Directory.CreateDirectory(path);
Directory.CreateDirectory(path);

return Path.Combine(path, name + "." + filetype + ".crypted");
}
Expand Down
11 changes: 8 additions & 3 deletions FileContextCore/FileManager/PrivateFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ class PrivateFileManager : IFileManager
IEntityType type;
private readonly string filetype;
private readonly string databasename;
private readonly string _location;

public PrivateFileManager(IEntityType _type, string _filetype, string _databasename)
public PrivateFileManager(IEntityType _type, string _filetype, string _databasename, string _location)
{
type = _type;
filetype = _filetype;
databasename = _databasename;
this._location = _location;
}

public string GetFileName()
{
string name = type.Relational().TableName.GetValidFileName();
string path = Path.Combine(AppContext.BaseDirectory, "appdata", databasename);

Directory.CreateDirectory(path);
string path = string.IsNullOrEmpty(_location)
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
: _location;

Directory.CreateDirectory(path);

return Path.Combine(path, name + ".private." + filetype);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FileContextOptionsExtension : IDbContextOptionsExtension
{
private string _serializer = "json";
private string _filemanager = "default";
private string _location = "";
private string _databasename = "";

private string _logFragment;
Expand All @@ -38,6 +39,7 @@ protected FileContextOptionsExtension([NotNull] FileContextOptionsExtension copy
{
_serializer = copyFrom._serializer;
_filemanager = copyFrom._filemanager;
_location = copyFrom._location;
}

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

public virtual string DatabaseName => _databasename;

public virtual string Location => _location;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename)
public virtual FileContextOptionsExtension WithSerializerAndFileManager(string serializer, string filemanager, string databasename, string location)
{
FileContextOptionsExtension clone = Clone();

clone._serializer = serializer;
clone._filemanager = filemanager;
clone._databasename = databasename;
clone._location = location;

return clone;
}
Expand Down Expand Up @@ -110,7 +115,11 @@ public virtual string LogFragment
{
StringBuilder builder = new StringBuilder();

builder.Append("serializer=").Append(_serializer).Append(";filemanager=").Append(_filemanager).Append(";databasename=").Append(_databasename).Append(' ');
builder
.Append("serializer=").Append(_serializer)
.Append(";filemanager=").Append(_filemanager)
.Append(";databasename=").Append(_databasename)
.Append(";location=").Append(_location).Append(' ');

_logFragment = builder.ToString();
}
Expand Down
14 changes: 8 additions & 6 deletions FileContextCore/Serializer/BSONSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;

namespace FileContextCore.Serializer
{
class BSONSerializer : ISerializer
class BSONSerializer<T> : ISerializer
{
private IEntityType entityType;
private readonly IPrincipalKeyValueFactory<T> _keyValueFactory;
private string[] propertyKeys;
private readonly Type[] typeList;

public BSONSerializer(IEntityType _entityType)
public BSONSerializer(IEntityType _entityType, IPrincipalKeyValueFactory<T> _keyValueFactory)
{
entityType = _entityType;
this._keyValueFactory = _keyValueFactory;
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}
Expand Down Expand Up @@ -52,7 +55,9 @@ private void DeserializeValues<TKey>(JObject array, Dictionary<TKey, object[]> n
{
JObject json = (JObject)current.Value;

TKey key = (TKey)json.Value<string>("__Key__").Deserialize(typeof(TKey));
TKey key = SerializerHelper.GetKey<TKey, T>(_keyValueFactory, entityType,
propertyName => json.Value<string>(propertyName));

List<object> value = new List<object>();

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

writer.WritePropertyName("__Key__");
writer.WriteValue(val.Key.Serialize());

for (int i = 0; i < propertyKeys.Length; i++)
{
writer.WritePropertyName(propertyKeys[i]);
Expand Down
15 changes: 8 additions & 7 deletions FileContextCore/Serializer/CSVSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;

namespace FileContextCore.Serializer
{
class CSVSerializer : ISerializer
class CSVSerializer<T> : ISerializer
{
private IEntityType entityType;
private readonly IPrincipalKeyValueFactory<T> _keyValueFactory;
private string[] propertyKeys;
private readonly Type[] typeList;

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

while (reader.Read())
{
TKey key = (TKey)reader.GetField(0).Deserialize(typeof(TKey));
List<object> value = new List<object>();

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

TKey key = SerializerHelper.GetKey<TKey, T>(_keyValueFactory, entityType,
propertyName => reader.GetField(propertyName));

newList.Add(key, value.ToArray());
}

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

writer.WriteField("Key");

for (int i = 0; i < propertyKeys.Length; i++)
{
writer.WriteField(propertyKeys[i]);
Expand All @@ -68,8 +71,6 @@ public string Serialize<TKey>(Dictionary<TKey, object[]> list)

foreach (KeyValuePair<TKey, object[]> val in list)
{
writer.WriteField(val.Key.Serialize());

for (int i = 0; i < propertyKeys.Length; i++)
{
writer.WriteField(val.Value[i].Serialize());
Expand Down
Loading

0 comments on commit 7dd1989

Please sign in to comment.