From 96e0ce70a64346d9e4821e6720cce8b7e3ea3397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Fri, 16 Aug 2019 12:26:51 +0200 Subject: [PATCH 1/8] Add Microsoft.EntityFrameworkCore.Relational --- FileContextCore/FileContextCore.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/FileContextCore/FileContextCore.csproj b/FileContextCore/FileContextCore.csproj index 276561c..ffc706c 100644 --- a/FileContextCore/FileContextCore.csproj +++ b/FileContextCore/FileContextCore.csproj @@ -26,6 +26,7 @@ + From c1cacf746aecd97273eea6338cdf3905a1fbadba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Fri, 16 Aug 2019 12:28:30 +0200 Subject: [PATCH 2/8] Use TableName als file name --- FileContextCore/FileManager/DefaultFileManager.cs | 3 ++- FileContextCore/FileManager/EncryptedFileManager.cs | 3 ++- FileContextCore/FileManager/PrivateFileManager.cs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/FileContextCore/FileManager/DefaultFileManager.cs b/FileContextCore/FileManager/DefaultFileManager.cs index c7e5e7c..52fe668 100644 --- a/FileContextCore/FileManager/DefaultFileManager.cs +++ b/FileContextCore/FileManager/DefaultFileManager.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore.Metadata; using System; using System.IO; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.FileManager { @@ -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); diff --git a/FileContextCore/FileManager/EncryptedFileManager.cs b/FileContextCore/FileManager/EncryptedFileManager.cs index d42feb7..6e61a67 100644 --- a/FileContextCore/FileManager/EncryptedFileManager.cs +++ b/FileContextCore/FileManager/EncryptedFileManager.cs @@ -3,6 +3,7 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.FileManager { @@ -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()) { diff --git a/FileContextCore/FileManager/PrivateFileManager.cs b/FileContextCore/FileManager/PrivateFileManager.cs index 8fc21a2..b7343c8 100644 --- a/FileContextCore/FileManager/PrivateFileManager.cs +++ b/FileContextCore/FileManager/PrivateFileManager.cs @@ -3,6 +3,7 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.FileManager { @@ -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); From eb7f1677fcf6e41448bf25b20df96306344917c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Fri, 16 Aug 2019 12:31:21 +0200 Subject: [PATCH 3/8] Use ColumnName for column names --- FileContextCore/Serializer/BSONSerializer.cs | 3 ++- FileContextCore/Serializer/CSVSerializer.cs | 3 ++- FileContextCore/Serializer/EXCELSerializer.cs | 3 ++- FileContextCore/Serializer/JSONSerializer.cs | 3 ++- FileContextCore/Serializer/XMLSerializer.cs | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/FileContextCore/Serializer/BSONSerializer.cs b/FileContextCore/Serializer/BSONSerializer.cs index 0111fe2..f59b730 100644 --- a/FileContextCore/Serializer/BSONSerializer.cs +++ b/FileContextCore/Serializer/BSONSerializer.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Text; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.Serializer { @@ -18,7 +19,7 @@ class BSONSerializer : ISerializer public BSONSerializer(IEntityType _entityType) { entityType = _entityType; - propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray(); + propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); } diff --git a/FileContextCore/Serializer/CSVSerializer.cs b/FileContextCore/Serializer/CSVSerializer.cs index ed87a13..43ed5b0 100644 --- a/FileContextCore/Serializer/CSVSerializer.cs +++ b/FileContextCore/Serializer/CSVSerializer.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Text; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.Serializer { @@ -17,7 +18,7 @@ class CSVSerializer : ISerializer public CSVSerializer(IEntityType _entityType) { entityType = _entityType; - propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray(); + propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); } diff --git a/FileContextCore/Serializer/EXCELSerializer.cs b/FileContextCore/Serializer/EXCELSerializer.cs index 6382392..8367074 100644 --- a/FileContextCore/Serializer/EXCELSerializer.cs +++ b/FileContextCore/Serializer/EXCELSerializer.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Text; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.Serializer { @@ -36,7 +37,7 @@ FileInfo GetFilePath() public EXCELSerializer(IEntityType _entityType, string _password, string databaseName) { entityType = _entityType; - propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray(); + propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); password = _password; this.databaseName = databaseName; diff --git a/FileContextCore/Serializer/JSONSerializer.cs b/FileContextCore/Serializer/JSONSerializer.cs index 7f44245..f766bcd 100644 --- a/FileContextCore/Serializer/JSONSerializer.cs +++ b/FileContextCore/Serializer/JSONSerializer.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.Serializer { @@ -15,7 +16,7 @@ class JSONSerializer : ISerializer public JSONSerializer(IEntityType _entityType) { entityType = _entityType; - propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray(); + propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); } diff --git a/FileContextCore/Serializer/XMLSerializer.cs b/FileContextCore/Serializer/XMLSerializer.cs index 6943ca3..13015f0 100644 --- a/FileContextCore/Serializer/XMLSerializer.cs +++ b/FileContextCore/Serializer/XMLSerializer.cs @@ -7,6 +7,7 @@ using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; +using Microsoft.EntityFrameworkCore; namespace FileContextCore.Serializer { @@ -19,7 +20,7 @@ class XMLSerializer : ISerializer public XMLSerializer(IEntityType _entityType) { entityType = _entityType; - propertyKeys = entityType.GetProperties().Select(p => p.Name).ToArray(); + propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); } From b0c679c0711ef08f89999af9891aa555323b33df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Fri, 16 Aug 2019 12:32:33 +0200 Subject: [PATCH 4/8] Apply value converters --- .../Storage/Internal/FileContextTable.cs | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/FileContextCore/Storage/Internal/FileContextTable.cs b/FileContextCore/Storage/Internal/FileContextTable.cs index ddca258..341cbeb 100644 --- a/FileContextCore/Storage/Internal/FileContextTable.cs +++ b/FileContextCore/Storage/Internal/FileContextTable.cs @@ -185,7 +185,7 @@ private Dictionary InitExcel(string filetype) UpdateMethod = new Action>((list) => { - excel.Serialize(list); + excel.Serialize(ConvertToProvider(list)); }); Dictionary newlist = new Dictionary(_keyValueFactory.EqualityComparer); @@ -253,15 +253,44 @@ private Dictionary Init() UpdateMethod = new Action>((list) => { - string cnt = serializer.Serialize(list); + string cnt = serializer.Serialize(ConvertToProvider(list)); fileManager.SaveContent(cnt); }); string content = fileManager.LoadContent(); Dictionary newList = new Dictionary(_keyValueFactory.EqualityComparer); - Dictionary result = serializer.Deserialize(content, newList); + Dictionary result = ConvertFromProvider(serializer.Deserialize(content, newList)); GenerateLastAutoPropertyValues(result); return result; } + + private Dictionary ConvertToProvider(Dictionary list) + { + var result = new Dictionary(); + 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 : converter.ConvertToProvider(value); + }).ToArray(); + } + return result; + } + private Dictionary ConvertFromProvider(Dictionary list) + { + var result = new Dictionary(); + 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 : converter.ConvertFromProvider(value); + }).ToArray(); + } + return result; + } } } From 035c941e76b88f2ddfafcb0fec6deede86156933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Fri, 16 Aug 2019 12:41:36 +0200 Subject: [PATCH 5/8] Refactor duplicate code --- .../Storage/Internal/FileContextTable.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/FileContextCore/Storage/Internal/FileContextTable.cs b/FileContextCore/Storage/Internal/FileContextTable.cs index 341cbeb..6bdd6cd 100644 --- a/FileContextCore/Storage/Internal/FileContextTable.cs +++ b/FileContextCore/Storage/Internal/FileContextTable.cs @@ -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 @@ -264,7 +265,7 @@ private Dictionary Init() return result; } - private Dictionary ConvertToProvider(Dictionary list) + private Dictionary ApplyValueConverter(Dictionary list, Func> conversionFunc) { var result = new Dictionary(); var converters = entityType.GetProperties().Select(p => p.GetValueConverter()).ToArray(); @@ -273,24 +274,20 @@ private Dictionary ConvertToProvider(Dictionary result[keyValuePair.Key] = keyValuePair.Value.Select((value, index) => { var converter = converters[index]; - return converter == null ? value : converter.ConvertToProvider(value); + return converter == null ? value : conversionFunc(converter)(value); }).ToArray(); } return result; } + + private Dictionary ConvertToProvider(Dictionary list) + { + return ApplyValueConverter(list, converter => converter.ConvertToProvider); + } + private Dictionary ConvertFromProvider(Dictionary list) { - var result = new Dictionary(); - 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 : converter.ConvertFromProvider(value); - }).ToArray(); - } - return result; + return ApplyValueConverter(list, converter => converter.ConvertFromProvider); } } } From 6fd81dc436c753fa91ac21550fdbad8e2370222b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Mon, 19 Aug 2019 16:10:03 +0200 Subject: [PATCH 6/8] Use correct CLR types for fields with value converters --- FileContextCore/Serializer/BSONSerializer.cs | 2 +- FileContextCore/Serializer/CSVSerializer.cs | 2 +- FileContextCore/Serializer/EXCELSerializer.cs | 2 +- FileContextCore/Serializer/JSONSerializer.cs | 2 +- FileContextCore/Serializer/XMLSerializer.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/FileContextCore/Serializer/BSONSerializer.cs b/FileContextCore/Serializer/BSONSerializer.cs index f59b730..8f89a83 100644 --- a/FileContextCore/Serializer/BSONSerializer.cs +++ b/FileContextCore/Serializer/BSONSerializer.cs @@ -20,7 +20,7 @@ public BSONSerializer(IEntityType _entityType) { entityType = _entityType; propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); - typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); + typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray(); } public Dictionary Deserialize(string list, Dictionary newList) diff --git a/FileContextCore/Serializer/CSVSerializer.cs b/FileContextCore/Serializer/CSVSerializer.cs index 43ed5b0..2b30c67 100644 --- a/FileContextCore/Serializer/CSVSerializer.cs +++ b/FileContextCore/Serializer/CSVSerializer.cs @@ -19,7 +19,7 @@ public CSVSerializer(IEntityType _entityType) { entityType = _entityType; propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); - typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); + typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray(); } public Dictionary Deserialize(string list, Dictionary newList) diff --git a/FileContextCore/Serializer/EXCELSerializer.cs b/FileContextCore/Serializer/EXCELSerializer.cs index 8367074..7aecb14 100644 --- a/FileContextCore/Serializer/EXCELSerializer.cs +++ b/FileContextCore/Serializer/EXCELSerializer.cs @@ -38,7 +38,7 @@ public EXCELSerializer(IEntityType _entityType, string _password, string databas { entityType = _entityType; propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); - typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); + typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray(); password = _password; this.databaseName = databaseName; diff --git a/FileContextCore/Serializer/JSONSerializer.cs b/FileContextCore/Serializer/JSONSerializer.cs index f766bcd..98b8403 100644 --- a/FileContextCore/Serializer/JSONSerializer.cs +++ b/FileContextCore/Serializer/JSONSerializer.cs @@ -17,7 +17,7 @@ public JSONSerializer(IEntityType _entityType) { entityType = _entityType; propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); - typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); + typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray(); } public Dictionary Deserialize(string list, Dictionary newList) diff --git a/FileContextCore/Serializer/XMLSerializer.cs b/FileContextCore/Serializer/XMLSerializer.cs index 13015f0..c62b222 100644 --- a/FileContextCore/Serializer/XMLSerializer.cs +++ b/FileContextCore/Serializer/XMLSerializer.cs @@ -21,7 +21,7 @@ public XMLSerializer(IEntityType _entityType) { entityType = _entityType; propertyKeys = entityType.GetProperties().Select(p => p.Relational().ColumnName).ToArray(); - typeList = entityType.GetProperties().Select(p => p.ClrType).ToArray(); + typeList = entityType.GetProperties().Select(p => p.GetValueConverter()?.ProviderClrType ?? p.ClrType).ToArray(); } public Dictionary Deserialize(string list, Dictionary newList) From 56458204bce83515800a180d71a4d0cc0fad397c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4ser?= Date: Mon, 19 Aug 2019 16:10:42 +0200 Subject: [PATCH 7/8] Do not rely on column order when deserializing CSV --- FileContextCore/Serializer/CSVSerializer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/FileContextCore/Serializer/CSVSerializer.cs b/FileContextCore/Serializer/CSVSerializer.cs index 2b30c67..e6aad4e 100644 --- a/FileContextCore/Serializer/CSVSerializer.cs +++ b/FileContextCore/Serializer/CSVSerializer.cs @@ -28,6 +28,7 @@ public Dictionary Deserialize(string list, Dictionary Deserialize(string list, Dictionary Date: Tue, 20 Aug 2019 11:42:58 +0200 Subject: [PATCH 8/8] Fix handling of empty CSV files --- FileContextCore/Serializer/CSVSerializer.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/FileContextCore/Serializer/CSVSerializer.cs b/FileContextCore/Serializer/CSVSerializer.cs index e6aad4e..5e39b9b 100644 --- a/FileContextCore/Serializer/CSVSerializer.cs +++ b/FileContextCore/Serializer/CSVSerializer.cs @@ -24,6 +24,11 @@ public CSVSerializer(IEntityType _entityType) public Dictionary Deserialize(string list, Dictionary newList) { + if (string.IsNullOrEmpty(list)) + { + return new Dictionary(); + } + TextReader tr = new StringReader(list); CsvReader reader = new CsvReader(tr);