WPF and MVVM





5.00/5 (1 vote)
WPF and MVVM
One of the most interesting features of the .NET Framework I have been using in recent times is Windows Presentation Foundation which allows graphical interfaces to be described using XAML. This allows rich user interfaces to be put together simply and easily. One of the main areas of thinking on top of WPF is the Model-View-ViewModel approach to application design, which allows for the ‘separation of concerns’ or layering of an application design, with each of those 3 areas only dealing with a particular area.
By separating the 3 layers out, we can make testing our application through the use of unit testing frameworks such as NUnit much simpler, and this in turn should lead to more stable, better designed applications.
The 3 layers are the ‘view’ which is effectively just the UI description in XAML. There is some debate about how much code we can put in the view, but effectively it is just UI code and nothing else. There should theoretically be no data stored in the UI.
The ‘view-model’ is a class object that is purely in C# [or VB.NET] code and exposes the data and commands to the view. The view model is not coupled to the view, it has no knowledge of the view. Because of this, all features of the view model should be testable. We assign the view model to the view at run time through the use of a ‘data context’.
Models are all our other objects that do not directly provide data or services to the view.
Throughout the next few posts, I aim to very simply demonstrate the basics of WPF and MVVM, and then move to some more complex scenarios. I don't plan on doing this through making a full application, but rather focus on small areas of detail that you may be able to incorporate into your own projects, should you wish to do so.
There are also many other advantages of WPF, that are enabled through the use of frameworks such as Prism, which I will discuss when the time comes.
Finally, I would also like to say that I don't believe WPF is the answer to everything. For small, personal applications or throw-away applications that require a user interface, the investment in time and learning required for WPF may not be suitable.
Filed under: C#, CodeProject, WPF
Tagged: C#, Introduction, Model, MVVM, Net, View, Viewmodel, WPF, XAML