-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvDbContext.cs
45 lines (35 loc) · 1.36 KB
/
CsvDbContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Arex388.Extensions.CsvHelper {
public abstract class CsvDbContext {
private readonly IList<PropertyInfo> _csvDbSets;
protected CsvDbContext(
string path) :
this(new CsvDbContextOptions(path)) {
}
protected CsvDbContext(
CsvDbContextOptions options) {
_csvDbSets = GetType().GetProperties().Where(
p => typeof(ICsvDbSet).IsAssignableFrom(p.PropertyType)).ToList();
var csvMaps = GetType().Assembly.GetTypes().Where(
t => typeof(ICsvMap).IsAssignableFrom(t.BaseType)).ToList();
foreach (var csvDbSet in _csvDbSets) {
var instance = Activator.CreateInstance(csvDbSet.PropertyType, options, csvMaps);
csvDbSet.SetValue(this, instance);
}
}
protected abstract void Relate();
public async Task SaveAsync(
CancellationToken cancellationToken = default) {
foreach (var csvDbSet in _csvDbSets) {
var instance = (ICsvDbSet)csvDbSet.GetValue(this, null);
await instance.SaveAsync(cancellationToken).ConfigureAwait(false);
}
Relate();
}
}
}