Skip to content

Commit 035c941

Browse files
author
Christian Käser
committed
Refactor duplicate code
1 parent b0c679c commit 035c941

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

FileContextCore/Storage/Internal/FileContextTable.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.EntityFrameworkCore;
1616
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
1717
using Microsoft.EntityFrameworkCore.Metadata;
18+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1819
using Microsoft.EntityFrameworkCore.Update;
1920

2021
namespace FileContextCore.Storage.Internal
@@ -264,7 +265,7 @@ private Dictionary<TKey, object[]> Init()
264265
return result;
265266
}
266267

267-
private Dictionary<TKey, object[]> ConvertToProvider(Dictionary<TKey, object[]> list)
268+
private Dictionary<TKey, object[]> ApplyValueConverter(Dictionary<TKey, object[]> list, Func<ValueConverter, Func<object, object>> conversionFunc)
268269
{
269270
var result = new Dictionary<TKey, object[]>();
270271
var converters = entityType.GetProperties().Select(p => p.GetValueConverter()).ToArray();
@@ -273,24 +274,20 @@ private Dictionary<TKey, object[]> ConvertToProvider(Dictionary<TKey, object[]>
273274
result[keyValuePair.Key] = keyValuePair.Value.Select((value, index) =>
274275
{
275276
var converter = converters[index];
276-
return converter == null ? value : converter.ConvertToProvider(value);
277+
return converter == null ? value : conversionFunc(converter)(value);
277278
}).ToArray();
278279
}
279280
return result;
280281
}
282+
283+
private Dictionary<TKey, object[]> ConvertToProvider(Dictionary<TKey, object[]> list)
284+
{
285+
return ApplyValueConverter(list, converter => converter.ConvertToProvider);
286+
}
287+
281288
private Dictionary<TKey, object[]> ConvertFromProvider(Dictionary<TKey, object[]> list)
282289
{
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;
290+
return ApplyValueConverter(list, converter => converter.ConvertFromProvider);
294291
}
295292
}
296293
}

0 commit comments

Comments
 (0)