Skip to content

Commit

Permalink
Merge pull request #5 from dfyx/model-builder-options
Browse files Browse the repository at this point in the history
Add support for some ModelBuilder options
  • Loading branch information
morrisjdev authored Aug 23, 2019
2 parents fb5e285 + 1791541 commit a574260
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 17 deletions.
1 change: 1 addition & 0 deletions FileContextCore/FileContextCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="EPPlus.Core" Version="1.5.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
Expand Down
3 changes: 2 additions & 1 deletion FileContextCore/FileManager/DefaultFileManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.FileManager
{
Expand All @@ -21,7 +22,7 @@ public DefaultFileManager(IEntityType _type, string _filetype, string _databasen

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

Directory.CreateDirectory(path);
Expand Down
3 changes: 2 additions & 1 deletion FileContextCore/FileManager/EncryptedFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.FileManager
{
Expand All @@ -25,7 +26,7 @@ public EncryptedFileManager(IEntityType _type, string _filetype, string _key, st

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

foreach(char c in Path.GetInvalidFileNameChars())
{
Expand Down
3 changes: 2 additions & 1 deletion FileContextCore/FileManager/PrivateFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.FileManager
{
Expand All @@ -23,7 +24,7 @@ public PrivateFileManager(IEntityType _type, string _filetype, string _databasen

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

Directory.CreateDirectory(path);
Expand Down
5 changes: 3 additions & 2 deletions FileContextCore/Serializer/BSONSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.Serializer
{
Expand All @@ -18,8 +19,8 @@ class BSONSerializer : ISerializer
public BSONSerializer(IEntityType _entityType)
{
entityType = _entityType;
propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray();
typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray();
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}

public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey, object[]> newList)
Expand Down
13 changes: 10 additions & 3 deletions FileContextCore/Serializer/CSVSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.Serializer
{
Expand All @@ -17,16 +18,22 @@ class CSVSerializer : ISerializer
public CSVSerializer(IEntityType _entityType)
{
entityType = _entityType;
propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray();
typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray();
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}

public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey, object[]> newList)
{
if (string.IsNullOrEmpty(list))
{
return new Dictionary<TKey, object[]>();
}

TextReader tr = new StringReader(list);
CsvReader reader = new CsvReader(tr);

reader.Read();
reader.ReadHeader();

while (reader.Read())
{
Expand All @@ -35,7 +42,7 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey

for (int i = 0; i < propertyKeys.Length; i++)
{
object val = reader.GetField(i + 1).Deserialize(typeList[i]);
object val = reader.GetField(propertyKeys[i]).Deserialize(typeList[i]);
value.Add(val);
}

Expand Down
5 changes: 3 additions & 2 deletions FileContextCore/Serializer/EXCELSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.Serializer
{
Expand Down Expand Up @@ -36,8 +37,8 @@ FileInfo GetFilePath()
public EXCELSerializer(IEntityType _entityType, string _password, string databaseName)
{
entityType = _entityType;
propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray();
typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray();
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
password = _password;
this.databaseName = databaseName;

Expand Down
5 changes: 3 additions & 2 deletions FileContextCore/Serializer/JSONSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.Serializer
{
Expand All @@ -15,8 +16,8 @@ class JSONSerializer : ISerializer
public JSONSerializer(IEntityType _entityType)
{
entityType = _entityType;
propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray();
typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray();
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}

public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey, object[]> newList)
Expand Down
5 changes: 3 additions & 2 deletions FileContextCore/Serializer/XMLSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Microsoft.EntityFrameworkCore;

namespace FileContextCore.Serializer
{
Expand All @@ -19,8 +20,8 @@ class XMLSerializer : ISerializer
public XMLSerializer(IEntityType _entityType)
{
entityType = _entityType;
propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray();
typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray();
propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray();
typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray();
}

public Dictionary<TKey, object[]> Deserialize<TKey>(string list, Dictionary<TKey, object[]> newList)
Expand Down
32 changes: 29 additions & 3 deletions FileContextCore/Storage/Internal/FileContextTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Update;

namespace FileContextCore.Storage.Internal
Expand Down Expand Up @@ -185,7 +186,7 @@ private Dictionary<TKey, object[]> InitExcel(string filetype)

UpdateMethod = new Action<Dictionary<TKey, object[]>>((list) =>
{
excel.Serialize(list);
excel.Serialize(ConvertToProvider(list));
});

Dictionary<TKey, object[]> newlist = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);
Expand Down Expand Up @@ -253,15 +254,40 @@ private Dictionary<TKey, object[]> Init()

UpdateMethod = new Action<Dictionary<TKey, object[]>>((list) =>
{
string cnt = serializer.Serialize(list);
string cnt = serializer.Serialize(ConvertToProvider(list));
fileManager.SaveContent(cnt);
});

string content = fileManager.LoadContent();
Dictionary<TKey, object[]> newList = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);
Dictionary<TKey, object[]> result = serializer.Deserialize(content, newList);
Dictionary<TKey, object[]> result = ConvertFromProvider(serializer.Deserialize(content, newList));
GenerateLastAutoPropertyValues(result);
return result;
}

private Dictionary<TKey, object[]> ApplyValueConverter(Dictionary<TKey, object[]> list, Func<ValueConverter, Func<object, object>> conversionFunc)
{
var result = new Dictionary<TKey, object[]>();
var converters = entityType.GetProperties().Select(p => p.GetValueConverter()).ToArray();
foreach (var keyValuePair in list)
{
result[keyValuePair.Key] = keyValuePair.Value.Select((value, index) =>
{
var converter = converters[index];
return converter == null ? value : conversionFunc(converter)(value);
}).ToArray();
}
return result;
}

private Dictionary<TKey, object[]> ConvertToProvider(Dictionary<TKey, object[]> list)
{
return ApplyValueConverter(list, converter => converter.ConvertToProvider);
}

private Dictionary<TKey, object[]> ConvertFromProvider(Dictionary<TKey, object[]> list)
{
return ApplyValueConverter(list, converter => converter.ConvertFromProvider);
}
}
}

0 comments on commit a574260

Please sign in to comment.