Skip to content

Commit b0c679c

Browse files
author
Christian Käser
committed
Apply value converters
1 parent eb7f167 commit b0c679c

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

FileContextCore/Storage/Internal/FileContextTable.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private Dictionary<TKey, object[]> InitExcel(string filetype)
185185

186186
UpdateMethod = new Action<Dictionary<TKey, object[]>>((list) =>
187187
{
188-
excel.Serialize(list);
188+
excel.Serialize(ConvertToProvider(list));
189189
});
190190

191191
Dictionary<TKey, object[]> newlist = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);
@@ -253,15 +253,44 @@ private Dictionary<TKey, object[]> Init()
253253

254254
UpdateMethod = new Action<Dictionary<TKey, object[]>>((list) =>
255255
{
256-
string cnt = serializer.Serialize(list);
256+
string cnt = serializer.Serialize(ConvertToProvider(list));
257257
fileManager.SaveContent(cnt);
258258
});
259259

260260
string content = fileManager.LoadContent();
261261
Dictionary<TKey, object[]> newList = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);
262-
Dictionary<TKey, object[]> result = serializer.Deserialize(content, newList);
262+
Dictionary<TKey, object[]> result = ConvertFromProvider(serializer.Deserialize(content, newList));
263263
GenerateLastAutoPropertyValues(result);
264264
return result;
265265
}
266+
267+
private Dictionary<TKey, object[]> ConvertToProvider(Dictionary<TKey, object[]> list)
268+
{
269+
var result = new Dictionary<TKey, object[]>();
270+
var converters = entityType.GetProperties().Select(p => p.GetValueConverter()).ToArray();
271+
foreach (var keyValuePair in list)
272+
{
273+
result[keyValuePair.Key] = keyValuePair.Value.Select((value, index) =>
274+
{
275+
var converter = converters[index];
276+
return converter == null ? value : converter.ConvertToProvider(value);
277+
}).ToArray();
278+
}
279+
return result;
280+
}
281+
private Dictionary<TKey, object[]> ConvertFromProvider(Dictionary<TKey, object[]> list)
282+
{
283+
var result = new Dictionary<TKey, object[]>();
284+
var converters = entityType.GetProperties().Select(p => p.GetValueConverter()).ToArray();
285+
foreach (var keyValuePair in list)
286+
{
287+
result[keyValuePair.Key] = keyValuePair.Value.Select((value, index) =>
288+
{
289+
var converter = converters[index];
290+
return converter == null ? value : converter.ConvertFromProvider(value);
291+
}).ToArray();
292+
}
293+
return result;
294+
}
266295
}
267296
}

0 commit comments

Comments
 (0)