AgFx = Developing Data Driven Windows Phone 7 App Joy

What are app's without data? Just about every app needs to read or write data. Mobile app's especially so. I mean, if it's not a game, and not a fart app, what mobile app doesn't need data (then again, most games have some sort of data element... and the best fart apps are cloud driven, socially integrated, data connected somehow too...)?

So if this is a wheel we all need, you'd think there'd be a framework to help us build data heavy Windows Phone 7 apps...

AgFx makes writing data-heavy Windows Phone applications child's play!

I am currently busy working on 3 Windows Phone applications which are all VERY data-heavy… And with each application, I have to worry about data caching! I started playing with AgFx and haven’t looked back! It’s a little hard to get started with but once you understand their data first view of the world, you can create application VERY fast… and the best of all? They are ROCK SOLID!!!

...

Before you read any future, read their introduction: Building a connected phone app with AgFx

Everything in the AgFx world revolves around data, and more specifically… data on the web! As long as you can get to it using normal HttpWebRequest, you can use AgFx! It doesn’t matter if your data is RSS/ATOM, OData or raw XML, they all play nice with AgFx!

In my scenario, I wanted to fetch data from Twitter (ATOM), some RSS feeds (ATOM), custom OData feeds (from SQL Azure) and Flickr… and AgFx was the glue!

Rudi continues on to show just how he's using AgFx with Twitter;

I will use Twitter in this example but it works EXACTLY the same for all the other sources… to start off with, we need a ViewModel that represent the tweet

public class TweetViewModel
{
    public string From { get; set; }
    public string Text { get; set; }
    public Uri Image { get; set; }
}

Next, we need a collection of tweets

[CachePolicy(CachePolicy.CacheThenRefresh, 60 * 15)]     
public class TwitterViewModel : ModelItemBase<TwitterLoadContext>
{
    private BatchObservableCollection<TweetViewModel> tweets = new BatchObservableCollection<TweetViewModel>();
    public BatchObservableCollection<TweetViewModel> Tweets 
    {
        get { return tweets; } 
        set
        {
            tweets = value;
            RaisePropertyChanged("Tweets");
        }
    }

    #region IDataLoader

    public class TwitterViewModelDataLoader : IDataLoader<TwitterLoadContext>
    {
        private const string TwitterSearchUriFormat = "http://search.twitter.com/search.atom?lang=en&q={0}";

        public LoadRequest GetLoadRequest(TwitterLoadContext loadContext, System.Type objectType)
        {
            string uri = String.Format(TwitterSearchUriFormat, loadContext.Terms, DateTime.Now.Ticks);
            return new WebLoadRequest(loadContext, new Uri(uri));
        }

        public object Deserialize(TwitterLoadContext loadContext, System.Type objectType, System.IO.Stream stream)
        {
            var document = XDocument.Load(stream);
            var feed = SyndicationFeed.Load(document.CreateReader());

            TwitterViewModel vm = new TwitterViewModel { LoadContext = loadContext };

            foreach (var item in feed.Items)
            {
                TweetViewModel tweet = new TweetViewModel 
                { 
                    From = item.Authors.FirstOrDefault().Name, 
                    Text = item.Title.Text,
                    Image = (from _ in item.Links
                            where _.MediaType == "image/png"
                            select _).FirstOrDefault().Uri
                };
                    
                vm.Tweets.Add(tweet);
            }

            return vm;
        }    
    }

    #endregion
}

In a second article, AgFx hidden gem - PhoneApplicationFrameEx, Rudi shows off another neat little feature in AgFx

...One of the hidden gems of this package is the AgFx.Controls.Phone.dll library! It contains PhoneApplicationFrameEx… Just replace your old RootFrame with this new PhoneApplicationFrameEx

...

Now, you might wonder why this piece of code is cool? Every time you download data using AgFx (no mater on which page you are), a progress bar will be displayed on top of you page indicating to the user that data is being downloaded/parsed!

Interested in AgFx yet? Here's a snip from the Project page,AgFx Windows Phone Application Data Caching Framework

Welcome to AgFx, a framework for building Windows Phone 7 applications. This framework exists because many apps for Windows Phone 7 run into the same types of problems around managing data, keeping work off of the UI thread, and optimizing network usage. It's goal is to make all of this easy so you can focus on writing your application.

...

Features

  • Automatic caching and retrieval of data from network requests
  • Automatically handles determines if cache is valid or if a new valid data needs to be fetched
  • Easy framework for building databound view model objects based on cached data
  • Instance tracking ensures that all parts of your app are referencing the same instance - an update in one place will update data in other places
  • Flexible framework for defining how objects are cached, how long the cache is valid, and how to handle invalid cache items, etc.
  • Simple framework for app-wide broadcast messages
  • Many helper classes for implementing common pattern in Windows Phone 7 apps.
  • Debugging features to allow you to see what AgFx is doing and see reports for timings of how the network fetch and deserilization parts of your app are performing.

Overview

In brief, AgFx allows you to describe the two things that it can't automatically figure out, and then it does everything else.
There are three things that each data-connected application has to describe
  1. How to fetch it's data. This is usually an URL, but could be other things.
  2. How to deserialize that data, once fetched, into a model or view model object
  3. How long the data is valid for, and what to do when it expires. In other words, after what point should new data be fetched, and should invalid cached data be

With that information, which you define for each of your objects, AgFx can handle the rest.

If you're building a data driven Windows Phone 7 app and want to focus on your app and not the data plumbing, you might just want to check out AgFx and let it do the heavy data lifting for you...

 

Here’s a few more links you might find interesting:

 

Post image, Treasured memories, courtesy of Sim75´

Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
    Unsubscribing
    Subscribing

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.