For example, a typical property in my view model might look like this:
private PagedCollectionView foos;
public IEnumerable<Foo> Foos
{
get
{
if (foos == null)
{
Context.Load(Context.GetFoos().Where(f => f.Filter ==
this.CurrentFilter));
foos = new PagedCollectionView(Context.Foos);
foos.Filter = FoosFilter;
}
}
set
{
foos = value;
RaisePropertyChanged("Foos");
}
}
When the CurrentFilter changes I just do a Foos = null. What I like about
this style is that the ViewModel is usually just reacting to what the View
asks for and not jumping ahead. If the View isn't binding Foos at the
moment then the PagedCollectionView isn't created and no data is loaded
from the server. Changing the CurrentFilter doesn't cause new data to load
from the server either, it just clears the foos PagedCollectionView and
raises the PropertyChanged event which, if the View is still bound, will
eventually cause new data to be loaded.
I find this style lends itself well to long lived ViewModels that are being
kept in an IoC container and ViewModels that are being shared by multiple
Views. However, is this a good general way to design ViewModels or are
there design problems with being so property driven that I haven't hit yet?
My main concern would be the risk of reducing UI responsiveness. I
would avoid stepping out to grab data from the UI thread. I would bind
to an Observable collection, and trigger the population some other
way.
http://bea.stollnitz.com/blog/?p=27
On Feb 5, 4:53 pm, CBl...@EKI-CONSULTING.COM wrote:
Sorry, I forgot to underscore a few things. I am using Silverlight and WCF RIA Services. Context is the DomainContext and Context.Load is async. Unless there are already entities in the EntitySet which match the filter, the call to the Foos property is returning an empty PagedCollectionView. The PagedCollectionView will fill itself when the load completes and it is Observable.
Colin Blair
Daniel Vaughan <dbva...@gmail.com> Sent by: wpf-di...@googlegroups.com 02/05/2010 08:07 AM PST Please respond to wpf-di...@googlegroups.com |
To |
WPF Disciples <wpf-di...@googlegroups.com> |
cc |
||
bcc |
||
Subject |
[WPF Disciples] Re: Passive MVVM | |