views:

58

answers:

1

Hello,

To grab some content from a WCF Data Service into my View Model is straight forward:

public const string RequestsPropertyName = "Requests";
private DataServiceCollection<Request> _requests = null;
public DataServiceCollection<Request> Requests
{
  get { return _requests; }

  set
  {
    if (_requests == value) { return; }

    var oldValue = _requests;
    _requests = value;

    RaisePropertyChanged(RequestsPropertyName, oldValue, value, true);
  }
}

and then

Requests.LoadAsync(query);

But what if I have a property which is not a collection?

public const string RequestDetailsPropertyName = "RequestDetails";
private Request _requestDetails = null;
public Request RequestDetails
{
  get { return _requestDetails; }

and so on. Where do I get the 'LoadAsync(query)' method from?

Thank you,

Ueli

A: 

This is a pretty simple thing to do. You just need to use the DomainContext in your application. This is where you create your query from, then apply the result to your property.

Here is an example of what this might look like in your code:

    void LoadRequest(int requstID)
    {
        var query = workContext.GetRequestByIDQuery(requestID);
        workContext.Load(query, lo =>
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        if (lo.HasError)
                            throw lo.Error;
                        else
                            RequestDetails = lo.Entities.Single();
                    });
        }, null);
    }

In this example, the workContext object is the DomainContext. The query is an specific version on the server - you can also just contruct the query client side with:

.Where(r => r.RequestID == requestID)  

After the async call, it thows any errors that occurred from the async call, and then returns the only entity returned. If you get more than 1 entity, you might use .First() instead.

If this is not enough to get you going, let me know and I can explain further.

Ryan from Denver
Hi Ryan, Thank you for your answer. I´m stuck with the following: I don't have a 'Load' method in my context. Besides, I'm not quite sure how in your example the context would be aware of what I just downloawded (an be able to save changes back with the 'BeginSaveChanges' method). Am I missing something? Thank you, Ueli
Ueli Sonderegger
Sorry, I assumed that you would be using RIA Services. Perhaps you are using WCF Services directly? If so, please post the code to show where you create the query object in your code, as noted in your second object. This must occur on the connection object. The DataServiceCollection is just a collection. It is not a connection object.
Ryan from Denver