Skip to content

Instantly share code, notes, and snippets.

@factormystic
Created June 20, 2014 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save factormystic/3d44bf95ecb0af6684a7 to your computer and use it in GitHub Desktop.
Save factormystic/3d44bf95ecb0af6684a7 to your computer and use it in GitHub Desktop.
example of akavache + dynamic
using Akavache;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
namespace akavache_dynamic_deserialization_3
{
class ExampleDynamic
{
public List<dynamic> list;
}
class Example
{
public List<string> list;
}
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void RunTest()
{
var action = new Action<string>(str => Debug.WriteLine(str));
try
{
var example = new Example()
{
list = new List<string> {
"string literal"
}
};
action(example.list[0]);
}
catch (Exception)
{
Debug.WriteLine("no exception");
}
try
{
dynamic example = new ExampleDynamic()
{
list = new List<dynamic> {
new { z = "all ok here" }
}
};
action(example.list[0].z);
}
catch (Exception)
{
Debug.WriteLine("still no exception");
}
GetOrFetchExample().Subscribe(example =>
{
try
{
action(example.list[0]);
}
catch (Exception)
{
Debug.WriteLine("this didn't happen");
}
});
GetOrFetchExampleDynamic().Subscribe(example =>
{
try
{
action(example.list[0].z);
}
catch (Exception)
{
Debug.WriteLine("this shouldn't have happened, but did");
}
});
}
static IObservable<ExampleDynamic> GetOrFetchExampleDynamic()
{
Debug.WriteLine("about to get/fetch...");
return BlobCache.InMemory.GetOrFetchObject<ExampleDynamic>("ExampleDynamic", new System.Func<Task<ExampleDynamic>>(() =>
{
Debug.WriteLine("fetching...");
return Task.Factory.StartNew<ExampleDynamic>(() =>
{
return new ExampleDynamic()
{
list = new List<dynamic> {
new { z = "wow" },
},
};
});
}));
}
static IObservable<Example> GetOrFetchExample()
{
Debug.WriteLine("about to get/fetch...");
return BlobCache.InMemory.GetOrFetchObject<Example>("Example", new System.Func<Task<Example>>(() =>
{
Debug.WriteLine("about to fetching...");
return Task.Factory.StartNew<Example>(() =>
{
return new Example()
{
list = new List<string> {
"wow again",
},
};
});
}));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RunTest();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment