-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectStorageHelper.cs
More file actions
187 lines (176 loc) · 7.44 KB
/
Copy pathObjectStorageHelper.cs
File metadata and controls
187 lines (176 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Windows.Storage;
using Windows.Storage.Streams;
namespace WinRtCacheHelper
{
public class ObjectStorageHelper<T>
{
private ApplicationData appData = Windows.Storage.ApplicationData.Current;
private XmlSerializer serializer;
private StorageType storageType;
private string FileName(T Obj, string Handle)
{
/*When I first started writing this class I thought I would only be storing objects that could be serialized as XML
and hence I put a .xml suffix on the filename. I now realise the folly of doing this as I may want to, in the future,
store objects that cannot be serialized as XML (e.g. Media objects) and instead use some alternative method that does
not involve serialization.
I had a choice between supporting backward compatibility or dropping the .xml suffix. I opted for the latter.*/
var str = String.Concat(Handle, String.Format("{0}", Obj.GetType().ToString()));
return str;
//return String.Format("{0}.xml", Obj.GetType().FullName).Replace("'","").Replace(",","").Replace(".","").Replace("&","");
}
/// <summary>
/// Generic class to simplify the storage and retrieval of data to/from Windows.Storage.ApplicationData
/// </summary>
/// <param name="StorageType"></param>
public ObjectStorageHelper(StorageType StorageType)
{
serializer = new XmlSerializer(typeof(T));
storageType = StorageType;
}
/// <summary>
/// Delete a stored instance of T from Windows.Storage.ApplicationData
/// </summary>
/// <returns></returns>
public async Task DeleteAsync()
{
string fileName = FileName(Activator.CreateInstance<T>(), String.Empty);
await DeleteAsync(fileName);
}
/// <summary>
/// Delete a stored instance of T with a specified handle from Windows.Storage.ApplicationData.
/// Specification of a handle supports storage and deletion of different instances of T.
/// </summary>
/// <param name="Handle">User-defined handle for the stored object</param>
public async Task DeleteAsync(string Handle)
{
if (Handle == null)
throw new ArgumentNullException("Handle");
string fileName = FileName(Activator.CreateInstance<T>(), Handle);
try
{
StorageFolder folder = GetFolder(storageType);
var file = await GetFileIfExistsAsync(folder, fileName);
if (file != null)
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Store an instance of T to Windows.Storage.ApplicationData with a specified handle.
/// Specification of a handle supports storage and deletion of different instances of T.
/// </summary>
/// <param name="Obj">Object to be saved</param>
/// <param name="Handle">User-defined handle for the stored object</param>
public async Task SaveAsync(T Obj, string Handle)
{
if (Obj == null)
throw new ArgumentNullException("Obj");
if (Handle == null)
throw new ArgumentNullException("Handle");
StorageFile file = null;
string fileName = FileName(Activator.CreateInstance<T>(), Handle);
StorageFolder folder = GetFolder(storageType);
file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result)
{
serializer.Serialize(outStream, Obj);
await outStream.FlushAsync();
}
}
/// <summary>
/// Save an instance of T to Windows.Storage.ApplicationData.
/// </summary>
/// <param name="Obj">Object to be saved</param>
/// <param name="Handle">User-defined handle for the stored object</param>
public async Task SaveAsync(T Obj)
{
string fileName = FileName(Obj, String.Empty);
await SaveAsync(Obj, fileName);
}
/// <summary>
/// Retrieve a stored instance of T with a specified handle from Windows.Storage.ApplicationData.
/// Specification of a handle supports storage and deletion of different instances of T.
/// </summary>
/// <param name="Handle">User-defined handle for the stored object</param>
public async Task<T> LoadAsync(string Handle)
{
if (Handle == null)
throw new ArgumentNullException("Handle");
string fileName = FileName(Activator.CreateInstance<T>(), Handle);
try
{
StorageFile file = null;
StorageFolder folder = GetFolder(storageType);
file = await folder.GetFileAsync(fileName);
IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
using (Stream inStream = Task.Run(() => readStream.AsStreamForRead()).Result)
{
return (T)serializer.Deserialize(inStream);
}
}
catch (FileNotFoundException)
{
//file not existing is perfectly valid so simply return the default
return default(T);
//Interesting thread here: How to detect if a file exists (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb)
//throw;
}
catch (Exception)
{
//Unable to load contents of file
throw;
}
}
/// <summary>
/// Retrieve a stored instance of T from Windows.Storage.ApplicationData.
/// </summary>
public async Task<T> LoadAsync()
{
string fileName = FileName(Activator.CreateInstance<T>(), String.Empty);
return await LoadAsync(fileName);
}
private StorageFolder GetFolder(StorageType storageType)
{
StorageFolder folder;
switch (storageType)
{
case StorageType.Roaming:
folder = appData.RoamingFolder;
break;
case StorageType.Local:
folder = appData.LocalFolder;
break;
case StorageType.Temporary:
folder = appData.TemporaryFolder;
break;
default:
throw new Exception(String.Format("Unknown StorageType: {0}", storageType));
}
return folder;
}
private async Task<StorageFile> GetFileIfExistsAsync(StorageFolder folder, string fileName)
{
try
{
return await folder.GetFileAsync(fileName);
}
catch
{
return null;
}
}
}
}