katie77
katie77

Reputation: 1811

MMVM Light Using WCF Asynchronously

What is a good practice to implement MMVM with WCF Services? The View model will be communicating with the service. So lets say in a view I have 3-4 data display modules. All this information for the modules is coming from different WCF Service calls. If I do this synchronously, I have a feeling that the data in the view model will take time to load.

I want to do the calls for all these service methods asynchronously with out waiting for the first call to come back. What is good way of doing this?

Upvotes: 1

Views: 1014

Answers (1)

Ben
Ben

Reputation: 1216

I think the best way is to call the Service Asynchronously and then assign value on Complete method, like:

 class TestViewModel : ViewModelBase
{
    private ObservableCollection<string> data;

    public ObservableCollection<string> Data
    {
        get { return data; }
        set
        {
            if (value == data) return;
            data = value;
            RaisePropertyChanged("Data");
        }
    }

    public TestViewModel()
    {
        GetDataClient proxy = new GetDataClient();

        System.EventHandler<GetDataCompletedEventArgs> Client_GetDataCompleted = null;
        Client_GetDataCompleted = (s, e) =>
        {
            if (e.Error == null && e.Result != null)
            {
                Data = new ObservableCollection<SelectionItem<string>>(e.Result));                    
            }

            proxy.GetDataCompleted -= Client_GetDataCompleted;
        };

        proxy.GetDataCompleted += Client_GetDataCompleted;            
        proxy.GetDataAsync();            
    }
}

Upvotes: 1

Related Questions