Skip to content

Commit

Permalink
Remove singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Feb 10, 2021
1 parent 11c8eb6 commit 90e292d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 128 deletions.
6 changes: 3 additions & 3 deletions Grand.Core/Data/DataSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
38 changes: 14 additions & 24 deletions Grand.Core/Data/DataSettingsManager.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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 == '_')
{
Expand All @@ -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<string>();
Expand Down Expand Up @@ -96,13 +98,10 @@ protected virtual string ComposeSettings(DataSettings settings)
/// <summary>
/// Load settings
/// </summary>
/// <param name="filePath">File path; pass null to use default settings file path</param>
/// <returns></returns>
public virtual DataSettings LoadSettings(string filePath = null, bool reloadSettings = false)
{

if (!reloadSettings && Singleton<DataSettings>.Instance != null)
return Singleton<DataSettings>.Instance;
if (!reloadSettings && _dataSettings != null)
return _dataSettings;

if (string.IsNullOrEmpty(filePath))
filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), filename);
Expand All @@ -111,33 +110,24 @@ public virtual DataSettings LoadSettings(string filePath = null, bool reloadSett
return new DataSettings();

var text = File.ReadAllText(filePath);
Singleton<DataSettings>.Instance = ParseSettings(text);
return Singleton<DataSettings>.Instance;
_dataSettings = ParseSettings(text);
return _dataSettings;

}

/// <summary>
/// Save settings to a file
/// </summary>
/// <param name="settings"></param>
public virtual void SaveSettings(DataSettings settings)
public virtual async Task SaveSettings(DataSettings settings)
{
if (settings == null)
throw new ArgumentNullException("settings");

Singleton<DataSettings>.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);
}
}
}
84 changes: 0 additions & 84 deletions Grand.Core/Infrastructure/Singleton.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Grand.Core/Plugins/PluginFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public static IList<string> ParseInstalledPluginsFile(string filePath)
string str;
while ((str = reader.ReadLine()) != null)
{
if (String.IsNullOrWhiteSpace(str))
if (string.IsNullOrWhiteSpace(str))
continue;
lines.Add(str.Trim());
}
}
return lines;
}

public static async Task SaveInstalledPluginsFile(IList<String> pluginSystemNames, string filePath)
public static async Task SaveInstalledPluginsFile(IList<string> pluginSystemNames, string filePath)
{
string result = "";
foreach (var sn in pluginSystemNames)
Expand Down
22 changes: 8 additions & 14 deletions Grand.Core/Plugins/PluginFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
}

/// <summary>
Expand All @@ -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);
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion Grand.Web/Controllers/InstallController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public virtual async Task<IActionResult> Index(InstallModel model)
DataProvider = "mongodb",
DataConnectionString = connectionString
};
settingsManager.SaveSettings(settings);
await settingsManager.SaveSettings(settings);

var dataProviderInstance = _serviceProvider.GetRequiredService<BaseDataProviderManager>().LoadDataProvider();
dataProviderInstance.InitDatabase();
Expand Down

0 comments on commit 90e292d

Please sign in to comment.