开发者

IDataService in MVVM-Light toolkit V4 Beta

开发者 https://www.devze.com 2023-04-12 09:58 出处:网络
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

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 开发者_如何学Goshould 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:

IDataService ds = ViewModelBase.IsDesignTimeStatic // from the MVVMLight toolkit
? (IDataService) new DesignTimeDataService()
: (IDataService) new DataService();


The structure provided form MVVM Light, as well as the example, is thought with your application connecting to a service.

Laurent explains it in his video Understanding the Model-View-ViewModel Pattern

and here DEEP DIVE MVVM at #MIX11

there is a very good explanation of the model.

What I've done is to replace the DataService with my onw libraries to access the REST mode via async HTTPRequest. Due to the nature of the async communication I decided not to use the callback(), so I've removed it, but used messages to send back the data via the payload of the message.

In my understanding you have to use those classes as the ViewModels are initialized with the IDataService as parameter and I've not found any way to change it.

Hoping it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号