Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 19 February 2011

WP7Contrib: Isolated Storage Cache Provider

Posted on 09:00 by Unknown
15/04/2001 - The code for this example has been updated - new WP7 build of SilverlightSerializer, this has removed the explicit implementation of ISerializeObject interface from the example model classes.

WP7Contrib received it's first question on the discussions forum, it was about how to use the IsolatedStorageCacheProvider.

Rich & I have been using this for a while in development and the question highlighted the lack of examples on how to use some of the stuff we've pushed out. So I thought I would share some insight into the demo I produced to answer the question.

You'll find the demo in the 'CacheProviderDemo' in the 'Spikes' directory in the code-base. It demostrates how to add both value & reference types to the cache, how these gets persisted to isolated storage and how values are purged from the cache. Before I show some code snippets I'll give some background to our thinking about caching.

As wikipedia states 'a cache is a component that transparently stores data so that future requests for that data can be served faster...'.

This is basis for the caching mechanisms in the WP7Contrib along with the principle that any implementation should be fault tolerant, i.e. if the cache fails to add or remove items this should not cause the host application to crash, the only exhibited behaviour would be a slight degradation in application performance.

The isolated cache provider has 3 requirements for any items you want to store:
  • Value types - These can not be used directly with the cache provider, the wrapper class CacheValue has to be used with these or cast to the type of Object. The cache provider only supports storing reference types,
  • Serialization - Any item you want to add to the cache (key or value) must support serialization via the SilverlightSerializer, Mike Talbot's blog has detailed information about to mark reference types for serialization and providing custom implementation through the ISerializeObject interface,
  • Hash code - Items are stored using a key-value pair hash-table in memory (as well as being persisted to isolated storage), and the key is expected to have a predictable hash code. Therefore we recommend using the properties of the reference type to calculate the value. We provide a mechanism in the BaseModel class for this purpose, CombineHashCodes, obviously you can use what ever alogrthim best fits your requirement.

That's enough background lets get to the code, as with all my demos the code is in the 'code behind'.

The snippet below shows the setup of the cache provider. There are 3 requirements for the constructor:

  • Application name - string identifing the cache, typically we use the application name, e.g. 'CacheProviderDemo',
  • Serialization assemblies - a list of any extra assemblies required for serialization by the SilverlightSerializer, these will be the assemblies containing your references types you've marked for serialization,
  • Log - captures diagnostic info about the provider.
We also initialise a variable for cache timeout - this is how long items remain in the cache before they are purged, we set the value to 20 seconds, obviously you can set this on a per item basis as required.
private const string ApplicationName = "CacheProviderDemo";
private ICacheProvider cacheProvider;
private TimeSpan cacheTimeout;
private ILog log;

public MainPage()
{
    InitializeComponent();

    var extraSerialisationAssemblies = new List<Assembly>();
    extraSerialisationAssemblies.Add(this.GetType().Assembly);

    log = new DebugLog();
    cacheProvider = new IsolatedStorageCacheProvider(ApplicationName, extraSerialisationAssemblies, log);
    cacheTimeout = TimeSpan.FromSeconds(20);
}
The final snippet below shows adding a value type to the cache and then reading the value back, both the key & value are value types in this example.
private void addValueType_Click(object sender, RoutedEventArgs e)
{
    var key = Guid.NewGuid().ToString();
    var value = new CacheValue(42);

    cacheProvider.Add(key, value, cacheTimeout);

    log.Write("Added value type to the cache...");

    var cacheValue = cacheProvider.Get<string, CacheValue>(key);
    var actualValue = (int)cacheValue.Value;

    log.Write("Value type read back from cache, value - '{0}'", actualValue);
}
The snippet below shows adding a reference type to the cache and then reading the value back, both the key & value are reference types in this example. The key is an instance of the 'ExampleReferenceTypeKey' class and supports serialization (see bottom of post for full class listing).
private void addReferenceType_Click(object sender, RoutedEventArgs e)
{
    var key = new ExampleReferenceTypeKey
        {
        FirstName = "Dirk",
        LastName = "Gently",
        Url = new Uri("http://wp7contrib.codeplex.com/"),
        Random = Guid.NewGuid()
        };

    var value = new List<Guid>();
    for (var i = 0; i < 100; i++)
       value.Add(Guid.NewGuid());

    cacheProvider.Add(key, value, cacheTimeout);

    log.Write("Added reference type to the cache...");

    var cacheValue = cacheProvider.Get<ExampleReferenceTypeKey, List<Guid>>(key);

    log.Write("Value type read back from cache, list count = {0}", cacheValue.Count);
}
The screenshot below shows the automatic persistence and purging working for the demo, it's a screenshot of the output window from visual studio. The timeline for this is as follows:

