WPF, ViewModels & Autofac

In MVVM there are lots of elaborate ways to make sure our XAML’s DataContext is correctly instantiated, and if you are using IoC then you should be letting the IoC container instantiate your viewmodel, which doesn’t fit naturally into the XAML view paradigm.

An easy way to do this is to create a XAML resource that will resolve your ViewModel for you, via your IoC container.

Many MVVM frameworks offer this feature, but I wanted one to specifically work with Autofac, so I have written on for this purpose.

Here’s my data provider class

[Read more...]

Responsive WPF With Asynchronous Bindings

I see a lot of developers writing code in their ViewModels that include a lot of data initialization logic that is performed eagerly, before the data is required. Most developers believe that lazy initialization is a tricky thing to achieve in MVVM, because it is difficult to know when exactly a XAML data binding will need the data in question.

Another area of confusion is the incorrect belief that property data that is lazily initialized needs to be marshaled onto the UI thread via the dispatcher. All of this misunderstanding and misinformation often leads to complex viewmodels that include a lot of complex asynchronous code as well as a heavy sprinkling of Dispatcher.BeginInvoke.

The answer is to use a little known property of the binding class called ‘IsAsync’.

[Read more...]

INotifyPropertyChanged – Not Required!

Here’s another quickie

Question : Does your DataContext (or ViewModel if you’re doing MVVM) have to implement INotifyPropertyChanged in order to participate in XAML data binding and change synchronization (and all other goodies that follow)

Answer: Absolutely NOT !

Its an almost ubiquitous misunderstanding that unless you implement INotifyPropertyChanged on your class that is used as a binding source/target (DataContext) then your XAML wont work. Well it will – perfectly ! (well almost)

When you create a binding expression on XAML like this

[Read more...]

WPF & Background Threads – Have You Got It Wrong?

Just a quickie

You have MVVM and you have properties that notify via INotifyPropertyChanged.

Question : Do I have to use Dispatcher.BeginInvoke in order to change my bound properties in my ViewModel (i,e. when changing property values on background threads)

Answer: No. The Binding class will automatically marshal property changes via INotifyPropertyChanged onto the dispatcher for you automatically, and most importantly efficiently – by which I mean it evaluates the binding sources and targets and decide whether to marshal asynchronously or synchronously, and at what priority depending on other factors. This is half the reason for not bothering with MVVM frameworks like Caliburn, as they mostly perform tasks for you that don’t actually need to be performed.

[Read more...]

FX Trading & Reactive Extensions (Rx) – Part 1 – Getting a Feel

My current contract is within FX trading, supporting a front-office team with high-performance, low latency applications.

We are currently doing a refactoring exercise around our currency pair data feeds, and we are looking to code better data consumption logic through implementations of IObsrevable and IObserver, courtesy of the Reactive Extensions library.

Form more information about Reactive Extensions, you should visit the Microsoft Reactive Extensions site (http://msdn.microsoft.com/en-us/data/gg577609) or read Lee Campbell’s excellent (free) eBook (http://www.amazon.co.uk/Introduction-to-Rx-ebook/dp/B008GM3YPM/)

Firstly I used LinqPad (http://www.linqpad.net/) to play around with potential candidates for an approach.
Firstly, I thought I’d create a fake currency rate ticker and observe it.

[Read more...]