From 90e292d17d4d1302de2fd037961c5fdc7a4c4dc6 Mon Sep 17 00:00:00 2001 From: KrzysztofPajak Date: Wed, 10 Feb 2021 14:41:14 +0100 Subject: [PATCH] Remove singleton class --- Grand.Core/Data/DataSettingsHelper.cs | 6 +- Grand.Core/Data/DataSettingsManager.cs | 38 ++++------ Grand.Core/Infrastructure/Singleton.cs | 84 ---------------------- Grand.Core/Plugins/PluginFileParser.cs | 4 +- Grand.Core/Plugins/PluginFinder.cs | 22 +++--- Grand.Web/Controllers/InstallController.cs | 2 +- 6 files changed, 28 insertions(+), 128 deletions(-) delete mode 100644 Grand.Core/Infrastructure/Singleton.cs diff --git a/Grand.Core/Data/DataSettingsHelper.cs b/Grand.Core/Data/DataSettingsHelper.cs index 2db4d80e8a..00c5b7e1cd 100644 --- a/Grand.Core/Data/DataSettingsHelper.cs +++ b/Grand.Core/Data/DataSettingsHelper.cs @@ -19,8 +19,8 @@ public static bool DatabaseIsInstalled() { var manager = new DataSettingsManager(); var settings = manager.LoadSettings(); - _databaseIsInstalled = settings != null && !String.IsNullOrEmpty(settings.DataConnectionString); - if (!String.IsNullOrEmpty(settings.DataConnectionString)) + _databaseIsInstalled = settings != null && !string.IsNullOrEmpty(settings.DataConnectionString); + if (!string.IsNullOrEmpty(settings.DataConnectionString)) _connectionString = settings.DataConnectionString; } return _databaseIsInstalled.Value; @@ -29,7 +29,7 @@ public static void InitConnectionString() { var manager = new DataSettingsManager(); var settings = manager.LoadSettings(); - if (!String.IsNullOrEmpty(settings.DataConnectionString)) + if (!string.IsNullOrEmpty(settings.DataConnectionString)) _connectionString = settings.DataConnectionString; } public static string ConnectionString() diff --git a/Grand.Core/Data/DataSettingsManager.cs b/Grand.Core/Data/DataSettingsManager.cs index 52663b5b35..8f20b2a9b5 100644 --- a/Grand.Core/Data/DataSettingsManager.cs +++ b/Grand.Core/Data/DataSettingsManager.cs @@ -1,9 +1,9 @@ -using Grand.Core.Infrastructure; -using Grand.Domain.Data; +using Grand.Domain.Data; using System; using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading.Tasks; namespace Grand.Core.Data { @@ -15,10 +15,12 @@ public partial class DataSettingsManager protected const char separator = ':'; protected const string filename = "Settings.txt"; + private DataSettings _dataSettings; + protected string RemoveSpecialCharacters(string str) { var sb = new StringBuilder(); - foreach (char c in str) + foreach (var c in str) { if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') { @@ -36,7 +38,7 @@ protected string RemoveSpecialCharacters(string str) protected virtual DataSettings ParseSettings(string text) { var shellSettings = new DataSettings(); - if (String.IsNullOrEmpty(text)) + if (string.IsNullOrEmpty(text)) return shellSettings; var settings = new List(); @@ -96,13 +98,10 @@ protected virtual string ComposeSettings(DataSettings settings) /// /// Load settings /// - /// File path; pass null to use default settings file path - /// public virtual DataSettings LoadSettings(string filePath = null, bool reloadSettings = false) { - - if (!reloadSettings && Singleton.Instance != null) - return Singleton.Instance; + if (!reloadSettings && _dataSettings != null) + return _dataSettings; if (string.IsNullOrEmpty(filePath)) filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), filename); @@ -111,8 +110,8 @@ public virtual DataSettings LoadSettings(string filePath = null, bool reloadSett return new DataSettings(); var text = File.ReadAllText(filePath); - Singleton.Instance = ParseSettings(text); - return Singleton.Instance; + _dataSettings = ParseSettings(text); + return _dataSettings; } @@ -120,24 +119,15 @@ public virtual DataSettings LoadSettings(string filePath = null, bool reloadSett /// Save settings to a file /// /// - public virtual void SaveSettings(DataSettings settings) + public virtual async Task SaveSettings(DataSettings settings) { - if (settings == null) - throw new ArgumentNullException("settings"); - - Singleton.Instance = settings; - - string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), filename); + var filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), filename); if (!File.Exists(filePath)) { - using (File.Create(filePath)) - { - //we use 'using' to close the file after it's created - } + File.Create(filePath); } - var text = ComposeSettings(settings); - File.WriteAllText(filePath, text); + await File.WriteAllTextAsync(filePath, text); } } } diff --git a/Grand.Core/Infrastructure/Singleton.cs b/Grand.Core/Infrastructure/Singleton.cs deleted file mode 100644 index b35419d062..0000000000 --- a/Grand.Core/Infrastructure/Singleton.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Grand.Core.Infrastructure -{ - /// - /// A statically compiled "singleton" used to store objects throughout the - /// lifetime of the app domain. Not so much singleton in the pattern's - /// sense of the word as a standardized way to store single instances. - /// - /// The type of object to store. - /// Access to the instance is not synchrnoized. - public class Singleton : Singleton - { - static T instance; - - /// The singleton instance for the specified type T. Only one instance (at the time) of this object for each type of T. - public static T Instance - { - get { return instance; } - set - { - instance = value; - AllSingletons[typeof(T)] = value; - } - } - } - - /// - /// Provides a singleton list for a certain type. - /// - /// The type of list to store. - public class SingletonList : Singleton> - { - static SingletonList() - { - Singleton>.Instance = new List(); - } - - /// The singleton instance for the specified type T. Only one instance (at the time) of this list for each type of T. - public new static IList Instance - { - get { return Singleton>.Instance; } - } - } - - /// - /// Provides a singleton dictionary for a certain key and vlaue type. - /// - /// The type of key. - /// The type of value. - public class SingletonDictionary : Singleton> - { - static SingletonDictionary() - { - Singleton>.Instance = new Dictionary(); - } - - /// The singleton instance for the specified type T. Only one instance (at the time) of this dictionary for each type of T. - public new static IDictionary Instance - { - get { return Singleton>.Instance; } - } - } - - /// - /// Provides access to all "singletons" stored by . - /// - public class Singleton - { - static Singleton() - { - allSingletons = new Dictionary(); - } - - static readonly IDictionary allSingletons; - - /// Dictionary of type to singleton instances. - public static IDictionary AllSingletons - { - get { return allSingletons; } - } - } -} diff --git a/Grand.Core/Plugins/PluginFileParser.cs b/Grand.Core/Plugins/PluginFileParser.cs index 9bc7257fad..7f3aac455a 100644 --- a/Grand.Core/Plugins/PluginFileParser.cs +++ b/Grand.Core/Plugins/PluginFileParser.cs @@ -27,7 +27,7 @@ public static IList ParseInstalledPluginsFile(string filePath) string str; while ((str = reader.ReadLine()) != null) { - if (String.IsNullOrWhiteSpace(str)) + if (string.IsNullOrWhiteSpace(str)) continue; lines.Add(str.Trim()); } @@ -35,7 +35,7 @@ public static IList ParseInstalledPluginsFile(string filePath) return lines; } - public static async Task SaveInstalledPluginsFile(IList pluginSystemNames, string filePath) + public static async Task SaveInstalledPluginsFile(IList pluginSystemNames, string filePath) { string result = ""; foreach (var sn in pluginSystemNames) diff --git a/Grand.Core/Plugins/PluginFinder.cs b/Grand.Core/Plugins/PluginFinder.cs index 6e52ac5218..8e3aacc2db 100644 --- a/Grand.Core/Plugins/PluginFinder.cs +++ b/Grand.Core/Plugins/PluginFinder.cs @@ -50,18 +50,12 @@ protected virtual bool CheckLoadMode(PluginDescriptor pluginDescriptor, LoadPlug if (pluginDescriptor == null) throw new ArgumentNullException("pluginDescriptor"); - switch (loadMode) - { - case LoadPluginsMode.All: - //no filering - return true; - case LoadPluginsMode.InstalledOnly: - return pluginDescriptor.Installed; - case LoadPluginsMode.NotInstalledOnly: - return !pluginDescriptor.Installed; - default: - throw new Exception("Not supported LoadPluginsMode"); - } + return loadMode switch { + LoadPluginsMode.All => true,//no filering + LoadPluginsMode.InstalledOnly => pluginDescriptor.Installed, + LoadPluginsMode.NotInstalledOnly => !pluginDescriptor.Installed, + _ => throw new Exception("Not supported LoadPluginsMode"), + }; } /// @@ -75,7 +69,7 @@ protected virtual bool CheckGroup(PluginDescriptor pluginDescriptor, string grou if (pluginDescriptor == null) throw new ArgumentNullException("pluginDescriptor"); - if (String.IsNullOrEmpty(group)) + if (string.IsNullOrEmpty(group)) return true; return group.Equals(pluginDescriptor.Group, StringComparison.OrdinalIgnoreCase); @@ -102,7 +96,7 @@ public virtual bool AuthenticateStore(PluginDescriptor pluginDescriptor, string throw new ArgumentNullException("pluginDescriptor"); //no validation required - if (String.IsNullOrEmpty(storeId)) + if (string.IsNullOrEmpty(storeId)) return true; if (!pluginDescriptor.LimitedToStores.Any()) diff --git a/Grand.Web/Controllers/InstallController.cs b/Grand.Web/Controllers/InstallController.cs index 595378185b..b55e5345ea 100644 --- a/Grand.Web/Controllers/InstallController.cs +++ b/Grand.Web/Controllers/InstallController.cs @@ -175,7 +175,7 @@ public virtual async Task Index(InstallModel model) DataProvider = "mongodb", DataConnectionString = connectionString }; - settingsManager.SaveSettings(settings); + await settingsManager.SaveSettings(settings); var dataProviderInstance = _serviceProvider.GetRequiredService().LoadDataProvider(); dataProviderInstance.InitDatabase();