1. An item is added to the cache,
2. A maximum wait of 15 seconds happens before the cache is persisted(to isolated storage),
3. 20 seconds after the item is added to the cache it is purged from the cache,
4. A maximum wait of 15 seconds happens before the cache is persisted again.



That pretty much rounds up how the IsolatedStorageCacheProvider and as i said earlier the implementation and demo can be downloaded from the WP7Contrib project on CodePlex.


Full list for class ExampleReferenceTypeKey;

[Serializer(typeof(ExampleReferenceTypeKey))]
public class ExampleReferenceTypeKey : BaseModel, IEquatable<ExampleReferenceTypeKey>, ISerializeObject
{
    private string firstName;
    private string lastName;
    private Uri url;
    private Guid random;

    public ExampleReferenceTypeKey()
    {
    }

    public object[] Serialize(object target)
    {
        var key = (ExampleReferenceTypeKey)target;
        return new object[] { key.FirstName,
            key.LastName,
            key.Url,
            key.Random };
    }

    public object Deserialize(object[] data)
    {
        var key = new ExampleReferenceTypeKey
        {
            FirstName = (string)data[0],
            LastName = (string)data[1],
            Url = (Uri)data[2],
            Random = (Guid)data[3]
        };

        return key;
    }

    public string FirstName
    {
        get { return this.firstName; }
        set { this.SetPropertyAndNotify(ref this.firstName, value, () => this.FirstName); }
    }

    public string LastName
    {
        get { return this.lastName; }
        set { this.SetPropertyAndNotify(ref this.lastName, value, () => this.LastName); }
    }

    public Uri Url
    {
        get { return this.url; }
        set { this.SetPropertyAndNotify(ref this.url, value, () => this.Url); }
    }

    public Guid Random
    {
        get { return this.random; }
        set { this.SetPropertyAndNotify(ref this.random, value, () => this.Random); }
    }
        
    public override int GetHashCode()
    {
        return this.CombineHashCodes(FirstName, LastName, Url, Random);
    }

    public static bool operator ==(ExampleReferenceTypeKey k1, ExampleReferenceTypeKey k2)
    {
        return Equals(k1, k2);
    }

    public static bool operator !=(ExampleReferenceTypeKey k1, ExampleReferenceTypeKey k2)
    {
        return !Equals(k1, k2);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }

        return obj is ExampleReferenceTypeKey && this.Equals((ExampleReferenceTypeKey)obj);
    }

    public bool Equals(ExampleReferenceTypeKey key)
    {
        if (ReferenceEquals(null, key))
        {
            return false;
        }

        if (this.FirstName != key.FirstName)
        {
            return false;
        }

        if (this.LastName != key.LastName)
        {
            return false;
        }

        if (this.Url != key.Url)
        {
            return false;
        }

        if (this.Random != key.Random)
        {
            return false;
        }

        return true;
    }
}
Read More
Posted in Caching, CodePlex, WP7, WP7Contrib | No comments

Monday, 14 February 2011

WP7: Thinking about performance implicitly

Posted on 05:59 by Unknown
It's inevitable as the moon going around the earth - the performance of your Windows Phone 7 app will become an issue during your application development life cycle, hopefully you're using agile practices and therefore this won't happen to late in the process. There is one technique I believe you should always apply to the process and that is 'thinking about performance implicitly'.

So I have 3 tips which help me achieve thinking about performance implicitly.

The emulator is not your friend - You should as others have pointed out always be testing on a device and not the emulator - the emulator is great for spiking and unit testing but not as environment for building an application, it runs at the speed of the host and I haven't yet managed to find a WP7 phone to match the perf of my 4 * Quad Core machine :)

