Skip to content

Commit

Permalink
refactor: introduce IStorageAccess interface and update DataStore to …
Browse files Browse the repository at this point in the history
…use FileAccess methods
  • Loading branch information
ttu committed Dec 14, 2024
1 parent 6bd9766 commit cfa1b55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
7 changes: 4 additions & 3 deletions JsonFlatFileDataStore/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace JsonFlatFileDataStore
{
public class DataStore : IDataStore
{
private readonly IStorageAccess _fileAccess = new FileAccess();
private readonly string _filePath;
private readonly string _keyProperty;
private readonly bool _reloadBeforeGetCollection;
Expand Down Expand Up @@ -86,7 +87,7 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
_jsonData = JObject.Parse(jsonText);
}

return FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonText);
return _fileAccess.WriteJson(_filePath, _encryptJson, jsonText);
},
GetJsonTextFromFile);
});
Expand Down Expand Up @@ -114,7 +115,7 @@ public void UpdateAll(string jsonData)
_jsonData = JObject.Parse(jsonData);
}

FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonData);
_fileAccess.WriteJson(_filePath, _encryptJson, jsonData);
}

public void Reload()
Expand Down Expand Up @@ -432,7 +433,7 @@ private dynamic SingleDynamicItemReadConverter(JToken e)
}
}

private string GetJsonTextFromFile() => FileAccess.ReadJsonFromFile(_filePath, _encryptJson, _decryptJson);
private string GetJsonTextFromFile() => _fileAccess.ReadJson(_filePath, _encryptJson, _decryptJson);

private JObject GetJsonObjectFromFile() => JObject.Parse(GetJsonTextFromFile());

Expand Down
27 changes: 23 additions & 4 deletions JsonFlatFileDataStore/FileAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

namespace JsonFlatFileDataStore
{
internal static class FileAccess
public interface IStorageAccess
{
internal static string ReadJsonFromFile(string path, Func<string, string> encryptJson, Func<string, string> decryptJson)
string ReadJson(string path, Func<string, string> encryptJson, Func<string, string> decryptJson);
bool WriteJson(string path, Func<string, string> encryptJson, string content);
}

internal class FileAccess : IStorageAccess
{
public string ReadJson(string path, Func<string, string> encryptJson, Func<string, string> decryptJson)
{
Stopwatch sw = null;
var json = "{}";
Expand Down Expand Up @@ -36,7 +42,7 @@ internal static string ReadJsonFromFile(string path, Func<string, string> encryp
return decryptJson(json);
}

internal static bool WriteJsonToFile(string path, Func<string, string> encryptJson, string content)
public bool WriteJson(string path, Func<string, string> encryptJson, string content)
{
Stopwatch sw = null;

Expand All @@ -61,4 +67,17 @@ internal static bool WriteJsonToFile(string path, Func<string, string> encryptJs
}
}
}
}

internal class LocalStorageAccess : IStorageAccess
{
private string _content = string.Empty;

public string ReadJson(string path, Func<string, string> encryptJson, Func<string, string> decryptJson) => _content;

public bool WriteJson(string path, Func<string, string> encryptJson, string content)
{
_content = content;
return true;
}
}
}

0 comments on commit cfa1b55

Please sign in to comment.