As any .Net UI developer will tell you, INotifyPropertyChanged is a fundamental part of 'binding' an object to a UI control. Without it binding is essentially one-way: changes in the control change the object, but if this has a ripple effect on other properties, or properties are changed by other 'below the UI' processes, the UI can't know to repaint. This is essentially an implementation of the Observer pattern[1].
Unfortunately it's not for free - you have to implement it yourself - and that's where the problems start. So much has been written on the pain of implementing INotifyPropertyChanged (INPC for short) that I need not repeat it all here. It's generated so many questions on StackOverflow you'd think it's due its own StackExchange site by now.
The principal complaints are around all the boilerplate code and magic strings required to implement, so for the sake of completeness I'll summarize some of the solutions available:
- Design-time assistance to crank out the boilerplate through Snippets, or tools like ReSharper that also facilitate refactoring (without breaking magic strings)
- Compile-time IL re-writing approaches such as
NotifyPropertyWeaverFody and PostSharp - Run-time implementations using reflection, expression trees (such as in Caliburn.Micro's PropertyChangedBase), call interception using ContextBoundObject or Dynamic Proxies (e.g. Castle or roll-your-own), or the use of [CallerMemberName] in .Net 4.5
- Employing the Value Object pattern and turning all your your properties into some kind of Notifiable<T> (Ayende has a good example, here’s another). This does mean changing all your binding however (x.FirstName becomes x.FirstName.Value)
No.
It's so much worse than that.
Do you remember the first time you implemented GetHashCode(), and later when you realized you'd done it wrong? And later when you really realized you'd done it wrong, that there was no good way you could override Equals() for a mutable object, and the sneaking realization that this whole problem existed only for the benefit of Hashtables? It's a bit like that.
What we have with INotifyPropertyChanged is an implicit contract, that is to say that a large part of the contract can't be formally defined in code. Which means you have to validate your implementation manually. In this case the implicit bit is about threading. INotifyPropertyChanged exists to support UI frameworks and (bizarrely, in this day-and-age) they are still single threaded, and can only execute on the thread that constructed them – including event handlers. Think about this a bit, and you will eventually conclude:
An object that implements INotifyPropertyChanged must raise the PropertyChanged event only on the thread that was originally used to construct any registered subscribers for that event
Now there's a problem[2].
Clearly this is something that's just not possible to check for at runtime, so your design has to cater for this. Passing objects that might have been bound to business logic that might mutate them? UI thread please. Adding an item into a collection that might be ObservableCollection? UI thread please. Doing some calculations in the background to pass back to an object that may have been bound? Marshal via UI thread please. And so on. And don't even get me started on what you do if you have two (or more) 'UI' threads[3].
This is a horrible, horrible creeping plague of uncertainty that spreads through your UI, where the validity of an operation can't be determined at the callsite, but must also take into account the underlying type of an object (violating polymorphism), where that object came from (violating encapsulation), and what thread is being used to process the call (violating all that is sacred). These are aspects that we just can't model or visualize well with current tooling, at least not at design time, and none of the solutions above will save you here.
So there you go. INotifyPropertyChanged: far, far worse than you imagined.
[1] ok, any use of .net events could be argued is Observer, but the intent here is the relevant bit: the object is explicitly signalling that it's changed state.
[2] Actually I've over-simplified, because you can have whole chains of objects listening to each other, and if any one of them is listened to by an object with some type of thread-affinity, that's the constraint you have to consider.
[3] Don’t try this at home. There are any number of lessons you’ll learn the hard way.
7 comments:
indeed you are right, but do you have solution to propose ? Is there more elegant pattern for binding?
love this. great read while chuckling at the same time at the pain.
Cheers,
John Barrett
You ViewModel is bound to the UI. Making a change to the ViewModel bound properties is analoguous to a UI manipulation, therefore if you are on a non UI thread, you should always use the Dispatcher.
@anonymous: how does an object know it is a ViewModel? How about its collaborators?
Re raising propertychanged on the UI thread. Does this meet your requirement? https://github.com/Fody/PropertyChanged/wiki/NotificationInterception ?
@Simon Nice. I didn't know Fody did that actually.
Can you get deadlocked calling Invoke() on the dispatcher? I normally try for a .Post() instead.
@piers7
Well you control the code inside PropertyChangedNotificationInterceptor. Fody just detects that class in your assembly and uses it. So you can do Post instead of an Invoke
Post a Comment