This project has moved. For the latest updates, please go here.

Loading Nested Objects

There will be many cases were a server request will reference objects that require another request to fully fill out. Some examples could be collections like a Flickr Photostream, which is a collection of discreet Photo objects. Another example would be when merging data from multiple services, such as one service gives you statistics about a company and another gets its current stock price.

In these cases, DataManager will do most of the work for you and you can make them easily data bindable by exposing properties with the following pattern.

In general you use the following pattern.  In this case a weather report is coming from one service, and that look up is done via a zip code.  Turning that zip code into a city name is done by another service, so we have two separate calls:

[CachePolicy(CachePolicy.ValidCacheOnly, 60 * 15)] 
    public class WeatherForecastVm : ModelItemBase<ZipCodeLoadContext>
    {
        public WeatherForecastVm()
        {
        }
        
        public WeatherForecastVm(string zipcode): base(new ZipCodeLoadContext(zipcode))
        {
        }

        /// <summary>
        /// ZipCode info is the name of the city for the zipcode.  This is a sepearate 
        /// service lookup, so we treat it seperately.
        /// </summary>
        public ZipCodeVm ZipCodeInfo
        {
            get
            {                
                return DataManager.Current.Load<ZipCodeVm>(LoadContext.ZipCode);
            }
        }

    }

 

Note that we pass any calls to ZipCodeInfo down to the DataManager to load the separate object.  This allows us to manage the lifetimes of the data separately (the cache policy for the zip code is CachePolicy.Forever), whereas we need to update the weather data more frequently.

Last edited Mar 31, 2011 at 12:35 AM by shawnburke, version 2

Comments

No comments yet.