From f7c9a12510a5b54556ad1d43a426c30da53ad546 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Tue, 7 Feb 2017 00:21:39 +0000 Subject: [PATCH] remove storage dependency from jsonstorage --- Plugins/Wox.Plugin.WebSearch/Main.cs | 3 +- .../Wox.Plugin.WebSearch/SettingsViewModel.cs | 2 +- Wox.Infrastructure/Helper.cs | 8 +++++ Wox.Infrastructure/Storage/BinaryStorage.cs | 16 +++++----- Wox.Infrastructure/Storage/JsonStorage.cs | 28 ++++++++--------- .../Storage/PluginJsonStorage.cs | 11 +++---- Wox.Infrastructure/Storage/Storage.cs | 31 ------------------- Wox.Infrastructure/Storage/WoxJsonStorage.cs | 21 +++++++++++++ Wox.Infrastructure/Wox.Infrastructure.csproj | 2 +- Wox/ViewModel/MainViewModel.cs | 12 +++---- Wox/ViewModel/SettingWindowViewModel.cs | 4 +-- 11 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 Wox.Infrastructure/Storage/Storage.cs create mode 100644 Wox.Infrastructure/Storage/WoxJsonStorage.cs diff --git a/Plugins/Wox.Plugin.WebSearch/Main.cs b/Plugins/Wox.Plugin.WebSearch/Main.cs index be36d1cccd..1581ddb334 100644 --- a/Plugins/Wox.Plugin.WebSearch/Main.cs +++ b/Plugins/Wox.Plugin.WebSearch/Main.cs @@ -132,9 +132,8 @@ namespace Wox.Plugin.WebSearch public void Init(PluginInitContext context) { - - var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory; _context = context; + var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory; var bundledImagesDirectory = Path.Combine(pluginDirectory, Images); ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images); Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory); diff --git a/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs b/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs index 6a727a5ddb..5d50c3926c 100644 --- a/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs +++ b/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs @@ -4,7 +4,7 @@ namespace Wox.Plugin.WebSearch { public class SettingsViewModel { - private readonly JsonStrorage _storage; + private readonly PluginJsonStorage _storage; public SettingsViewModel() { diff --git a/Wox.Infrastructure/Helper.cs b/Wox.Infrastructure/Helper.cs index e5123c16a2..1c94daa281 100644 --- a/Wox.Infrastructure/Helper.cs +++ b/Wox.Infrastructure/Helper.cs @@ -55,5 +55,13 @@ namespace Wox.Infrastructure } } } + + public static void ValidateDirectory(string path) + { + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + } } } diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index 5c787f83b5..b5128e4721 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -11,21 +11,21 @@ namespace Wox.Infrastructure.Storage /// /// Stroage object using binary data /// Normally, it has better performance, but not readable - /// You MUST mark implement class as Serializable /// - public class BinaryStorage : Storage + public class BinaryStorage { public BinaryStorage(string filename) { - FileSuffix = ".cache"; - DirectoryName = "Cache"; - DirectoryPath = Path.Combine(DirectoryPath, DirectoryName); - FileName = filename; - FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); + const string directoryName = "Cache"; + var directoryPath = Path.Combine(Constant.DataDirectory, directoryName); + Helper.ValidateDirectory(directoryPath); - ValidateDirectory(); + const string fileSuffix = ".cache"; + FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}"); } + public string FilePath { get; } + public T TryLoad(T defaultData) { if (File.Exists(FilePath)) diff --git a/Wox.Infrastructure/Storage/JsonStorage.cs b/Wox.Infrastructure/Storage/JsonStorage.cs index 94c74a4427..054f34098c 100644 --- a/Wox.Infrastructure/Storage/JsonStorage.cs +++ b/Wox.Infrastructure/Storage/JsonStorage.cs @@ -1,6 +1,6 @@ -using System.IO; +using System; +using System.IO; using Newtonsoft.Json; -using Wox.Infrastructure; using Wox.Infrastructure.Logger; namespace Wox.Infrastructure.Storage @@ -8,19 +8,19 @@ namespace Wox.Infrastructure.Storage /// /// Serialize object using json format. /// - public class JsonStrorage : Storage where T : new() + public class JsonStrorage { private readonly JsonSerializerSettings _serializerSettings; + private T _data; + // need a new directory name + public const string DirectoryName = "Settings"; + public const string FileSuffix = ".json"; + public string FilePath { get; set; } + public string DirectoryPath { get; set; } + internal JsonStrorage() { - FileSuffix = ".json"; - DirectoryName = "Settings"; - DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName); - FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); - - ValidateDirectory(); - // use property initialization instead of DefaultValueAttribute // easier and flexible for default value of object _serializerSettings = new JsonSerializerSettings @@ -48,14 +48,14 @@ namespace Wox.Infrastructure.Storage { LoadDefault(); } - return Data; + return _data; } private void Deserialize(string searlized) { try { - Data = JsonConvert.DeserializeObject(searlized, _serializerSettings); + _data = JsonConvert.DeserializeObject(searlized, _serializerSettings); } catch (JsonSerializationException e) { @@ -66,13 +66,13 @@ namespace Wox.Infrastructure.Storage public void LoadDefault() { - Data = JsonConvert.DeserializeObject("{}", _serializerSettings); + _data = JsonConvert.DeserializeObject("{}", _serializerSettings); Save(); } public void Save() { - string serialized = JsonConvert.SerializeObject(Data, Formatting.Indented); + string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented); File.WriteAllText(FilePath, serialized); } } diff --git a/Wox.Infrastructure/Storage/PluginJsonStorage.cs b/Wox.Infrastructure/Storage/PluginJsonStorage.cs index 9f10327bb3..27de500c72 100644 --- a/Wox.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Wox.Infrastructure/Storage/PluginJsonStorage.cs @@ -6,14 +6,13 @@ namespace Wox.Infrastructure.Storage { public PluginJsonStorage() { - DirectoryName = Constant.Plugins; - // C# releated, add python releated below - var assemblyName = DataType.Assembly.GetName().Name; - DirectoryPath = Path.Combine(DirectoryPath, DirectoryName, assemblyName); - FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); + var dataType = typeof(T); + var assemblyName = typeof(T).Assembly.GetName().Name; + DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Plugins, assemblyName); + Helper.ValidateDirectory(DirectoryPath); - ValidateDirectory(); + FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}"); } } } diff --git a/Wox.Infrastructure/Storage/Storage.cs b/Wox.Infrastructure/Storage/Storage.cs deleted file mode 100644 index 6928d5d833..0000000000 --- a/Wox.Infrastructure/Storage/Storage.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.IO; - -namespace Wox.Infrastructure.Storage -{ - public class Storage - { - protected T Data; - protected Type DataType { get; } - public string FileName { get; set; } - public string FilePath { get; set; } - public string FileSuffix { get; set; } - public string DirectoryPath { get; set; } - public string DirectoryName { get; set; } - - protected Storage() - { - DataType = typeof (T); - FileName = DataType.Name; - DirectoryPath = Constant.DataDirectory; - } - - protected void ValidateDirectory() - { - if (!Directory.Exists(DirectoryPath)) - { - Directory.CreateDirectory(DirectoryPath); - } - } - } -} diff --git a/Wox.Infrastructure/Storage/WoxJsonStorage.cs b/Wox.Infrastructure/Storage/WoxJsonStorage.cs new file mode 100644 index 0000000000..f117aee229 --- /dev/null +++ b/Wox.Infrastructure/Storage/WoxJsonStorage.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wox.Infrastructure.Storage +{ + class WoxJsonStorage : JsonStrorage where T : new() + { + public WoxJsonStorage() + { + var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName); + Helper.ValidateDirectory(directoryPath); + + var filename = typeof(T).Name; + FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}"); + } + } +} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 1251fda12c..48aa25d58d 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -75,10 +75,10 @@ - + diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 526052f539..da5757ea62 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -29,9 +29,9 @@ namespace Wox.ViewModel private Query _lastQuery; private string _queryTextBeforeLeaveResults; - private readonly JsonStrorage _historyItemsStorage; - private readonly JsonStrorage _userSelectedRecordStorage; - private readonly JsonStrorage _topMostRecordStorage; + private readonly WoxJsonStorage _historyItemsStorage; + private readonly WoxJsonStorage _userSelectedRecordStorage; + private readonly WoxJsonStorage _topMostRecordStorage; private readonly Settings _settings; private readonly History _history; private readonly UserSelectedRecord _userSelectedRecord; @@ -56,9 +56,9 @@ namespace Wox.ViewModel _settings = settings; - _historyItemsStorage = new JsonStrorage(); - _userSelectedRecordStorage = new JsonStrorage(); - _topMostRecordStorage = new JsonStrorage(); + _historyItemsStorage = new WoxJsonStorage(); + _userSelectedRecordStorage = new WoxJsonStorage(); + _topMostRecordStorage = new WoxJsonStorage(); _history = _historyItemsStorage.Load(); _userSelectedRecord = _userSelectedRecordStorage.Load(); _topMostRecord = _topMostRecordStorage.Load(); diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 32af970994..ae23776825 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -19,11 +19,11 @@ namespace Wox.ViewModel { public class SettingWindowViewModel : BaseModel { - private readonly JsonStrorage _storage; + private readonly WoxJsonStorage _storage; public SettingWindowViewModel() { - _storage = new JsonStrorage(); + _storage = new WoxJsonStorage(); Settings = _storage.Load(); Settings.PropertyChanged += (s, e) => {