Permalink
Browse files

Code fixes to build on Silverlight and WP7

  • Loading branch information...
1 parent 12380f7 commit 0eb5f8780ec7e25b32b50d78a423585957e73c9d @paulcbetts paulcbetts committed Dec 24, 2010
@@ -21,9 +21,6 @@ public class EsentStorageEngine : IExtendedStorageEngine
Dictionary<string, Guid> _syncPointIndex;
Dictionary<string, SortedSet<ISyncPointInformation>> _syncPoints;
- static readonly Lazy<IEnumerable<Type>> allStorageTypes = new Lazy<IEnumerable<Type>>(
- () => Utility.GetAllTypesImplementingInterface(typeof(ISerializableItem)).ToArray());
-
Func<object, IObjectSerializationProvider> serializerFactory;
public EsentStorageEngine(string databasePath)
@@ -1,18 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Text;
using System.IO;
using Newtonsoft.Json;
-using ReactiveXaml;
-using System.Reflection;
-using System.Runtime.Serialization;
-using System.CodeDom;
-using System.Collections.ObjectModel;
-using System.Disposables;
-using System.ComponentModel;
using System.Globalization;
+#if SILVERLIGHT
+using System.IO.IsolatedStorage;
+#endif
+
namespace ReactiveXaml.Serialization
{
public class DSESerializedObjects
@@ -29,9 +25,6 @@ public class DictionaryStorageEngine : IStorageEngine
Dictionary<Guid, string> itemTypeNames;
Dictionary<string, Guid> syncPointIndex;
- static readonly Lazy<IEnumerable<Type>> allStorageTypes = new Lazy<IEnumerable<Type>>(
- () => Utility.GetAllTypesImplementingInterface(typeof(ISerializableItem)).ToArray());
-
Func<object, IObjectSerializationProvider> serializerFactory;
public DictionaryStorageEngine(string Path = null)
@@ -81,7 +74,11 @@ public void FlushChanges()
initializeStoreIfNeeded();
this.Log().Info("Flushing changes");
var dseData = new DSESerializedObjects() {allItems = this.allItems, syncPointIndex = this.syncPointIndex, itemTypeNames = this.itemTypeNames};
- File.WriteAllBytes(backingStorePath, serializerFactory(dseData).Serialize(dseData));
+
+ using (var sw = getWriteStreamFromBackingStore(backingStorePath)) {
+ byte[] buf = serializerFactory(dseData).Serialize(dseData);
+ sw.Write(buf, 0, buf.Length);
+ }
}
public Guid[] GetAllObjectHashes()
@@ -135,6 +132,29 @@ public Guid[] GetOrderedRevisionList(Type type, string qualifier = null)
return ret.ToArray();
}
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing) {
+ initializeStoreIfNeeded();
+ FlushChanges();
+ }
+
+ allItems = null;
+ if (backingStorePath != null && !disposing)
+ throw new Exception("Always dispose the Storage Engine");
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ~DictionaryStorageEngine()
+ {
+ Dispose(false);
+ }
+
void initializeStoreIfNeeded()
{
if (allItems != null)
@@ -148,11 +168,13 @@ void initializeStoreIfNeeded()
}
try {
- var serializer = new JsonSerializer();
+ var serializer = serializerFactory(null);
DSESerializedObjects dseData;
- using(var reader = new JsonTextReader(new StreamReader(backingStorePath))) {
- dseData = serializer.Deserialize<DSESerializedObjects>(reader);
+
+ using (var sr = getReadStreamFromBackingStore(backingStorePath)) {
+ dseData = (DSESerializedObjects)serializer.Deserialize(sr.GetAllBytes(), typeof (DSESerializedObjects));
}
+
allItems = dseData.allItems;
syncPointIndex = dseData.syncPointIndex;
itemTypeNames = dseData.itemTypeNames;
@@ -169,28 +191,25 @@ static string getKeyFromQualifiedType(Type type, string qualifier)
return String.Format(CultureInfo.InvariantCulture, "{0}_{1}", type.FullName, qualifier);
}
- protected virtual void Dispose(bool disposing)
+#if SILVERLIGHT
+ protected Stream getReadStreamFromBackingStore(string path)
{
- if (disposing) {
- initializeStoreIfNeeded();
- FlushChanges();
+ using (var fs = IsolatedStorageFile.GetUserStoreForApplication()) {
+ return fs.OpenFile(path, FileMode.Open);
}
-
- allItems = null;
- if (backingStorePath != null && !disposing)
- throw new Exception("Always dispose the Storage Engine");
}
-
- public void Dispose()
+
+ protected Stream getWriteStreamFromBackingStore(string path)
{
- Dispose(true);
- GC.SuppressFinalize(this);
+ using (var fs = IsolatedStorageFile.GetUserStoreForApplication()) {
+ return fs.OpenFile(path, FileMode.Create);
+ }
}
+#else
+ protected Stream getReadStreamFromBackingStore(string path) { return File.OpenRead(path); }
+ protected Stream getWriteStreamFromBackingStore(string path) { return File.Open(path, FileMode.Create); }
+#endif
- ~DictionaryStorageEngine()
- {
- Dispose(false);
- }
}
}
@@ -1,10 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using ReactiveXaml;
-using System.Disposables;
namespace ReactiveXaml.Serialization
{
@@ -153,23 +153,19 @@ public override bool CanConvert(Type objectType)
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
- if (reader.TokenType != JsonToken.String) {
- throw new Exception(String.Format("Expected string, got {0}", reader.Value));
- }
-
- Guid contentHash;
- if (!Guid.TryParse((string)reader.Value, out contentHash)) {
- throw new Exception(String.Format("Expected Guid, got {0}", reader.Value));
+ if (reader.TokenType != JsonToken.Bytes) {
+ throw new Exception(String.Format("Expected bytes, got {0}", reader.Value));
}
+ var contentHash = new Guid((byte[]) reader.Value);
return _engine.Load(contentHash);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var si = (ISerializableItem)value;
_engine.Save(si);
- writer.WriteValue(si.ContentHash.ToString());
+ writer.WriteValue(si.ContentHash.ToByteArray());
}
}
@@ -1,17 +1,23 @@
using System;
-using System.Collections.Generic;
using System.Linq;
-using System.Text;
-using ReactiveXaml;
-using System.Security.Cryptography;
using System.Runtime.Serialization;
-using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
+#if WINDOWS_PHONE
+using Microsoft.Phone.Reactive;
+#else
+using System.Concurrency;
+#endif
+
namespace ReactiveXaml.Serialization
{
+#if WINDOWS_PHONE
+ [DataContract]
+ public abstract class ModelBase : ReactiveObject, ISerializableItem
+#else
[DataContract]
public abstract class ModelBase : ReactiveValidatedObject, ISerializableItem
+#endif
{
[IgnoreDataMember]
public Guid ContentHash { get; protected set; }
@@ -40,12 +46,13 @@ public virtual Guid CalculateHash()
[IgnoreDataMember]
public IObservable<object> ItemChanging {
- get { return Changing.Select(_ => this); }
+ // XXX: We need the explicit type on SL4 :-/
+ get { return Changing.Select<IObservedChange<object, object>, object>(_ => this); }
}
[IgnoreDataMember]
public IObservable<object> ItemChanged {
- get { return Changed.Select(_ => this); }
+ get { return Changed.Select<IObservedChange<object, object>, object>(_ => this); }
}
}
}
@@ -84,9 +84,13 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
- <Compile Include="DictionaryStorageEngine.cs" />
+ <Compile Include="DictionaryStorageEngine.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Interfaces.cs" />
<Compile Include="JsonNetObjectSerializationProvider.cs" />
+ <Compile Include="MD5.cs" />
+ <Compile Include="MD5Managed.cs" />
<Compile Include="ModelBase.cs" />
<Compile Include="NaiveExtendedEngine.cs" />
<Compile Include="NullStorageEngine.cs" />
@@ -82,9 +82,13 @@
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
- <Compile Include="DictionaryStorageEngine.cs" />
+ <Compile Include="DictionaryStorageEngine.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Interfaces.cs" />
<Compile Include="JsonNetObjectSerializationProvider.cs" />
+ <Compile Include="MD5.cs" />
+ <Compile Include="MD5Managed.cs" />
<Compile Include="ModelBase.cs" />
<Compile Include="NaiveExtendedEngine.cs" />
<Compile Include="NullStorageEngine.cs" />
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
-using System.Concurrency;
+using System.IO;
using System.Linq;
-using System.Text;
-using ReactiveXaml;
-using System.Security.Cryptography;
using System.Runtime.Serialization;
-using System.ComponentModel.DataAnnotations;
-using System.ComponentModel;
+using System.Security.Cryptography;
+using System.Text;
+
+#if WINDOWS_PHONE
+using Microsoft.Phone.Reactive;
+#else
+using System.Concurrency;
+#endif
namespace ReactiveXaml.Serialization
{
@@ -103,12 +106,16 @@ void setupCollection(IScheduler sched)
public Guid CalculateHash()
{
- // XXX: This is massively inefficient, we can do way better
- return new Guid(String.Join(",",
- this.Select(x => {
- var si = x as ISerializableItem;
- return (si != null ? si.ContentHash.ToString() : x.ToString().MD5Hash().ToString());
- })).MD5Hash());
+ var buf = new MemoryStream();
+ foreach(var v in this) {
+ var si = v as ISerializableItem;
+ if (si != null) {
+ buf.Write(si.ContentHash.ToByteArray(), 0, 16);
+ }
+ }
+
+ var md5 = MD5.Create();
+ return new Guid(md5.ComputeHash(buf.ToArray()));
}
public Type GetBaseListType()
Oops, something went wrong.

0 comments on commit 0eb5f87

Please sign in to comment.