Reputation: 50
Alright, so I'm attempting my hand at some pattern programming in my effort to further my skill set. I am converting a Win Forms application to WPF utilizing the MVVM pattern. The application is basically a GUI for a mainframe session. It navigates through various panels on the mainframe screen grabbing data for eventual editing and updating. I believe I have grasped the core concepts of MVVM per below, but have a question.
I have built (exhausting with 100 or so data points) a data MODEL that represents all properties of the object I will be manipulating. I have also created proxy properties on the VIEWMODEL which implements INPC for binding on the VIEW. Now from what I have read, the business logic to grab the data should be the responsibility of the MODEL. The VIEWMODEL cleans that data up and presents it to the VIEW where it is finally displayed.
So this all seems to be functioning but here is the issue:
[SKIP TO HERE FOR THE QUESTION]
If the MODEL controls the business logic and pulls the data from the mainframe session over a series of different panels, how do I continue to update the VIEW through the VIEWMODEL as to what point the data pull is at. In essence a sort of progress indicator of the MODEL getting the data. I know I can call a method on the MODEL then report back, but how do I call the method and report back during the methods progress without the MODEL knowing of the existence of the VIEWMODEL. This is of course without implementing INPC directly on the MODEL.
Thanks for any help,
Aaron Van
Upvotes: 0
Views: 1218
Reputation: 9478
Generally the ViewModel will have operations to fetch data and not the entity model. So your CustomerViewModel might have a GetCustomers
method.
Also check out the async CTP or if you're using Visual Studio 11 you don't need the CTP I believe. The new async stuff makes it very easy to make async call and dispatch updates to a progress indicator on the UI thread for example.
Asynchronous Programming with Async and Await http://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx
Upvotes: 0
Reputation: 11495
If you are using a background thread, you might use a BackgroundWorker
and use the ProgressChanged event to report progress back to the ViewModel. Since that is really an implementation detail you might design your model with a rich event model.
One way:
public enum MainframeLoadStage { StageOne, StageTwo, StageThree }
public class LoadingStageEventArgs : EventArgs {
public MainframeLoadStage { get; set; }
}
class MyMainframeModel {
public event EventHandler<LoadingStageEventArgs> StageLoaded;
}
Or if you need fine grained data per "stage" of your loading:
public class MyMainframeModel {
public event EventHandler<LoadingStageOneEventArgs> StageOneLoaded;
public event EventHandler<LoadingStageTwoEventArgs> StageTwoLoaded;
}
Alternately, if you want, you can report status back for a single operation on the model by passing in a callback along with the invocation:
public class MyMainframeModel {
public void Load (Action<LoadingMessage> callback);
}
Even further, you could think about using Task Parallel Library (TPL) and use the featureset from there: http://msdn.microsoft.com/en-us/library/dd537609.aspx
The role of your ViewModel in all this is of course to translate these events from your model into whatever form works best for the UI (usually through INPC, as you stated).
Upvotes: 1