-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupHelper.cs
62 lines (53 loc) · 2.27 KB
/
BackupHelper.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
namespace BackupHelper
{
public class BackupHelper
{
private readonly List<BackupableList> backupLists = new List<BackupableList>();
public readonly string BackupFilesPrefix;
public BackupHelper(string backupFilesPrefix = "")
{
BackupFilesPrefix = backupFilesPrefix;
}
public List<object> CreateBackupableList(SerializationType serializationType, string backupFileName, string backupFileExtention = "log")
{
var backupList = new BackupableList(serializationType, backupFileName, backupFileExtention);
backupLists.Add(backupList);
return backupList.ObjectsList;
}
public void Backup()
=> backupLists.ForEach(list => File.WriteAllText($"{BackupFilesPrefix}{list.BackupFileName}.{list.BackupFileExtention}", Serialize(list.ObjectsList, list.SerializationType)));
private string Serialize(List<object> objectsList, SerializationType serializationType)
{
switch (serializationType)
{
case SerializationType.Inline:
return string.Join("\n", objectsList);
case SerializationType.JsonSerializable:
return JsonConvert.SerializeObject(objectsList, Formatting.Indented);
default:
return "";
}
}
private class BackupableList
{
public readonly List<object> ObjectsList = new List<object>();
public readonly SerializationType SerializationType;
public readonly string BackupFileName;
public readonly string BackupFileExtention;
public BackupableList(SerializationType serializationType, string backupFileName, string backupFileExtention)
{
SerializationType = serializationType;
BackupFileName = backupFileName;
BackupFileExtention = backupFileExtention;
}
}
public enum SerializationType
{
Inline,
JsonSerializable
}
}
}