Get the worst device possible - Okay any device running WP7 will have been certified by MS as acceptable, but there are still great differences between them. We all want the latest with the best memory, processor etc but in the long run it's better to test against the lowest common denominator - it will save you time & money in the long term (if you can get hold of 2 devices then even better).
I'm currently using an LG GW910 and to say I don't like the device is an under statement, the responsiveness of the touch screen, the quality of the display and the number of 'white screens' compared to newer devices is noticeable, so if I can get good perf from this device then I know it will be better on newer devices, unless something less inspiring comes to the market that is.

Turn off WIFI support - When testing communication to back end services turn off WIFI, test using 2G\2.5G\3G or GSM EDGE it will give you a more real world experience. If your application works great over these it will fly on WIFI.
Read More
Posted in Development, WP7, WP7Dev | No comments

Tuesday, 8 February 2011

WP7Contrib: Location Push Model

Posted on 14:22 by Unknown
Anyone who's tried to get your current location of a WP7 device will know this is not as simple as it first appears, the problems really revolve around the frequency at which the location information can be generated by the device (GeoCoordinateWatcher class) and the fact it is generated on the UI thread. Jaime Rodriguez has a very insightful post on the issues, you should read this first if you're not familiar with the issues. For the WP7Contrib we wanted to abstract away the issues and simplify the interface for any developer wanting to get location information.

Following the pattern we used for Network Connectivity we use a push model using the MS Reactive Extensions for .Net. We use an observable sequence which returns the current location (latitude & longitude) in one of three ways - the current location, the location by time threshold (seconds or TimeSpan) and the location by distance threshold (metre).

The interface for the location service is shown below:

public interface ILocationService
{
    IObservable<GeoCoordinate> Location();
    IObservable<GeoCoordinate> LocationByTimeThreshold(int frequency);
    IObservable<GeoCoordinate> LocationByTimeThreshold(TimeSpan frequency);
    IObservable<GeoCoordinate> LocationByDistanceThreshold(int distance);
}

As you can see we have abstracted away the GeoCoordinateWatcher class and provide a clean push model using the IObservable as the return types for all variants.

The implementation of this interface can be found in the LocationService class, in the WP7Contrib.Services assembly.

We decided the implementation would not return a value when the location is unknown (latitude = NaN, longitude = NaN) - if a time out is required for trying to obtain a value then 'Timeout' observable extension should be used, and if the previous value is the same as the current value a value would not be returned - this only applies to the threshold based variants.

Examples of how easy it can be to get location information are shown below, as usual I've implemented this in the 'code behind' for simplicity.

The first example is how to get the current location:

private void currentLocation_Click(object sender, RoutedEventArgs e)
{
    currentSubscriber = locationService.Location()
       .ObserveOnDispatcher()
       .Subscribe(location =>
       {
          this.results.Insert(0, string.Format("'{0}', '{1}'", location.Latitude, location.Longitude));
       });
}

The second example is how to get the location using a distance threshold:

private void startDistance_Click(object sender, RoutedEventArgs e)
{
    if (locationSubscriber != null)
       return;

    var val = Convert.ToInt32(this.threshold.Text);
    locationSubscriber = locationService.LocationByDistanceThreshold(val)
       .ObserveOnDispatcher()
       .Subscribe(location =>
       {
          this.results.Insert(0, string.Format("'{0}', '{1}'", location.Latitude, location.Longitude));
       });
}

And the final example is how to get the location using a time threshold:

private void startTime_Click(object sender, RoutedEventArgs e)
{
    if (locationSubscriber != null)
       return;

    var val = (int)(Convert.ToDouble(this.threshold.Text) * 1000);
    locationSubscriber = locationService.LocationByTimeThreshold(val)
       .ObserveOnDispatcher()
       .Subscribe(location =>
       {
          this.results.Insert(0, string.Format("'{0}', '{1}'", location.Latitude, location.Longitude));
       });
}

These examples are from a quick application called 'RxLocationService' (the code can be found in WP7Contrib on CodePlex in the Spikes directory), screenshot shown below.


