|
3 | 3 | using InMemory; |
4 | 4 | using Newtonsoft.Json; |
5 | 5 | using Newtonsoft.Json.Serialization; |
6 | | - using Properties; |
7 | | - using System; |
8 | 6 | using System.Collections.Generic; |
9 | | - using System.Globalization; |
10 | 7 | using System.IO; |
11 | 8 |
|
12 | 9 | /// <summary> |
13 | 10 | /// Represents a repository for storing entities as an json formatted file. |
14 | 11 | /// </summary> |
15 | | - public abstract class JsonRepositoryBase<TEntity, TKey> : InMemoryRepositoryBase<TEntity, TKey> where TEntity : class |
| 12 | + public abstract class JsonRepositoryBase<TEntity, TKey> : InMemoryFileBasedRepositoryBase<TEntity, TKey> where TEntity : class |
16 | 13 | { |
17 | | - #region Fields |
18 | | - |
19 | | - private const string FileExtension = ".json"; |
20 | | - |
21 | | - #endregion |
22 | | - |
23 | 14 | #region Constructors |
24 | 15 |
|
25 | 16 | /// <summary> |
26 | 17 | /// Initializes a new instance of the <see cref="JsonRepositoryBase{TEntity, TKey}"/> class. |
27 | 18 | /// </summary> |
28 | 19 | /// <param name="filePath">The file path.</param> |
29 | | - protected JsonRepositoryBase(string filePath) |
| 20 | + protected JsonRepositoryBase(string filePath) : base(filePath) |
30 | 21 | { |
31 | | - if (string.IsNullOrEmpty(filePath)) |
32 | | - throw new ArgumentNullException(nameof(filePath)); |
33 | | - |
34 | | - // Ensures we have a valid file |
35 | | - var fileName = filePath; |
36 | | - |
37 | | - if (Directory.Exists(filePath)) |
38 | | - { |
39 | | - if (!fileName.EndsWith(@"\")) |
40 | | - fileName += @"\"; |
41 | | - |
42 | | - fileName += $"{GetType().Name}{FileExtension}"; |
43 | | - } |
44 | | - else |
45 | | - { |
46 | | - if (string.IsNullOrEmpty(Path.GetExtension(fileName))) |
47 | | - fileName += FileExtension; |
48 | | - |
49 | | - if (!Path.GetExtension(fileName).Equals(FileExtension)) |
50 | | - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidFileExtension, fileName, FileExtension)); |
51 | | - |
52 | | - if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) < 0) |
53 | | - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidFilePath, fileName)); |
54 | | - } |
| 22 | + } |
55 | 23 |
|
56 | | - DatabaseName = fileName; |
| 24 | + #endregion |
57 | 25 |
|
58 | | - // Creates the file if does not exist |
59 | | - if (!File.Exists(DatabaseName)) |
60 | | - { |
61 | | - File.Create(DatabaseName).Dispose(); |
62 | | - } |
63 | | - // Otherwise, try to get the data from the file |
64 | | - else |
65 | | - { |
66 | | - // Adds the data from the file into memory |
67 | | - using (var stream = new FileStream(DatabaseName, FileMode.Open, FileAccess.Read)) |
68 | | - using (var reader = new StreamReader(stream)) |
69 | | - { |
70 | | - var serializer = new JsonSerializer(); |
71 | | - var entities = (List<TEntity>)serializer.Deserialize(reader, typeof(List<TEntity>)); |
| 26 | + #region Overrides of InMemoryFileBasedRepositoryBase<TEntity,TKey> |
72 | 27 |
|
73 | | - EnsureDeleted(); |
| 28 | + /// <summary> |
| 29 | + /// Gets the file extension. |
| 30 | + /// </summary> |
| 31 | + protected override string FileExtension { get; } = ".json"; |
74 | 32 |
|
75 | | - entities.ForEach(AddItem); |
| 33 | + /// <summary> |
| 34 | + /// A protected overridable method for loading the entities from the specified stream reader. |
| 35 | + /// </summary> |
| 36 | + protected override IEnumerable<TEntity> OnLoaded(StreamReader reader) |
| 37 | + { |
| 38 | + var serializer = new JsonSerializer(); |
| 39 | + var entities = (List<TEntity>)serializer.Deserialize(reader, typeof(List<TEntity>)); |
76 | 40 |
|
77 | | - base.SaveChanges(); |
78 | | - } |
79 | | - } |
| 41 | + return entities; |
80 | 42 | } |
81 | 43 |
|
82 | | - #endregion |
83 | | - |
84 | | - #region Overrides of InMemoryRepositoryBase<TEntity,TKey> |
85 | | - |
86 | 44 | /// <summary> |
87 | | - /// A protected overridable method for saving changes made in the current unit of work in the repository. |
| 45 | + /// A protected overridable method for saving the entities to the specified stream writer. |
88 | 46 | /// </summary> |
89 | | - protected override void SaveChanges() |
| 47 | + protected override void OnSaved(StreamWriter writer, IEnumerable<TEntity> entities) |
90 | 48 | { |
91 | | - using (var stream = new FileStream(DatabaseName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Delete)) |
| 49 | + var serializer = new JsonSerializer |
92 | 50 | { |
93 | | - // Saves the data into memory |
94 | | - base.SaveChanges(); |
95 | | - |
96 | | - // Puts from memory into the file |
97 | | - using (var writer = new StreamWriter(stream)) |
98 | | - { |
99 | | - var entities = GetQuery(); |
100 | | - var serializer = new JsonSerializer |
101 | | - { |
102 | | - Formatting = Formatting.Indented, |
103 | | - ContractResolver = new CamelCasePropertyNamesContractResolver(), |
104 | | - TypeNameHandling = TypeNameHandling.All |
105 | | - }; |
| 51 | + Formatting = Formatting.Indented, |
| 52 | + ContractResolver = new CamelCasePropertyNamesContractResolver(), |
| 53 | + TypeNameHandling = TypeNameHandling.All |
| 54 | + }; |
106 | 55 |
|
107 | | - serializer.Serialize(writer, entities); |
108 | | - } |
109 | | - } |
| 56 | + serializer.Serialize(writer, entities); |
110 | 57 | } |
111 | 58 |
|
112 | 59 | #endregion |
|
0 commit comments