Let’s say I have a service whose interface is something like this:
public interface IProxyRotator
{
ProxyRotationMode RotationMode { get; set; }
void Add(Proxy proxy);
void AddRange(IEnumerable<Proxy> proxies);
bool Remove(Proxy proxy);
void Clear();
Proxy Peek();
}
The questions comes into how I’m supposed to bind the proxies contained in the service to the view.
The way I’m doing is by creating a ProxyList array on the service interface and notifying changes to that property. Like this:
public interface IProxyRotator : INotifyPropertyChanged
{
// ... omited for brevity
Proxy[] ProxyList { get; }
}
On the ViewModel I have another array property for the proxies. When the ViewModel is instantiated, it then subscribes to the IProxyRotator PropertyChanged event and ‘forwards’ the event so that the View can actually see that the proxy array changed. Like this:
public class ViewModel : INotifyPropertyChanged
{
public ViewModel(IProxyRotator proxyRotator)
{
this.proxyRotator = proxyRotator;
this.proxyRotator.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "ProxyList")
{
this.RaisePropertyChanged("ProxyList");
}
}
}
public Proxy[] ProxyList
{
get { return this.proxyRotator.ProxyList; }
}
private IProxyRotator proxyRotator;
}
The problem is that the overhead of having to intercept the service PropertyChanged event and forward it bugs me, it feels.. wrong. It’s also a major drawback when I have to rely on numerous different objects that implement INotifyPropertyChanged instead properly implementing INotifyCollectionChanged.
Should I consider implementing the INotifyCollectionChanged on the IProxyRotator service (I know this implies implementing IEnumerable as well) and simply reference the service on the ViewModel, binding the view directly to the service instance?
Your comments on this are much appreciated!
Thanks!
INotifyCollectionChanged will notify you (or the view) about the adding/removing, but not about the changes in the properties (in case of updating). IPropertyChanged can notify a view that a certain property (for instence ProxyList) was changed (but not about changes of the property of the item in this collection).
Also I don’t think you need INotifyCollectionChanged interface for your service. Just use apropriate implementation for ProxyList, for example: