Reputation: 29
Has anyone here ever use asynchronous method as a model in MVVM architecture in C# Windows Phone 8?
For example there are 3 steps inside a method I want to do in getting information(xml) from web: 1. Get XML from web (async) 2. deserialize xml 3. "normalize" the object created in no. 2 and return the object
Inside the model there is a method GetUser()
that returns a User object when it is called from the view. But the method GetXML is async, so it returns a Task<>
. So it is like an async chain from the model to the view(the object Task<>
is getting passed onto the view). But I want to deserialize and normalize the object inside the model. The async solution(that returning a Task<>
) is indicating that I must do the logic in the view, when the result of the Task<>
has been got.
Is there any solution for this? An async method that returns the result of the Task<>
not the Task<>
itself?
Upvotes: 1
Views: 87
Reputation: 564383
Is there any solution for this? An async method that returns the result of the Task<> not the Task<> itself?
Just make your model method something like public async Task<User> GetUserAsync(...)
, and use await
within the model to compose the asynchrony for you. There's nothing that prevents this within the Model layer, very similar to how it's done in the other layers of your application.
Upvotes: 1