Remove instance logic for BinaryStorage and JsonStorage, part 1

1. part of #389
2. huge refactoring
This commit is contained in:
bao-qian
2016-04-21 01:53:21 +01:00
parent 0bcb76fa81
commit 8d10c9aa41
52 changed files with 502 additions and 584 deletions

View File

@@ -1,82 +0,0 @@
using System;
using System.IO;
namespace Wox.Infrastructure.Storage
{
[Serializable]
public abstract class BaseStorage<T> : IStorage where T : class, IStorage, new()
{
protected string DirectoryPath { get; } = Path.Combine(WoxDirectroy.Executable, "Config");
protected string FilePath => Path.Combine(DirectoryPath, FileName + FileSuffix);
protected abstract string FileSuffix { get; }
protected abstract string FileName { get; }
private static object locker = new object();
protected static T serializedObject;
public event Action<T> AfterLoad;
protected virtual void OnAfterLoad(T obj)
{
Action<T> handler = AfterLoad;
if (handler != null) handler(obj);
}
public static T Instance
{
get
{
if (serializedObject == null)
{
lock (locker)
{
if (serializedObject == null)
{
serializedObject = new T();
serializedObject.Load();
}
}
}
return serializedObject;
}
}
/// <summary>
/// if loading storage failed, we will try to load default
/// </summary>
/// <returns></returns>
protected virtual T LoadDefault()
{
return new T();
}
protected abstract void LoadInternal();
protected abstract void SaveInternal();
public void Load()
{
if (!File.Exists(FilePath))
{
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
File.Create(FilePath).Close();
}
LoadInternal();
OnAfterLoad(serializedObject);
}
public void Save()
{
lock (locker)
{
SaveInternal();
}
}
}
}

View File

@@ -1,9 +1,9 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure.Storage
@@ -13,55 +13,73 @@ namespace Wox.Infrastructure.Storage
/// Normally, it has better performance, but not readable
/// You MUST mark implement class as Serializable
/// </summary>
[Serializable]
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
public class BinaryStorage<T> where T : class, new()
{
private static object syncObject = new object();
protected override string FileSuffix
private T _binary;
private string FilePath { get; }
private string FileName { get; }
private const string FileSuffix = ".dat";
private string DirectoryPath { get; }
private const string DirectoryName = "Config";
public BinaryStorage()
{
get { return ".dat"; }
FileName = typeof(T).Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); ;
}
protected override void LoadInternal()
public T Load()
{
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
try
if (!Directory.Exists(DirectoryPath))
{
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Directory.CreateDirectory(DirectoryPath);
}
if (File.Exists(FilePath))
{
using (var stream = new FileStream(FilePath, FileMode.Open))
{
if (fileStream.Length > 0)
if (stream.Length > 0)
{
BinaryFormatter binaryFormatter = new BinaryFormatter
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
if (serializedObject == null)
{
serializedObject = LoadDefault();
#if (DEBUG)
{
throw new System.Exception("deserialize failed");
}
#endif
}
Deserialize(stream);
}
else
{
serializedObject = LoadDefault();
LoadDefault();
}
}
}
catch (System.Exception e)
else
{
LoadDefault();
}
return _binary;
}
private void Deserialize(FileStream stream)
{
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
BinaryFormatter binaryFormatter = new BinaryFormatter
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
try
{
_binary = (T)binaryFormatter.Deserialize(stream);
}
catch (SerializationException e)
{
Log.Error(e);
serializedObject = LoadDefault();
#if (DEBUG)
{
throw;
}
#endif
LoadDefault();
}
catch (InvalidCastException e)
{
Log.Error(e);
LoadDefault();
}
finally
{
@@ -69,6 +87,11 @@ namespace Wox.Infrastructure.Storage
}
}
private void LoadDefault()
{
_binary = new T();
}
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly ayResult = null;
@@ -85,33 +108,24 @@ namespace Wox.Infrastructure.Storage
return ayResult;
}
protected override void SaveInternal()
public void Save()
{
ThreadPool.QueueUserWorkItem(o =>
using (var stream = new FileStream(FilePath, FileMode.Create))
{
lock (syncObject)
BinaryFormatter binaryFormatter = new BinaryFormatter
{
try
{
FileStream fileStream = new FileStream(FilePath, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
binaryFormatter.Serialize(fileStream, serializedObject);
fileStream.Close();
}
catch (System.Exception e)
{
Log.Error(e);
#if (DEBUG)
{
throw;
}
#endif
}
AssemblyFormat = FormatterAssemblyStyle.Simple
};
try
{
binaryFormatter.Serialize(stream, _binary);
}
});
catch (SerializationException e)
{
Log.Error(e);
}
}
}
}
}

View File

@@ -1,8 +0,0 @@
namespace Wox.Infrastructure.Storage
{
public interface IStorage
{
void Load();
void Save();
}
}

View File

@@ -1,50 +1,85 @@
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure.Storage
{
/// <summary>
/// Serialize object using json format.
/// </summary>
public abstract class JsonStrorage<T> : BaseStorage<T> where T : class, IStorage, new()
public class JsonStrorage<T> where T : new()
{
private static object syncObject = new object();
protected override string FileSuffix
private T _json;
private readonly JsonSerializerSettings _serializerSettings;
protected string FileName { get; set; }
protected string FilePath { get; set; }
protected const string FileSuffix = ".json";
protected string DirectoryPath { get; set; }
protected const string DirectoryName = "Config";
internal JsonStrorage()
{
get { return ".json"; }
FileName = typeof(T).Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
// use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object
_serializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
}
protected override void LoadInternal()
public T Load()
{
string json = File.ReadAllText(FilePath);
if (!string.IsNullOrEmpty(json))
if (!Directory.Exists(DirectoryPath))
{
try
Directory.CreateDirectory(DirectoryPath);
}
if (File.Exists(FilePath))
{
var searlized = File.ReadAllText(FilePath);
if (!string.IsNullOrWhiteSpace(searlized))
{
serializedObject = JsonConvert.DeserializeObject<T>(json);
Deserialize(searlized);
}
catch (System.Exception)
else
{
serializedObject = LoadDefault();
LoadDefault();
}
}
else
{
serializedObject = LoadDefault();
LoadDefault();
}
return _json;
}
protected override void SaveInternal()
private void Deserialize(string searlized)
{
ThreadPool.QueueUserWorkItem(o =>
try
{
lock (syncObject)
{
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
File.WriteAllText(FilePath, json);
}
});
_json = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
}
catch (JsonSerializationException e)
{
LoadDefault();
Log.Error(e);
}
}
private void LoadDefault()
{
_json = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
Save();
}
public void Save()
{
string serialized = JsonConvert.SerializeObject(_json, Formatting.Indented);
File.WriteAllText(FilePath, serialized);
}
}
}

View File

@@ -0,0 +1,20 @@
using System.IO;
namespace Wox.Infrastructure.Storage
{
public class PluginSettingsStorage<T> :JsonStrorage<T> where T : new()
{
public PluginSettingsStorage()
{
var pluginDirectoryName = "Plugins";
// C# releated, add python releated below
var type = typeof (T);
FileName = type.Name;
var assemblyName = type.Assembly.GetName().Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName, pluginDirectoryName, assemblyName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
}
}
}