In preparation for the Advanced WPF Workshop on Saturday Feb 20th, I thought I would throw together some blog posts around some techniques / code I will be sharing. JP asked the other day whether Views should have references to their ViewModels or should ViewModels have references to Views. Unfortunately twitter is a great medium for asking this question but not the best medium for answering the question.
I am going to try using the dry marker board as a form of communication with my blogs. I have little patience for lining up pretty lines and colored boxes with Visio or Power Point. A dry marker board is fast and allows for a free flowing expression of my ideas.
Let me try to diagram out how I currently manage my Views and ViewModels.
- We ask the StructureMap container for the ViewModel
- The VM instance is intercepted by a ViewModelTypeInterceptor
- Inside the VMTypeInterceptor
- Get the V for out VM from the container
- Get the ViewRegistry from the container
- Register the V with the VM in the ViewRegistry
- Return the VM
- And finally our VM is returned being registered with a V in the ViewRegistry
In short my ViewModel has no reference to my View, my View has no reference to my ViewModel (beyond setting the DataContext), and my View Registry registers the mappings between View and ViewModel.
The best code to look for this is in the ViewModelInterceptor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|