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,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);
}
}
}
}
}