Follow up on my previous blog on how to use the asynchronous methods of the Live Mesh in the Live Framework April 2009 CTP, I have received the comment from my colleague Ben Williams, who rightfully pointed out that with use of CreateQuery<T>() method of Mesh Application Service object, I could have accomplished the task of adding a new Data Feed in just two event handler steps comparing to the four steps shown in my previous post. Here is the new and updated snippet code that demonstrates the technique used to add a new Data Feed object into the Live Mesh:

 

        public void DataFeeds_Create(Claim claim)

        {

            this.claim = claim;

            DataFeed claimFeed = new DataFeed(claim.RefNumber);

            MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

            meshApp.DataFeeds.AddCompleted += new EventHandler<LiveItemEventArgs<DataFeed>>(DataFeeds_AddCompleted);

            meshApp.DataFeeds.AddAsync(claimFeed, claim.RefNumber);

        }

 

        public void DataFeeds_AddCompleted(object sender, LiveItemEventArgs<DataFeed> e)

        {

            MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

            var query = from datafeed in meshApp.CreateQuery<DataFeed>()

                        where datafeed.Resource.Title == (string)e.UserState

                        select datafeed;

 

            //Get a the DataFeed

            DataFeed claimFeed = query.FirstOrDefault();

            claimFeed = (DataFeed)meshApp.CreateQuery<DataFeed>().Where(d => d.Resource.Title == (string)e.UserState)

                                        .Single();

 

            //then add a new DataEntry

            DataEntryResource entryResource = new DataEntryResource();

            entryResource.Type = "Claim";

            DataEntry claimEntry = new DataEntry(entryResource);

 

            //Set the user data

            claimEntry.Resource.SetUserData<string>(claim.WriteToXml().ToString(System.Xml.Linq.SaveOptions.None));

 

            //Add DataEntry to the DataEntries collection

            claimFeed.DataEntries.AddCompleted += new EventHandler<LiveItemEventArgs<DataEntry>>(DataEntries_AddCompleted);

            claimFeed.DataEntries.AddAsync(claimEntry, claimFeed);

        }