As you can see from these examples we've greatly simplifies using the location based information on a WP7 device. The complexity is hidden away in the LocationService class, and below I've included code snippets for each difference variation.

Getting the current location is straight forward - create an instance of a Subject<GeoCoordinate> class, this is returned via the IObservable<GeoCoordinate> interface then create an instance of GeoCoordinateWatcher and hook up an event handler for the PositionChanged event, when the event is fired we update the Subject<GeoCoordinate> with the new value and also importantly signal any observers that the observable sequence has finished by calling 'OnCompleted', this is important because it will shutdown the GeoCoordinateWatcher correctly. Next we start the watcher (which will start asynchronously under the covers) and return the Subject<GeoCoordinate> as an observable sequence where we fitler out an unknown location values.

public IObservable<GeoCoordinate> Location()
{
    var subject = new Subject<GeoCoordinate>();
    var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
       { MovementThreshold = FixedLocationDistance };

    watcher.PositionChanged += (o, args) =>
       {
          subject.OnNext(args.Position.Location);
          subject.OnCompleted();
       };
    watcher.Start();

    return subject.AsObservable()
       .Where(c => !c.IsUnknown)
       .Finally(() =>
       {
          watcher.Stop();
          watcher.Dispose();
       });
}

Next is the code getting the location by a distance threshold - this is very similar to the current location, but the differences are we use a BehaviorSubject<GeoCoordinate> and importantly we don't signal the observable sequence has finished in the event handler. We also use the 'DistinctUntilChanged' extension method to filter out the results so that only distinct values are returned to any observers.

public IObservable<GeoCoordinate> LocationByDistanceThreshold(int distance)
{
    var subject = new BehaviorSubject<GeoCoordinate>(GeoCoordinate.Unknown);
    var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
       { MovementThreshold = distance };
    watcher.PositionChanged += (o, args) =>
       {
          var newLocation = args.Position.Location;
          subject.OnNext(args.Position.Location);
       };
    watcher.Start();

    return subject.Where(c => !c.IsUnknown)
       .DistinctUntilChanged()
       .Finally(() =>
       {
          watcher.Stop();
          watcher.Dispose();
       })
       .AsObservable();
}

And finally the location by a time threshold - this uses the Interval method on the Observable class to trigger getting the location at the required time interval. The CurrentLocationByTime method actual returns the value from the GeoCoordinateWatcher class using the TryStart method.

private IObservable<GeoCoordinate> LocationByTimeImpl(TimeSpan timeSpan)
{
    return Observable.Interval(timeSpan)
       .ObserveOn(Scheduler.ThreadPool)
       .SubscribeOn(Scheduler.ThreadPool)
       .Select(t => this.CurrentLocationByTime(timeSpan))
       .Where(c => !c.IsUnknown)
       .DistinctUntilChanged();
}

private GeoCoordinate CurrentLocationByTime(TimeSpan timeSpan)
{
    var currentLocation = GeoCoordinate.Unknown;
    using (var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default))
    {
       if (watcher.TryStart(true, timeSpan))
       {
          currentLocation = watcher.Position.Location;
       }

       watcher.Stop();
    }

    return currentLocation;
}

So that pretty much rounds it up, as I said the code can be found in the WP7Contrib CodePlex project in the WP7Contrib.Services project.
Read More
Posted in CodePlex, WP7, WP7Contrib, WP7Dev | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Understanding RefCount in Reactive Extensions
    A couple of weeks ago  @LordHanson  & I ran into an issue converting a stateless async service exposed as an Rx cold observable to a  co...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • Bad developers love 'The Daily WTF'
    When 'The Daily WTF' started up back in 2003/2004 it was a great laugh looking at shocking code other developers wrote, but after a ...
  • Using CompositeDisposable in base classes
    To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the di...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • Comparing performance of .Net 4.5 to .Net 4.0 for WPF
    Currently I'm working on a .Net 4.0 WPF app and we've had some discussion about moving to .Net 4.5, we don't get to make the dec...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ▼  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ▼  February (3)
      • WP7Contrib: Isolated Storage Cache Provider
      • WP7: Thinking about performance implicitly
      • WP7Contrib: Location Push Model
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile