There are only about 7 hours we have left in 2010 and this will be my last blog post for this year. For those of you who are not familiar with the new RadDomainDataSource control for Silverlight, here is my introductory blog post. This one describes how to load data with the new control and this one is about performing CRUD. Having read these three blogs might lead you to the next logical question: What about MVVM support?
I truly believe that every time someone places an UI element or a control in his view model, a baby kitten dies somewhere. So what can we do about this?
Luckily, the class that RadDomainDataSource internally uses as its view is public and can be used directly in your view models. You can learn about the relationship between the RadDomainDataSource control and the QueryableDomainServiceCollectionView here.
The architecture of my sample project is fairly simple. I have a page with a grid, a pager and a bunch of other configuration controls. All of these UI elements are bound to a common view model which is the data context of the root element:
All of the business logic happens inside this view model, which makes it perfect for unit testing. A central part of the view model is an instance of the QueryableDomainServiceCollectionView. The view itself and several of its properties are exposed as properties of the view model, so various UI elements can be bound to them:
To illustrate this, let’s take a look at how the page size can be changed by the user. The view contains a RadNumericUpDown bound to the PageSize property of the view model:
The view model simply forwards the page changing logic between the view and the model. In my case there is no additional complex logic involved, but I your real-life projects may be more complex than this:
The view model gives you unlimited opportunities to inject custom logic in both directions. As a matter of fact, RadDomainDataSource is built in a very similar way. It aggregates an instance of the QueryableDomainServiceCollectionView and simply forwards information forth and back. So, for example, when you go and change RadDomainDataSource.PageSize to 10, it will simply change the PageSize of its view to 10. In fact, you can write these two lines of code and they will do absolutely the same thing:
You can totally bypass RadDomainDataSource and work directly with the view. That’s the reason why in my previous blog post I have described RadDomainDataSource as just a “XAML-friendly thin wrapper over the QDSCV”. Come to think of it, when you are developing your very own view models that will aggregate a QDSCV, you are kind of writing your very own personal RadDomainDataSource that does exactly what you need. With the only difference that RadDomainDataSource is a control and your view model is not.
In the sample project that I have attached you will also see how to use buttons with commands, how to listen for changes in the QDSCV, and how to retrieve and expose distinct values. So attach your debuggers and hit F5.
May your source code compile and your unit tests pass in 2011! I wish you a Happy New Year!
Download Sample Project
Subscribe to be the first to get our expert-written articles and tutorials for developers!