How to achieve "Blendability" when using DataServiceCollection in my ViewModel

Posted by andlju on Stack Overflow See other posts from Stack Overflow or by andlju
Published on 2010-06-12T16:25:45Z Indexed on 2010/06/12 16:33 UTC
Read the original article Hit count: 860

I'm looking at using oData endpoints in my Silverlight client. Naturally, I'm doing MVVM and I want the project to be nice and "Blendable" (i.e. I must be able to cleanly use static data instead of the oData endpoints when in design mode.)

Now to the problem. I'd like to use the DataServiceCollection in my ViewModels, since it allows for nice bindable collections without having to worry too much with BeginExecute/EndExecute etc.

Now, let's look at some code. My Model interface looks like this:

public interface ITasksModel
{
    IQueryable<Task> Tasks { get; }
}

The oData endpoint implementation of that interface:

public class TasksModel : ITasksModel
{
    Uri svcUri = new Uri("http://localhost:2404/Services/TasksDataService.svc");

    TaskModelContainer _container;

    public TasksModel()
    {
        _container = new TaskModelContainer(svcUri);
    }

    public IQueryable<Task> Tasks
    {
        get
        {
            return _container.TaskSet;
        }
    }
}

And the "Blendable" design-time implementation:

public class DesignModeTasksModel : ITasksModel
{
    private List<Task> _taskCollection = new List<Task>();

    public DesignModeTasksModel()
    {
        _taskCollection.Add(new Task() { Id = 1, Title = "Task 1" });
        _taskCollection.Add(new Task() { Id = 2, Title = "Task 2" });
        _taskCollection.Add(new Task() { Id = 3, Title = "Task 3" });
    }

    public IQueryable<Task> Tasks
    {
        get {
            return _taskCollection.AsQueryable();
        }
    }
}

However, when I try to use this last one in my ViewModel constructor:

    public TaskListViewModel(ITasksModel tasksModel)
    {
        _tasksModel = tasksModel;

        _tasks = new DataServiceCollection<Task>();
        _tasks.LoadAsync(_tasksModel.Tasks);
    }

I get an exception:

Only a typed DataServiceQuery object can be supplied when calling the LoadAsync method on DataServiceCollection.

First of all, if this is the case, why not make the input parameter of LoadAsync be typed as DataServiceQuery?

Second, what is the "proper" way of doing what I'm trying to accomplish?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about mvvm