MVVM Light Silverlight Question - silverlight-4.0

Hi I've been getting started with MVVM in Silverlight. When you call your model from your viewmodel the model's call to it's Web Service is asynchronous. What is the best way of notifying the view model when you have your data. Currently I just use the messenger class to fire it back a notification with the data set up a listener on the viewmodel. This works fine but I'm not sure it's the best way of doing it. Can anyone point me in the direction of some best practices? Cheers.

I recommend using the Reactive Extensions for that. Specifically, take a look at the Subject class.

Related

What super class to choose when creating a network handler class in iOS

I'm currently creating a iOS 5 iPad app where there will be heavy network usage on a local domain, querying and fetching data, uploading and more.
So I'm thinking of creating a network handler that would take care of all these functionalities. Now in iOS/objective-c when you create a class you are really creating a interface of super classes (NSObject, UIViewController etc) and my question is which one would you use? NSURLProtocol?
I'm really new to iOS/Objective-c programming so I'm looking towards getting best practice tips from those more experienced in doing something similar to what I'm trying to do.
All advice would be appreciated.
-MrDresden
If you're creating a network manager that abstracts all the dirty work, you should make it a subclass of NSObject, simply. All the work will be done by local instances of NSURLConnection (though I advise you to use some higher-level framework like AFNetwork which is very good).

VB.NET Circular Dependency / Duplex Communication between Projects

I have 2 projects
UI
Functionality
UI references Functionality to call specific functions. Functionality needs to call certain functions that operate UI. This is what I mean by Duplex communication. Is there any way to do this without causing circular dependency?
Should there be 1 main project which simply does all the calling acting as a "wrapper" between UI and Functionality?
Your "functionality" (business layer) piece probably shouldn't be calling the "ui" puiece. In fact it should need to know anything about it since that is the point of separating your project into layers to begin with. If you had to change your UI layer to something different, web based, windows form, mobile, etc this should always be independent.
You probably want UI to call Functionality, not the other way around. That's best practice. You can use an event mechanism or callbacks if you need to observe the Functionality project and respond to events.
UI calling Functionality and Functionality calling UI is inadvertently going to be a circular reference. You want to avoid that.

Is having an instance of serviceclient at every viewmodel a good idea?

I have to refactor an application which has at almost every model and codebehind file an instance of serviceclient, and some of them use a serviceclient property from App.xaml.cs (Silverlight 4)
What is the best practice for this? Do it so, that all controls would use an instance of serviceclient kept in app.xaml.cs, or create at every viewmodel/codebehind an instance of serviceClient?
In coming few weeks (as I get an answer for my other question also) I need to write some unitests, so I need to keep this in mind.
As a best practice and a very hands-on approach to doing SL, MVVM and WCF, I would like to invite you to read and follow http://aviadezra.blogspot.com/2010/10/silverlight-mvvm-odata-wcf-data.html. Just perform this tutorial like steps and you will know how to perform your job.
Very specifically, about your question on where to create and maintain your service reference, the suggested approach is to create a static object called something like DalService that will be instanciated on first usage and just hang around and be available for any viewmodel to query the back-end.

How to implement IEditableObject for View Model?

Suppose I have VM with some entities as property. Say People, Addres, Phone, ....
Then I want to implelemt IEditableObject on VM so that user have undo functionnality.
What's the best way to implement request?
Demo code please.
Have a look at following two links which have good examples
http://www.a2zdotnet.com/View.aspx?Id=135
Using IEditableObject In Silverlight
IEditableObject doesn't make sense on a view model since all commands from the view such as Edit, Cancel, etc. are implemented in the view model itself. At best you can use IEditableObject internally in your view model on the data classes to make them easier to manage.

What is a good pattern for binding a collection of objects coming from WCF, in Silverlight?

I've got a question about a Silverlight WCF Databinding pattern:
There are many examples about how to bind data using {Binding} expressions in XAML, how to make async calls to a WCF service, set the DataContext property of a element in the UI, how to use ObservableCollections and INotifyPropertyChanged, INotifyCollectionChanged and so on.
Background:
I'm using the MVVM pattern, and have a Silverlight ItemsControl, whose ItemsSource is set to an ObservableCollection property on my ViewModel object. My view is of course the XAML which has the {Binding}. Say the model object is called 'Metric'. My ViewModel periodically makes calls to a WCF service that returns ObservableCollection. MetricInfo is the data transfer object (DTO).
My question is two-fold:
Is there any way to avoid copying each property of MetricInfo to the model class - Metric?
When the WCF calls completes, is there any way to make sure I sync the items which are in both my local ObservableCollection and the result of the WCF call - without having to first clear out all the items in the local collection and then add all the ones from the WCF call result?
thanks,
Krishna
1) I have done the mapping through a constructor like this:
public Metric(MetricInfo metricInfoDTO)
then map the properties from the DTO to the entity which of course is what you are trying to avoid. Yes, this is a bit of work but for me it has worked out very well. The alternative could be to use a object mapper like AutoMapper
2) I suppose you could have some kind of comparison logic to do updates and inserts into the collection. For me, I have done the clear and add which you describe in you question. It's simple, short and I haven't had any issues with it.