I’m learning Silverlight for WP7 and stumbled upon the MVVM Light toolkit. I thought it would be a good Idea to learn the newest thing so I installed the V4 Beta. Sadly there isn’t any documentation to it (yet?).
In the Model-Folder there are 3 Files, DataItem, DataService and IDataService.
public class DataItem
{
public DataItem(string title)
{
Title = title;
}
public string Title { get; private set; }
}
public class DataService : IDataService
{
public void GetData(Action<DataItem, Exception> callback)
{
// Use this to connect to the actual data service
var item = new DataItem("Welcome to MVVM Light");
callback(item, null);
}
}
public interface IDataService
{
void GetData(Action<DataItem, Exception> callback);
}
These classes are used by the MainViewModel for getting the value of a property.
Now to the question: Are these the classes you should use (specifically IDataService)? I can’t seem to find a way to use them effectively because the DataItem only contains a string (or is it meant to be used as a base class?).
I’ve used the IDataService successfully to provide a testing and dummy data hook. The code that you have is an example of how you should use it. The DataItem is an example of an “Entity” or “DTO (Data Transfer Object)” that represents data from the database or service. If you’re using WCF it would be the object that is generated when you do “Add Service Reference”. The DataService class is a representation of the interface. This DataService class would have methods to call the real web service and do CRUD actions. You could also have a DesignTimeDataService: IDataService that has the same methods, but creates the data using a foreach in memory. You could then use IoC or other Dependency injection to inject the implementation at runtime.
In my App.xaml.cs in Silverlight, I create an IDataService and use that throughout my application: