WPF: MV-VM Sample Application with Navigation


I have recently been reminded of how difficult it can be to get started when you are trying to build an application and have been given a completely clean sheet of paper to start from – in terms of the application architecture and design – the options are limitless, which can at times be a little daunting.

Recently, to help a colleague combat the clean sheet of paper syndrome, we had a conversation about some of the patterns and ideas that he might want to consider with the particular application he is writing with WPF. The result of the meeting, after 90 minutes of discussion, was a whiteboard full of pattern names, blocks and arrows, which all appeared to be clearly understood by all involved at the time and we left feeling that it was a job well done. However, it occurred to me on the drive home that when faced with a blank code editor the next morning, would all those squiggles and arrows still make sense?

That lead me to thinking that a simple application that outlines not just the theory of some of the standard patterns that I use for building applications today, but also shows concrete implementations, real strategies, real code and compromises, not just code snippets of isolated issues, but a complete, albeit simple, application.

The following sample application, and accompanying screencast, are the results of that thinking. When I handed off both of these to my colleague it also occurred to me that others might benefit from the sample and would also serve as a reminder for me in the future, hence this post.

The application itself is somewhat trivial, but the patterns and architecture discussed are not. The sample covers:

  • ModelView-ViewModel (Application architecture)
  • Repository and Service Patterns (data modelling and retrieval)
  • Command Pattern (for communication between the Views and the ViewModels) [1]
  • Various different types of View, including Page and Window [2]
  • Text to FlowDocument value converter
  • Poor man’s Dependency Injection

Click here to download the sample code.

I sincerely hope that you get some value out of this post, and as usual if you have any comments, questions, flames, enhancements I would love to hear from you. In the meantime, think deeply and enjoy.

[1] Including a hybrid DelegateCommand class inspired by standard MV-VM commanding, sometimes referred to as a RelayCommand, and Prism’s DelegateCommand implementation

[2] Page based Views and navigation are specific to the application discussed in the 90 minute meeting, where what you would usually see in a traditional MV-VM discussion would be UserControl instances as the Views. I think the use of Pages in the sample makes it a more interesting sample that shows more varied View types than you would normally see.

No comments: